我需要检测拖动我网站滚动条的鼠标移动,这样我就可以检测到用户不活动。
拖动滚动条时,鼠标移动事件未触发。
在IE11和Chrome中无法使用,我在Firefox 32中看到了mousemove事件,我还没有测试过其他浏览器。
示例代码:
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,因为这看起来真的多余,很脏,我通常不喜欢在我的代码中使用这样的黑客。
干杯;)
答案 0 :(得分:2)
只有在屏幕内移动鼠标时才会触发鼠标移动事件。 你在这里的问题滚动条不是屏幕的内侧,捕获鼠标移动事件所以你应该添加具有相同功能的滚动事件
答案 1 :(得分:1)