gpt4 book ai didi

javascript - 鼠标拖动滚动条不触发mousemove事件

转载 作者:行者123 更新时间:2023-11-30 12:23:33 25 4
gpt4 key购买 nike

我需要在拖动我的网站滚动条时检测鼠标移动,这允许我检测用户不活动。

拖动滚动条时,不会触发鼠标移动事件。

在 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 fiddle : https://jsfiddle.net/btdxha8k/

绑定(bind)到滚动事件是我目前的解决方案,但我想知道是否有更干净的解决方案,因为我需要绑定(bind)到 100 多个不需要滚动事件的 div,因为这看起来真的多余、肮脏,而且我通常不喜欢在我的代码中使用这样的技巧。

干杯;)

最佳答案

只有在屏幕内移动鼠标时才会触发鼠标移动事件。你这里的滚动条问题不是屏幕内侧捕获鼠标移动事件的地方,所以你应该添加具有相同功能的滚动事件

关于javascript - 鼠标拖动滚动条不触发mousemove事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com