防止循环 jQuery
我创建了这个使用 jQuery 向上滚动的脚本,但是当您多次将鼠标悬停和鼠标移出时,该脚本将进入循环,直到每次将鼠标悬停在其上时都完成。
我已经尝试寻找解决方案,但没有解决,代码如下:
$(document).ready(function(){
$('.desc').hide();
$('.work').mouseover(function(){
$('.desc', this).slideDown('900');
});
$('.work').mouseleave(function(){
$('.desc', this).slideUp('900');
});
});
I created this script for scrolling up with jQuery, but when you mouseover and mouseout multiple times, the script goes into the loop until it finishes for every time you mouse over it.
I already tried to search for a solution but I didn't sorted it out, here's the code:
$(document).ready(function(){
$('.desc').hide();
$('.work').mouseover(function(){
$('.desc', this).slideDown('900');
});
$('.work').mouseleave(function(){
$('.desc', this).slideUp('900');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在每个动画之前添加
stop()
,这会清除动画队列。 http://api.jquery.com/stop/Add
stop()
before each animation, which clears the animation queue. http://api.jquery.com/stop/.stop()
在纸面上会有所帮助,但动画看起来仍然不稳定。您真正寻找的是延迟悬停事件,以在用户实际“悬停”在您的.work
元素上时触发动画。有一些 jQuery 插件可以实现这一点。尝试 hoverIntent - 示例。或者您可以自己编写,这也不是很难。另外,FYI 900 几乎是一整秒——这比礼貌的动画应该长得多。
The
.stop()
will help on paper, but animation will still look jerky. What you really looking for is delayed hover event to trigger animation when user actually "hovering" over your.work
element. There are a few jQuery plugins for that. Try out hoverIntent - sample. Or you could write your own, which isn't terribly hard either.Also FYI 900 is almost a full second - that is way longer than a polite animation should be.
尝试使用
.stop()
,注意将 true 作为两个参数传递,它清除队列(阅读文档以获取更多信息)并跳转到末尾。如果您不将 true 作为参数传递,动画将排队并执行,因此您将面临与目前相同的行为try using
.stop()
, notice passing true as both the args, its clears the queue(read the docs for more information) and jumps to the end. if you dont pass true as the args the animations will be queued and executed so you will face the same behavior as you are facing at the moment您需要在每个动画(slideUp和slideDown)之前添加
stop()
。这个小函数会在将新动画添加到动画队列之前停止动画!You need to add
stop()
before each animation (slideUp and slideDown). This little function stops the animation before adding a new one to the animation queue!