1

当鼠标悬停时,我正在尝试向 div 添加叠加层。虽然它有效,但当我在 div 内移动鼠标时,覆盖层会一直闪烁。我怎样才能防止这种情况?

HTML:

<div id="d4" class="dmc_block" style="width:60%; background:#eee; z-index:1">
    <div style="padding:20px;">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. </p>
    </div>
</div>

JS:

$('body').on('mouseenter','.dmc_block',function(){
    var o = $(this).offset();
    var h = $(this).height();
    var w = $(this).width();
    $('<div>').addClass('hover').css({width:w,height:h,left:o.left,top:o.top,position:'absolute',zIndex:2}).insertAfter(this);
}).on('mouseleave','.dmc_block',function(){
    $('.hover').remove();
});    
$('body').on('mouseenter','.hover',function(){return false;});

小提琴:http: //jsfiddle.net/T9Lhw/

4

1 回答 1

4

改变这个:

.on('mouseleave','.dmc_block',function(){

对此:

.on('mouseleave','.hover',function(){

小提琴

当叠加层.hover放置在上方.dmc_block时,它会触发mouseleaveof,.dmc_block因为它现在位于 下方.hover。你想要的mouseleave.hover.

于 2012-10-15T18:53:36.107 回答