当鼠标指针位于浏览器视口外时捕获事件 [英] capture event when the mouse pointer outside of the browser viewport
本文介绍了当鼠标指针位于浏览器视口外时捕获事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在javascript中写一个简单的无限计数器,当页面加载它开始计数
i am writing a simple infinite counter in javascript when the page loads it starts counting
我想在鼠标指针位于视口之外时停止计数器
i would like to stop the counter when the mouse pointer is outside of the viewport
请帮忙?
var i=0;
setInterval(function (){
i++;
document.getElementById("counterLoop").innerHTML=i;
},1000);
var viewportWidth = document.documentElement.clientWidth;
var viewportHeight = document.documentElement.clientHeight;
function getCursorXY(e) {
CurX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
CurY = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
如何捕获视口宽度的鼠标移动事件和heigth
how can i capture mouse move event ouside of the viewport width and heigth
推荐答案
jQuery(document).mouseleave(function(){console.log('out')})
这将在您的页面中没有鼠标时触发想。只需将功能更改为您想要的功能。
this will trigger when ever the mouse is not in your page as you want. just change the function to do what every you want .
您也可以使用:
jQuery(document).mouseenter(function(){console.log('in')});
当鼠标进入页面再次启动你的计数器时触发。
to trigger when the mouse enters the page to start your counter again.
这篇关于当鼠标指针位于浏览器视口外时捕获事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文