jQuery image slide on hover effect
This is a simple technique to animate an image when hovering using jQuery’s animate() effect. We will use this effect to manipulate our CSS, creating a seamless transition between two areas of an image. Have a look at the demo below for a better understanding.
Update – 11/14
A plugin has been created for this effect. Please see the post jQuery image slide on hover effect plugin.
Xhtml
<div id="container"> <h1>jQuery image slide on hover effect</h1> <div><a href="#"><img src="image-1.jpg" alt="" /></a></div> <div><a href="#"><img src="image-2.png" alt="" /></a></div> <div><a href="#"><img src="image-3.jpg" alt="" /></a></div> </div>
The markup itself is quite basic. We have three divs that contain image links. Notice that each image contains both its default and hover states. This is done to create our animation.
CSS
/* simple reset */
html,body,div,h2,img {margin:0;padding:0;border:none;}
html {
font:1em Arial, Helvetica, sans-serif;
color:#444;
}
h1 { text-align:center;}
#container {
margin:100px auto;
width:909px;
}
#container div {
margin-right:3px;
float:left;
width:296px;
height:130px;
border:1px solid #999;
position:relative;
overflow:hidden;
}
#container img {
position:absolute;
}
Since we only want to show the image’s default state first, we set the height and width of the containing div, and set overflow to “hidden”. We will be absolutely positioning the image, so we also need to set the divs position to “relative” and the images to “absolute”>.
jQuery
$(function(){
$("#container div a").hover(function(){
$("img", this).stop().animate({top:"-130px"},{queue:false,duration:200});
}, function() {
$("img", this).stop().animate({top:"0px"},{queue:false,duration:200});
});
});
To get things rolling, we first run a hover function on our link tag. Before running our animation, we use the stop() method to cancel the animation queue if one exists. The animate effect uses the CSS Top property to reveal the image’s hidden hover state, sliding it upwards over a period of 200 milliseconds. We also set the queue to “false” so that the animation will start automatically without having to wait for its turn.
And there you have it… an easy way to create a hover animation using jQuery.






Hi
I would like to use this jquery. But where are the hover images? And how are they called?
I see here: image-1.jpg
where is the hover image for it and how should I call it??
The default and hover states are actually one image. Click here to see an example.
Since we only want to display the default portion of the image first, we need to apply some CSS to the parent div to hide the hover state. We do this with the following code:
#container div {margin-right:3px;
float:left;
width:296px;
height:130px;
border:1px solid #999;
position:relative;
overflow:hidden;
}
Notice that the width is identical to that of the image, but the height is only half that. ‘Overflow:hidden’ is then used to hide the rest of it. When the user hovers over the image, jQuery is used to slide it upwards revealing the hover state.
Is it possible to change the css so that it slides left/right?
It’s not really a questions of changing the CSS but actually jQuery. I’ve update the demo page to show you how to create the same effect horizontally, from left to right.
Thanks a lot – I wanted a slide-out effect for navigation tabs. This is perfect with a transparent png!
Certainly! I’d be interested in seeing how you incorporate a transparent png. Feel free to share a link when it’s up and running.
Hey.. thanks for posting this. i am AMAZED at how easy it was to get going..
I have a quick question though.. what if you have 3 different containers that I all want to animate vertical.. How would I add the other div names to the javascript.
I tried just repeating this code with the new div name, but it didn’t seem to like it repeated. I am new with javacrip, so sorry if this is a stupid question:
$(function(){
// Vertical
$(“#vertical div a”).hover(function(){
$(“img”, this).stop().animate({top:”-130px”},{queue:false,duration:400});
}, function() {
$(“img”, this).stop().animate({top:”0px”},{queue:false,duration:200});
});
$(“#verticalCorp div a”).hover(function(){
$(“img”, this).stop().animate({top:”-130px”},{queue:false,duration:400});
}, function() {
$(“img”, this).stop().animate({top:”0px”},{queue:false,duration:200});
});
});
wow.. I am REALLY having some issues with getting this to work for me. The issue really is that I will have 3 DIFFERENT containing divs in 3 different locations on the website.. So i can’t really place them separately if I have no idea how to call different named divs within the javascript
I just created a plugin which should address your issue. Please see this post – http://spoonfedproject.com/jquery/jquery-image-slide-on-hover-effect-plugin/
THANKS A BUNCH!! You need to add a blog plugin that lets commentors get notifications of follow up comments on posts.. i almost completely forgot about this!
i used ur code in my site..but in ie7 and ie8 dont show the mouse cursor when out of images…its only visible hover on a linked image or link..
can you help me..
thanks
By default, images don’t receive the same type of cursor as links do. Thanks to CSS, we can change the mouse cursor by using the cursor property. Try something like this:
.pointer {cursor:pointer}
<img class="pointer" src="path-to-image.jpg" alt="" />
Thx for this tutorial, i will use it for one of our customers.
Greetings
how can I call the effect from external link? currently it only works when you hover your mouse on the image. I want it to be work when you mouseover or click on link outside of the container.
hi there, this plugin rock but i having problem with my image… do you mind to have a look what’s going wrong at my page?
http://henshin.my/thunder/?page=products
my image align was not right but after the hover effect the image went back to normal
mind to have a look? if you need source code please let me know i will post it at here.
P/S: im a beginner coder sorry if i anything go wrong.
Adding “left:0″ to “#vertical img” and “#horizontal img” should fix it.
#vertical img, #horizontal img {
position:absolute;
left:0;
}
thanks admin~ u save my life xD
Hi there. My images don’t move when you roll over them. What am I doing wrong?
http://rctgeneration.comuf.com
Hey Jordan,
Just checked the link you’ve provided and it appears to be working correctly. Let me know if this is not the case.
Yeah, i had all the links messed up. I got it working about an hour after posting
Blaine,
Been looking for this effect for a long time. Thanks a lot!
I have a question:
How to show several sliding pics on hover?
For 4 frames my 200×200 DIV would have a 800×200 image inside it.
On hover each ‘frame’ would slide over every 1 second or so, and stop at the last frame.
On mouseout it slides all the way back to the start.
Sort of like an automatic slider activating on hover, and resetting on mouseout.
You should be able to chain multiple animations together. If you would like a pause before each animation, try using jQuery’s delay() method. I believe it was introduced in jQuery 1.4, so make sure to use at least that version.