473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

changing image size on roll over

I have been trying to write a script that will increase the size of
and image when you mouse over it, and decrease it to original size,
when you mouse out. After a couple of attempts, this is what I've come
up with. In this example, there are 5 images (0 thru 4) all with a
height of 80px. When you mouse over an image, it changes the
growingImage variable equal to the number of the image you mouse over,
when you mouse out, it sets it to 5, which means none of the images,
and runs the chechSize function. All this works fine, except every
time you mouse over an image, the time it takes to grow and shrink
decreases, until it is instantly jumping from small to large, without
transition. Any help fixing that bug would be appreciated, or if you
know a better solution to this problem, I'd love to hear it. Kenny
Here's my code. I only included one image, but there all similar.

var growingImage = 5;
var i = 0;

function resizeImage(imageNumber) {
document.images[imageNumber].height -= 1
document.images[imageNumber].width -= 1
}

function increaseSizeImage(imageNumber) {
document.images[imageNumber].height += 1
document.images[imageNumber].width += 1
}

function checkSize() {
if ((i == growingImage) && (document.images[i].height < 90)) {
increaseSizeImage(i)
}
if ((i != growingImage) && (document.images[i].height !=80)) {
resizeImage(i)
}
i++
if (i == 5) {i = 0}
setTimeout("checkSize()", 10)
}

<a href="#" onMouseOver="growingImage=0;checkSize()"
onMouseOut="growingImage=5;checkSize()"><img src="../images/Adora.jpg"
width="60" height="80" border="0" /></a>
Jul 20 '05 #1
4 16176
DU
Kenny wrote:
I have been trying to write a script that will increase the size of
and image when you mouse over it, and decrease it to original size,
when you mouse out.
[snipped]

Any help fixing that bug would be appreciated, or if you know a better solution to this problem, I'd love to hear it. Kenny


[snipped]

Here's a draft version of what I would do. The following code is valid,
has been tested and is working on Mozilla 1.5 final, K-meleon 0.8, MSIE
6 SP1 for Windows and Opera 7.23. I'm loading the file here since I have
now banner ads on my site and want to avoid that for now until I can
find another solution.
The file is a draft version because I would register event handlers for
the image(s) instead of using event attributes like in the same spirit
of this file:

Continuous modification of an image's opacity
http://www10.brinkster.com/doctorunc...icOpacity.html

Also, your file as submitted was using an embedding anchor for the
image: now, the events could be registered on the image or on the
anchor. The difference is in accessibility and usability: the default
border around links would still need to be there if link is used. I also
saw other usability issues: resorting to setTimeout(expr, 10) is way too
demanding on several systems. I personally never go under 32 msec to
avoid crashes on the users' system: some video cards and system
resources can not perform safely, confortably with DHTML at less than
32msec. In any case, animations faster than 32msec are too fast for the
eyes. If your increase/decrease height needs to be "faster", than
increase the step from 1 to 2 or more. I.e.:
document.getElementById("idImage").height += 2;

DU

---------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="date" content="2003-12-02T09:54:03+08:00" />
<meta http-equiv="imagetoolbar" content="no" />

<title>Dynamically enlarge image on mouseover and mouseout</title>

<style type="text/css">
body {margin:64px;}
</style>
<script type="text/javascript">
// <![CDATA[
var glbInc, glbDec;

function decreaseSizeImage() // will get back to its normal default size
{
if(glbInc != null) {clearTimeout(glbInc); glbInc = null;};
if (document.getElementById("idImage").height > 111)
{
document.getElementById("idImage").height -= 1;
document.getElementById("idImage").width -= 1;
glbDec = setTimeout("decreaseSizeImage()", 32);
};
}

function increaseSizeImage()
{
if(glbDec != null) {clearTimeout(glbDec); glbDec = null;};
if (document.getElementById("idImage").height < 150)
{
document.getElementById("idImage").height += 1;
document.getElementById("idImage").width += 1;
glbInc = setTimeout("increaseSizeImage()", 32);
};
}
// ]]>
</script>

</head>

<body>

<p><a href="#" onmouseover="increaseSizeImage();"
onmouseout="decreaseSizeImage();"><img id="idImage"
src="http://www10.brinkster.com/doctorunclear/GRAPHICS/JPG/KrustyHelpless.jpeg"
width="113" height="111" alt="Krusty is helpless" /></a></p>

<p id="validation"><a href="http://validator.w3.org/check/referrer"><img
src="http://www.w3.org/Icons/valid-xhtml10.png" title="Verify this
page's markup syntax" alt="Valid XHTML 1.0!" /></a> <a
href="http://jigsaw.w3.org/css-validator/check/referer"><img
src="http://www.w3.org/Icons/valid-css.png" title="Verify this page's
CSS code" alt="Valid CSS!" /></a> <a
href="http://www.webstandards.org"><img
src="http://www10.brinkster.com/doctorunclear/GRAPHICS/GIF/webstandardsproject.gif"
alt="Web standards project" /></a></p>

</body></html>

DU

Jul 20 '05 #2
DU
DU wrote:
Kenny wrote:
I have been trying to write a script that will increase the size of
and image when you mouse over it, and decrease it to original size,
when you mouse out.

[snipped]

Any help fixing that bug would be appreciated, or if you
know a better solution to this problem, I'd love to hear it. Kenny

[snipped]

Here's a draft version of what I would do. The following code is valid,
has been tested and is working on Mozilla 1.5 final, K-meleon 0.8, MSIE
6 SP1 for Windows and Opera 7.23. I'm loading the file here since I have
now banner ads on my site and want to avoid that for now until I can
find another solution.
The file is a draft version because I would register event handlers for
the image(s) instead of using event attributes like in the same spirit
of this file:

Continuous modification of an image's opacity
http://www10.brinkster.com/doctorunc...icOpacity.html
Also, your file as submitted was using an embedding anchor for the
image: now, the events could be registered on the image or on the
anchor. The difference is in accessibility and usability: the default
border around links would still need to be there if link is used. I also
saw other usability issues: resorting to setTimeout(expr, 10) is way too
demanding on several systems. I personally never go under 32 msec to
avoid crashes on the users' system: some video cards and system
resources can not perform safely, confortably with DHTML at less than
32msec. In any case, animations faster than 32msec are too fast for the
eyes. If your increase/decrease height needs to be "faster", than
increase the step from 1 to 2 or more. I.e.:
document.getElementById("idImage").height += 2;

DU

---------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="date" content="2003-12-02T09:54:03+08:00" />
<meta http-equiv="imagetoolbar" content="no" />

<title>Dynamically enlarge image on mouseover and mouseout</title>

<style type="text/css">
body {margin:64px;}
</style>
<script type="text/javascript">
// <![CDATA[
var glbInc, glbDec;

function decreaseSizeImage() // will get back to its normal default size
{
if(glbInc != null) {clearTimeout(glbInc); glbInc = null;};
if (document.getElementById("idImage").height > 111)
{
document.getElementById("idImage").height -= 1;
document.getElementById("idImage").width -= 1;
glbDec = setTimeout("decreaseSizeImage()", 32);
};
}

function increaseSizeImage()
{
if(glbDec != null) {clearTimeout(glbDec); glbDec = null;};
if (document.getElementById("idImage").height < 150)
{
document.getElementById("idImage").height += 1;
document.getElementById("idImage").width += 1;
glbInc = setTimeout("increaseSizeImage()", 32);
};
}
// ]]>
</script>

</head>

<body>

<p><a href="#" onmouseover="increaseSizeImage();"
onmouseout="decreaseSizeImage();"><img id="idImage"
src="http://www10.brinkster.com/doctorunclear/GRAPHICS/JPG/KrustyHelpless.jpeg"
width="113" height="111" alt="Krusty is helpless" /></a></p>

<p id="validation"><a href="http://validator.w3.org/check/referrer"><img
src="http://www.w3.org/Icons/valid-xhtml10.png" title="Verify this
page's markup syntax" alt="Valid XHTML 1.0!" /></a> <a
href="http://jigsaw.w3.org/css-validator/check/referer"><img
src="http://www.w3.org/Icons/valid-css.png" title="Verify this page's
CSS code" alt="Valid CSS!" /></a> <a
href="http://www.webstandards.org"><img
src="http://www10.brinkster.com/doctorunclear/GRAPHICS/GIF/webstandardsproject.gif"
alt="Web standards project" /></a></p>

</body></html>

DU


There is also this file

Dynamic magnification of an image:
http://www10.brinkster.com/doctorunc...MSIE6Zoom.html

but it only works in MSIE 6 for windows (it might work also in MSIE 5.5)
because Mozilla based browsers do not have an equivalent to MSIE's
proprietary style zoom property.
But then it would be easy to make that code work by modifying the image
width and height.
The difference with your code is that here there is no anchor
surrounding the image and the image size is entirely under the user's
control via a mouse drag, which I think is more pleasing for the user.

DU

Jul 20 '05 #3
DU
DU wrote:
DU wrote:

There is also this file

Dynamic magnification of an image:
http://www10.brinkster.com/doctorunc...MSIE6Zoom.html

but it only works in MSIE 6 for windows (it might work also in MSIE 5.5)
because Mozilla based browsers do not have an equivalent to MSIE's
proprietary style zoom property.
But then it would be easy to make that code work by modifying the image
width and height.
The difference with your code is that here there is no anchor
surrounding the image and the image size is entirely under the user's
control via a mouse drag, which I think is more pleasing for the user.

DU


I created an entirely new file which works very well for MSIE 6 for
Windows, Mozilla 1.5, K-meleon 0.8, Opera 7.23 and should work for all
DOM 2 compliant browsers. It works on a click and drag (up or down) user
interaction. The markup and CSS code would be entirely valid if it was
not of the banner ad include.

http://www10.brinkster.com/doctorunc...ification.html

DU

Jul 20 '05 #4

"Kenny" <ke*********@email.com> wrote in message
news:3d**************************@posting.google.c om...
I have been trying to write a script that will increase the size of
and image when you mouse over it, and decrease it to original size,
when you mouse out. After a couple of attempts, this is what I've come
up with. In this example, there are 5 images (0 thru 4) all with a
height of 80px. When you mouse over an image, it changes the
growingImage variable equal to the number of the image you mouse over,
when you mouse out, it sets it to 5, which means none of the images,
and runs the chechSize function. All this works fine, except every
time you mouse over an image, the time it takes to grow and shrink
decreases, until it is instantly jumping from small to large, without
transition. Any help fixing that bug would be appreciated, or if you
know a better solution to this problem, I'd love to hear it. Kenny
Here's my code. I only included one image, but there all similar.

var growingImage = 5;
var i = 0;

function resizeImage(imageNumber) {
document.images[imageNumber].height -= 1
document.images[imageNumber].width -= 1
}

function increaseSizeImage(imageNumber) {
document.images[imageNumber].height += 1
document.images[imageNumber].width += 1
}

function checkSize() {
if ((i == growingImage) && (document.images[i].height < 90)) {
increaseSizeImage(i)
}
if ((i != growingImage) && (document.images[i].height !=80)) {
resizeImage(i)
}
i++
if (i == 5) {i = 0}
setTimeout("checkSize()", 10)
}

<a href="#" onMouseOver="growingImage=0;checkSize()"
onMouseOut="growingImage=5;checkSize()"><img src="../images/Adora.jpg"
width="60" height="80" border="0" /></a>


I see your problem. When you do a mouseover, you register a timeout, that
continues to register itself again, and does not stop. When you do another
mouseover, you register a second timeout, which continues to register
itself, causing the function to be called twice as often, making it work
twice as fast. The same pattern continues to happen, until there are so
many callbacks registered, that the action happens almost immediately.

There are two ways to solve this... Either put some conditional logic around
the setTimeout call, to stop calling it when your action is done... or save
the ID that setTimeout returns in a global variable, and call clearTimeout
on that value for each mouseover, thus killing the previous routine.

My guess is that a combination of both options will be most useful... The
first one to stop the timeout from occuring when it is no longer needed, and
the second one to make sure if the first one is still running, it gets
killed, so two are not running at the same time.

Brian
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lee David | last post by:
I am using javascript to swap images when the mouse is over a "button" image. I created these with Fireworks and when I look at them in window explorer I see they all (all three "states") are...
3
by: Michael Eisenstadt | last post by:
What are the coding options for client-side image size changing? I want the viewer to be able to switch between two different sizes of the same image with his/her mouse. Thanks in advance for...
7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
5
by: Jerry | last post by:
Hello! I have an image that uses an image map for navigation. We would like a pull-down menu to appear when the user mouses over the link. The pull-down menu is another image that was created to...
4
by: lindsey.crocker | last post by:
I have this links list with background images set on them which changes when they roll over. The <td> is set valign="middle" however as soon as you apply the rollover to the link, the text jumps...
7
by: simchajoy2000 | last post by:
Hi, I am just a javascript beginner so maybe this is a simple problem but I am trying to do some rollovers on images in a separate <div>. Here is the relevent piece of my code: <html>...
4
by: Tim Streater | last post by:
I have a small image on my web page that, when clicked, does some javascript stuff which changes the page. To highlight that this has happened, I want to replace the image with another. Each is a...
7
by: simon2x1 | last post by:
I create a css that will make the text on my html page change colour when the mouse is on it but on the page when i roll over the mouse to an image that i link it also change colour how can i make...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.