js:为什么mouseenter事件会不停的触发?

1、项目中遇到的需求:鼠标进入某个元素,有个小简介,所以在鼠标停留位置需要显示一个弹出框。我绑定的是mouseenter 和 mouseleave事件,鼠标进入元素显示弹出框,离开元素隐藏弹出框。但是鼠标进入元素,在元素里面滑动的时候,弹出框会不停的闪现,特别是鼠标从元素左上角往右下角方向滑动时,弹出框闪现的更厉害。
2、自己上网搜的时候,很多都是说mouseenter和mouseover、mouseleave和mouseout的区别,我懂它们之间的区别,我觉得此bug应该与这个无关吧?
3、上代码,求大神给与解答疑惑,谢谢了~~~

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>悬停弹出框</title>
    <style>
        #contain{position: relative;}
        #d1,#d2{
            width:50px;
            height:50px;
            background:pink;
            position:absolute;
            text-align:center;
            line-height:50px;
            top:200px;
            left:400px;
        }
        #d2{background:lightblue;left:700px;}
        #popupBox{
            width:200px;
            height:200px;
            border:1px solid #000;
            background:#ddd;
            position:absolute;
            display:none;
        }
    </style>
</head>
<body>
    <div id="contain">
        <div id="d1">1</div>
        <div id="d2">2</div>
    </div>
    <!-- 弹出框 -->
    <div id="popupBox"></div>    
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        //获取鼠标位置
        function mouseCoords(e){
            e= e || window.event; 
            if(e.pageX || e.pageY){ 
                return {x:e.pageX, y:e.pageY}; 
            } else{
                return {
                    x:e.clientX + document.body.scrollLeft - document.body.clientLeft, 
                    y:e.clientY + document.body.scrollTop - document.body.clientTop 
                }
            }
        }
        // 绑定事件
        $("#contain").on("mouseenter","div",function(e){
            $("#popupBox").show();
            var mousePos = mouseCoords(e); 
            $("#popupBox").css({
                "top":mousePos.y,
                "left":mousePos.x
            })
        }).on("mouseleave","div",function(){
            $("#popupBox").hide();
        });
    </script>
</body>
</html>
阅读 11.5k
5 个回答
   var ismouseenter = false // 初态未移入鼠标
   $("#contain").on("mouseenter","div",function(e){
            if(ismouseenter == true){ //已经移入直接返回
                return;
            } else {
                $("#popupBox").show();
                var mousePos = mouseCoords(e); 
                $("#popupBox").css({
                    "top":mousePos.y,
                    "left":mousePos.x
                })
                ismouserenter = true; // 状态设为移入
            }
        }).on("mouseleave","div",function(){
            if(ismouseenter == false){
                return;
            } else {
            $("#popupBox").hide();
            ismouserenter = false;
            }
        });
#popupBox{
    width:200px;
    height:200px;
    border:1px solid #000;
    background:#ddd;
    position:absolute;
    display:none;
    pointer-events: none;
}

popupBox加上一个pointer-events: none样式,这样他就不会响应鼠标事件了

因为移动的时候鼠标进入tooltip了,触发了mouseleave事件,然后tip消失又触发mouseenter,所以会闪烁

加个锁,移入的时候确保不会二次弹出

把onmouseenter换成onmouseover

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题