我需要在网站的阻力上检测鼠标移动,这使我能够检测到用户不活动。
当滚动栏被拖动时,鼠标移动事件不会发射。
不在IE11和Chrome中工作,我看到Mousemove事件在Firefox 32中射击,我没有测试过其他浏览器。
示例代码:
html
<div class="parent" style="background-color:black;width:100px;height:500px;overflow:scroll;">
<div class="child" style="background-color:blue;width:100px;height:1000px"></div>
</div>
javaScript:
var lastMove;
$(window).mousemove(function (e) {
lastMove = new Date();
$(".child").css("background-color", "red");
lastTimeMouseMoved = new Date().getTime();
var t=setTimeout(function(){
var currentTime = new Date().getTime();
if(currentTime - lastTimeMouseMoved > 10){
$(".child").css("background-color", "blue");
}
},10);
});
JS小提琴:https://jsfiddle.net/btdxha8k/
绑定到滚动事件是我目前拥有的解决方案,但我想知道是否有一个更干净的解决方案,因为我需要绑定到100 div,而不需要滚动事件,因为这看起来真的很多余,很脏,我通常通常不喜欢在我的代码中使用这样的黑客。
欢呼;)
鼠标移动事件仅在您在屏幕中移动鼠标时才会触发。您在这里的问题scrollbar不是捕获鼠标移动事件的屏幕内侧,因此您可以使用相同功能添加滚动事件
我猜想没有增加鼠标事件,因为从技术上讲卷轴在您的页面外。
相反,您可以听onscroll
事件吗?
https://api.jquery.com/scroll/