在我们的页面中经常会用到mouseover与mouseout事件来给页面添加更好的渲染效果,但如果触发mouseover事件的元素有子元素的话,会造成闪烁的效果,看着很不舒服,这是因为mouseover与mouseout不论鼠标指针穿过被选元素或其子元素,都会触发。而mouseenter与mouseleave只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。
1 <ul class="con-ul"> 2 <li> 3 <div class="con-one"> 4 <div class="con-oneimg"> 5 <img src="http://image123465.cn"> 6 <div class="dask" style="display: block; opacity: 0;"></div> 7 <div class="input" style="display: block; opacity: 0;"> 8 <input type="button" class="inp inp-one" value="预览"> 9 <input type="button" class="inp inp-two" value="使用"> 10 </div> 11 </div> 12 <hr style="border-top:1px solid #b5b5b5;"> 13 <p>study</p> 14 </div> 15 </li> 16 </ul>
1 //1 2 $(".con-ul").on({ 3 mouseenter: function() { 4 $(this).find(".dask").stop(true,true).fadeIn(600); 5 $(this).find(".input").stop(true,true).fadeIn(600); 6 }, 7 mouseleave: function() { 8 $(this).find(".dask").stop(true,true).fadeOut(300); 9 $(this).find(".input").stop(true,true).fadeOut(300); 10 } 11 }, ".con-oneimg"); 12 13 //2 14 $(".con-ul").on({ 15 mouseover: function() { 16 $(this).find(".dask").stop(true,true).fadeIn(600); 17 $(this).find(".input").stop(true,true).fadeIn(600); 18 }, 19 mouseout: function() { 20 $(this).find(".dask").stop(true,true).fadeOut(300); 21 $(this).find(".input").stop(true,true).fadeOut(300); 22 } 23 }, ".con-oneimg");