【问题标题】:hiding a moving div when mouse if offscreen ( want not just to track when the mouse is offscreen) [duplicate]如果鼠标在屏幕外,则隐藏移动的 div(不仅要跟踪鼠标何时离开屏幕)[重复]
【发布时间】:2017-02-28 08:38:14
【问题描述】:

这是我在 js 中的代码:

        window.onload = function(){
            var bsDiv = document.getElementById("box");
            var x, y;
// On mousemove use event.clientX and event.clientY to set the location of the div to the location of the cursor:
            window.addEventListener('mousemove', function(event){
                x = event.clientX;
                y = event.clientY;                    
                if ( typeof x !== 'undefined' ){
                    bsDiv.style.left = x + "px";
                    bsDiv.style.top = y + "px";
                }
            }, false);
        }
    </script>

我想添加一个函数,如果光标不在屏幕上(或位置为空?)它使元素不可见,然后在光标重新输入时使其重新可见

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

document.onmouseout在鼠标离开屏幕时触发。

所以做类似的事情

function invisible(div){
    div.style.visibility = 'hidden';
}
document.addEventListener(onmouseout, invisible(divName), true);

【讨论】:

  • 你试过了吗?这是行不通的。它也会冒泡,所以所有mouseout 都会让它认为它在窗外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2016-12-17
  • 2013-04-22
相关资源
最近更新 更多