Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

[web] How to make the images grow when you hover over them

This topic is 3256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
The smooth action would not be possible in straight CSS, so something like MooTools would be required. An example that uses the Fx.Tween class may look like this:

<img class="hover-grow" src="/images/image1.jpg" />
<img class="hover-grow" src="/images/image2.jpg" />

<script type="text/javascript">
window.addEvent('domready', function() {
$$('img.hover-grow').each(function(img) {
var tweener = new Fx.Tween(img, { property : 'width', link : 'cancel' });
img.addEvent('mouseenter', function() { tweener.start(64); });
img.addEvent('mouseleave', function() { tweener.start(32); });
});
});
</script>

When the mouse entered each image its width would be tweened (according to the settings in the Fx class options) to 64 pixels wide (the height will automatically follow) and when the mouse leaves each image its width is tweened back to 32 pixels wide.

Share this post


Link to post
Share on other sites
This works too.
I'm sure you can get the same thing to work with CSS instead of JS, but I'm not as good with CSS.
Basically, when the image gets an onmouseover event, it calls the GrowImage event which keeps getting called until the size reaches 64. Make sure that the image itself has the resolution of the maximum image size so that it doesn't look zoomed in on.


<html>
<head></head>
<body>

<img onmouseover="GrowImage(this, 32);" src="deathrm.jpg" />

<script type="text/javascript">
function GrowImage(img, size)
{
img.width = size;
img.height = size;
size += 2;

if(size<=64)
{
setTimeout(function() { GrowImage(img, size); }, 10);
}
}

</script>
</body>
</html>


Another option you have would be to create a non-repeating animated GIF. When the image gets "onmouseover", just switch out the image's src with the animated GIF's src.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!