admin管理员组文章数量:1389859
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.
Please help?
<script type="text/javascript">
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);
}
How can I capture mouse move event outside of the viewport width and height?
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.
Please help?
<script type="text/javascript">
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);
}
How can I capture mouse move event outside of the viewport width and height?
Share Improve this question edited Oct 2, 2022 at 22:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 16, 2013 at 4:28 pavanpavan 3141 gold badge8 silver badges21 bronze badges 3- Did you try anything yet? – Dhaval Marthak Commented Apr 16, 2013 at 4:34
- maybe this will help stackoverflow./questions/10325132/… – maxlego Commented Apr 16, 2013 at 5:52
- @maxlego i tried the above link but i switching to other window it is not losting the focus – pavan Commented Apr 16, 2013 at 6:22
2 Answers
Reset to default 6jQuery(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 .
and also you may use :
jQuery(document).mouseenter(function(){console.log('in')});
to trigger when the mouse enters the page to start your counter again.
Below code is works for me,
const body = document.getElementsByTagName('body')[0];
var intervalExec;
body.addEventListener('mouseenter', function() { //mouseover, mousedown, mousemove
// Write your setInterval execution
intervalExec = setInterval(updateCounter, 1000); // updateCounter is your executable function
});
body.addEventListener('mouseleave', function() { //mouseout, mouseup
clearInterval(intervalExec);
});
本文标签: javascriptCapture event when the mouse pointer outside of the browser viewportStack Overflow
版权声明:本文标题:javascript - Capture event when the mouse pointer outside of the browser viewport - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664759a2618459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论