Jquery悬停效果消失
我试图实现一种效果,当您将鼠标悬停在图像上时,它会放大、变暗,而另一张图像会向下滑动。我已经成功实现了这个效果,但是有一个小问题。这是我的代码:
HTML
<div class="viewPort">
<a href="#"><img src="<?php echo bloginfo('template_url'); ?>/images/penguin.jpg" alt="penguin">
<span class="dark"></span><span 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);
});
这很好用;但是,如果您在图像上来回悬停几次, 似乎不会受到影响。我看着我的萤火虫控制台,悬停时不透明度正在变化;然而,从视觉上看,你并没有看到它变暗。有什么想法为什么会发生这种情况吗?它在正常悬停时效果很好,但如果你稍微使用它,变暗效果就会消失。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 jQuery 更改为:
在 JSFiddle 上为我工作: http://jsfiddle.net/bFUSX/ (FF4)
Try changing the jQuery to this:
Worked for me on the JSFiddle: http://jsfiddle.net/bFUSX/ (FF4)
您应该将
jumpToEnd
参数传递为 true(这样动画就会很快完成),以便在黑暗跨度上调用 stop 时起作用。这是演示:
http://jsfiddle.net/7zQFf/4/
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/