我需要检测拖动网站滚动条时的鼠标移动,这使我能够检测用户的不活动情况。
拖动滚动条时,不会触发鼠标移动事件。
无法在 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 fiddle : https://jsfiddle.net/btdxha8k/
绑定(bind)到滚动事件是我目前拥有的解决方案,但我想知道是否有一个更干净的解决方案,因为我需要绑定(bind)到 100 多个不需要滚动事件的 div,因为这看起来真的多余、肮脏,而且我通常不喜欢在我的代码中使用这样的 hack。
干杯;)
最佳答案
仅当您在屏幕内移动鼠标时才会触发鼠标移动事件。 您此处的问题滚动条不是捕获鼠标移动事件的屏幕内侧,因此您应该添加具有相同功能的滚动事件
关于javascript - 用鼠标拖动滚动条时不触发Mousemove事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250618/