简体   繁体   English

在元素之间移动鼠标太快时jQuery悬停问题

[英]jQuery hover problem when moving mouse too quickly between elements

I have the following html repeated many times on a page: 我在页面上多次重复以下html:

<div class="outer">
    outer
    <div class="inner">
        inner
   </div>
</div>

and have this jQuery: 并有这个jQuery:

$('.inner').hide();
$('.outer').hover(function(e) {
    $(this).children('.inner').show("slide", {
        direction: "right"
    }, 1000);
}, function(e) {
    $(this).children('.inner').hide("slide", {
        direction: "right"
    }, 1000);
});

As you can see here: http://jsfiddle.net/342q3/15/ moving the mouse slowly between the divs (waiting for the animation to complete) achieves the desired effect of displaying only one inner div at a time. 正如你在这里看到的: http//jsfiddle.net/342q3/15/在div之间缓慢移动鼠标(等待动画完成)实现了一次只显示一个内部div所需的效果。

However if you move the mouse quickly between the divs, all the inner divs remain visible. 但是,如果在div之间快速移动鼠标,则所有内部div仍然可见。

I've tried using the stop() function but without success. 我尝试过使用stop()函数但没有成功。 How can I prevent the inner divs from remaining open? 如何防止内部div保持打开状态?

If you stop the animation before you start the new one (sliding out) and use find instead of children (no idea why it doesn't work with children ), it works as supposed: 如果你在开始新动画之前停止动画(向外滑动)并使用find代替children (不知道为什么它不能与children ),它会按照假设:

$('.inner').hide();
$('.outer').hover(function(e) {
    $(this).find('.inner').stop(true, true).show("slide", {
        direction: "right"
    }, 1000);
}, function(e) {
    $(this).find('.inner').stop(true, true).hide("slide", {
        direction: "right"
    }, 1000);
});

http://jsfiddle.net/AVLdW/ http://jsfiddle.net/AVLdW/

尝试使用animation()编写代码,这样你就可以随时stop()并设置默认样式。

Animate() is the function you want, I have written my navigation system using this functiong with this kind of syntax: Animate()是你想要的功能,我用这种函数用这种语法编写了我的导航系统:

$("your-selecter").hover(function() {   

    $(this).stop().animate({
        backgroundPosition: '0 0'
    }, 600);
    }, function() {
       $(this).stop().animate({
            backgroundPosition: '0 -100px'
       }, 600); 
    });
};

Obviously you'd not want to change the BG position in yours, you'd call you slide function in there, but it's this kind of concept. 显然你不想改变你的BG位置,你会在那里称你为滑动功能,但这就是这种概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM