jquery - 在mouseenter和mouseleave上切换脱节的div?

时间:2012-09-27 21:31:15

标签: jquery mouseenter mouseleave

我已经尝试过寻找类似的问题,并根据我的情况使用这些答案,但要么我做错了(很可能......),要么就是我不需要的?

基本上,当我将鼠标悬停在#b-hover-nav上时,我正试图切换.block-5。但是,我希望.block-5保持打开状态,以便用户可以阅读/与其中的链接进行交互...对于示例中的其他链接也是如此...

我已经发布了jsfiddle,(http://jsfiddle.net/9fcFv/),但我也将其包含在下面:

#content {
     width: 400px;
    height: 400px;
}
span.button-hover-nav {
 display: block;
 clear: both;  
  width: 200px;    
    margin-bottom: 20px;
}
.left {
      width: 200px;
      float: left;
}
.block-5 {
     display: none;
    width: 200px;
    float: right;
}
.block-5 a {
     color: blue;
}
.block-6 {
     display: none;
    width: 200px;
    float: right;
}
.block-6 a {
     color: green;
}

​
<div class="body">
 <span class="button-hover-nav" id="b-hover-nav">Hover Me</span>
</div>

<div class="block-5">
<h1>Please stay open unless I leave...</h1>
<a link="#">Click Me</a>
</div>

//Totally does not work:
// Bind the mouse over /out to the first DIV.
            $('#b-hover-nav').live('mouseenter', function() {
        $('.block-5').show();
    }).live('mouseleave', function() {
        t = setTimeOut(function() {
            $('.block-5').hide();
        }, 100);
    });

    $('.block-5').live('mouseenter', function() {
        if(t) {
            clearTimeout(t);
            t=null;
        }
    });

1 个答案:

答案 0 :(得分:1)

您遇到的问题是由于用户在按钮上的mouseout事件触发之前没有时间将鼠标移动到对象上。你需要给他们一点时间用鼠标到达那里。

另外,我更喜欢jQuery的内置悬停方法。但是如果你愿意,可以使用绑定。

JSFiddle

var timer1,timer2;
var delay=1000;
$("#b-hover-nav").hover(function() {
    clearTimeout(timer1);
    $('.block-6').hide();
    $('.block-5').show();
}, function() {
    timer1= setTimeout(function() {
        $('.block-5').hide();
    }, delay);
});

$("#c-hover-nav").hover(function() {
    clearTimeout(timer2);
    $('.block-5').hide();
    $('.block-6').show();
}, function() {
    timer2= setTimeout(function() {
        $('.block-6').hide();
    }, delay);
});

$(".block-6").hover(function() {
    clearTimeout(timer2);
}, function() {
    timer2= setTimeout(function() {
        $('.block-6').hide();
    }, delay);
});

$(".block-5").hover(function() {
    clearTimeout(timer1);
}, function() {
    timer1= setTimeout(function() {
        $('.block-5').hide();
    }, 2000);
});

相关问题