2010-11-18 66 views
7

我有下面的代码,当另一个div被蒙上阴影时,切换div的可见性。它在工作正常,但如果你的鼠标和反复发作它会将所有的切换的:jQuery切换鼠标悬停 - 阻止队列

$(document).ready(function() { 
    $('.trigger').mouseover(function(){ 
     $('.info').toggle(400); 
    }).mouseout(function(){ 
     $('.info').toggle(400); 
    }); 
}); 

我已经试过这一点,但它似乎没有工作(它创建与切换的可见性问题div和结果根本没有显示它)

$(document).ready(function() { 
    $('.trigger').mouseover(function(){ 
     $('.info').stop().toggle(400); 
    }).mouseout(function(){ 
     $('.info').stop().toggle(400); 
    }); 
}); 

我该如何摆脱队列?

+0

你真的应该缓存'.info'选择。 – Stephen 2010-11-18 16:02:15

+0

如果你打算缓存选择器Stephen,那么你可以缓存整行......不幸的是,这对丹的问题无济于事。有些人为他人简化他们的代码以回答有问题的问题。我也同意约翰丹应该接受成为参与者而不是用户 – Blowsie 2010-11-18 16:05:45

+0

您只接受7个问题中的1个,点击我的问题,然后接受您为每个问题选择的答案 – Blowsie 2010-11-18 16:17:33

回答

13

使用.dequeue()函数和.stop()

.dequeue().stop() 

在这这里找到优秀的文章,我确定它会告诉你,你想知道什么。

http://css-tricks.com/examples/jQueryStop/

而且我会用。 show() and .hide()而不是.toggle()只是为了节省jquery的任何困惑。

编辑:更新

问题是动画心不是整理,利用真,真启动另一跳到结束。

Example

$('.trigger').mouseover(function() { 
    $('.info').stop(true, true).show(400); 
}).mouseout(function() { 
    $('.info').stop(true, true).hide(400); 
}); 
+0

谢谢。代码现在是这样的: $(document).ready(function() { \t $('.trigger').mouseover(function(){ \t \t $('.info').dequeue().stop().show(400); \t }).mouseout(function(){ \t \t $('.info').dequeue().stop().hide(400); \t }); }); 但是,这只是作为它在原始帖子中的第二个片段中的行为一样。似乎仍然排队动画,但得到真正的困惑,并没有显示任何内容。 – Dan 2010-11-18 16:08:06

+0

这个更新对你有好处吗,丹? – Blowsie 2010-11-19 12:48:24

+0

感谢Blowsie它对我有帮助 – srini 2012-10-10 10:18:50

0

你应该试试这个

$(".trigger").hover(function() { $(".info").stop(true).toggle(400); });