Sign in to follow this  
Alpha_ProgDes

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

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this