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

Started by
1 comment, last by Phytoplankton 14 years, 8 months ago
An example of this. Check out the News, Videos, Images icons I was wondering if this is image swapping or strictly CSS and/or JS code. How do you do this? If someone has code to do this, please share.

Beginner in Game Development?  Read here. And read here.

 

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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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/&#106avascript">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.
-----OpenEndedAdventure.com - The Adventure that Anyone Can Edit.

This topic is closed to new replies.

Advertisement