最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery hover problem when moving mouse too quickly between elements - Stack Overflow

programmeradmin0浏览0评论

I have the following html repeated many times on a page:

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

and have this 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: / moving the mouse slowly between the divs (waiting for the animation to plete) achieves the desired effect of displaying only one inner div at a time.

However if you move the mouse quickly between the divs, all the inner divs remain visible.

I've tried using the stop() function but without success. How can I prevent the inner divs from remaining open?

I have the following html repeated many times on a page:

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

and have this 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/342q3/15/ moving the mouse slowly between the divs (waiting for the animation to plete) achieves the desired effect of displaying only one inner div at a time.

However if you move the mouse quickly between the divs, all the inner divs remain visible.

I've tried using the stop() function but without success. How can I prevent the inner divs from remaining open?

Share Improve this question asked Sep 15, 2011 at 10:20 AndyAndy 7,8568 gold badges47 silver badges69 bronze badges 1
  • I think you will soon (adding more content into 'inner') have to pletely change your script to something more 'user friendly'. – Roko C. Buljan Commented Sep 15, 2011 at 10:28
Add a ment  | 

3 Answers 3

Reset to default 4

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:

$('.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/AVLdW/

try writing code with animation() that way you'll be able to stop() it anytime and set default styles.

Animate() is the function you want, I have written my navigation system using this functiong with this kind of syntax:

$("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.

发布评论

评论列表(0)

  1. 暂无评论