Jquery hover effect disappears
I am trying to achieve an effect where when you hover over an image, it zooms in, darkens, and another image slides down. I have achieved this effect successfully, but there is a small problem. Here is my code:
HTML
<div class="viewPort">
<a href="#"><img src="<?php echo bloginfo('template_url'); ?>/images/penguin.jpg" alt="penguin">
<span class="dark"></span><span 开发者_JAVA百科class="slide"></span></a>
</div>
CSS
.viewPort{float:left; position:relative; border:8px solid #60B1D0; height:300px; width:300px; overflow:hidden;}
.viewPort a{display:block;}
.viewPort img{ position:relative; top:-20px; left:-20px; height:330px;}
.viewPort span.dark{background:url(images/trans.png) repeat; width:100%; height:100%; position:absolute; top:0; left:0; z-index:10; display:none;}
.viewPort span.slide{top:-130px; left:84px; position:absolute; z-index:100; width:130px; height:130px; background:url(images/badge.png) no-repeat;}
jQuery
$(".viewPort a").hover(function(){
$(this).find('img').stop().animate({
height: '300', left: '0', top: '0', width: '300'
}, 250);
$('span.dark').fadeIn(600);
$('span.slide').stop().animate({
top: '80'
}, 600);
}, function(){
$('span.slide').stop().animate({
top: '-130'
}, 400);
$('span.dark').stop().fadeOut(400);
$('.viewPort img').stop().animate({
height: '330', left: '-20', top: '-20', width: '330'
}, 250);
});
This works great; however, if you hover back and forth over the image a few times, the <span class="dark"></span>
doesn't seem to be affected. I looked at my firebug console, and the opacity is changing on hover; however, visually, you do not see it darkening. Any ideas why this is happening? It works great on normal hovers, but if you play with it for a little bit, the darkening effect disappears.
http://jsfiddle.net/7zQFf/
Try changing the jQuery to this:
$(".viewPort a").hover(function(){
$(this).find('img').stop().animate({
height: '300', left: '0', top: '0', width: '300'
}, 250);
$('span.dark').stop().fadeTo(600, 1); // .stop().fadeTo()
$('span.slide').stop().animate({
top: '80'
}, 600);
}, function(){
$('span.slide').stop().animate({
top: '-130'
}, 400);
$('span.dark').stop().fadeTo(400, 0); // .stop().fadeTo()
$('.viewPort img').stop().animate({
height: '330', left: '-20', top: '-20', width: '330'
}, 250);
});
Worked for me on the JSFiddle: http://jsfiddle.net/bFUSX/ (FF4)
You should pass the jumpToEnd
argument as true (so the animation will be completed quickly) for this to work while calling stop on the dark span.
Here is the demo:
http://jsfiddle.net/7zQFf/4/
精彩评论