2016-12-28 148 views
-1

当我悬停#triangle时,#tri-overlay会显示,但会持续闪烁,因为它与css中的“display:none”有冲突。他一直从“display:none”转到“display:block”,导致闪烁效果。我怎样才能避免这种冲突?jQuery .show()/ .hide()保持闪烁

我使用的是下面的代码。

$(function() { 
 
    $('#triangle').hover(function() { 
 
    $('#tri-overlay').show(); 
 
    }, function() { 
 
    $('#tri-overlay').hide(); 
 
    }); 
 
});
.slide-des { 
 
    width: 50%; 
 
    height: 50%; 
 
} 
 
#triangle { 
 
    width: 12em; 
 
    height: 10em; 
 
    position: relative; 
 
    background: red; 
 
    display: block; 
 
} 
 
#tri-overlay { 
 
    background: green; 
 
    width: 100%; 
 
    height: 100%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<div id="tri-overlay" style="display: none;"></div> 
 
<div class="slide-des"> 
 
    <div id="triangle"></div> 
 
</div>

+2

,你可以做一个[MCVE]使用堆栈段? –

+2

我假定'#tri-overlay'覆盖了##三角形,因此否定了悬停事件,导致它再一次被隐藏,这使得悬停事件再次发生。请发布您的HTML,以便我们可以看到发生了什么。 – Archer

+1

片段是有用的。 –

回答

0

停止覆盖与CSS鼠标交互:

#tri-overlay{ 
    pointer-events: none; 
} 

当一个元素鼠标下出现,一些浏览器会立即说刚离开一个元素在鼠标下。

参考:https://developer.mozilla.org/en/docs/Web/CSS/pointer-events

借用射手布局示例:

$("#element2").hover(function() { 
 
    $("#element2-overlay").show(); 
 
    }, 
 
    function() { 
 
    $("#element2-overlay").hide(); 
 
});
#element2 { 
 
    background-color: green; 
 
} 
 
#element2-overlay { 
 
    pointer-events: none; 
 
    background-color: yellow; 
 
    display: none; 
 
} 
 

 
.example { 
 
    height: 100px; 
 
    position: absolute; 
 
    top: 0; 
 
    width: 100px; 
 
} 
 
.example2 { 
 
    left: 20px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="element2" class="example example2"></div> 
 
<div id="element2-overlay" class="example example2"></div>

+0

Doens't工作不幸 –

+0

“不工作”有点含糊。在哪种情况下不起作用? –

+0

它仍然保持闪烁,就像没有指针事件一样。仍然是显示无与块之间的冲突。当我检查元素时,你会发现“display”正在从“none”切换到“block”,并且返回的速度非常快 –