这是我关于stackoverflow的第一个问题,希望有人能帮助回答我的问题。
这是我的问题:当检测到移动/运动/Activity 时(即通过使用 jQuery/MooTools 的 Javascript),我如何显示或隐藏 div,或调用任何操作。
我知道这是可能的,因为 gmail 会利用它,例如,当您长时间处于非 Activity 状态时,您的状态将显示为非 Activity 状态。当您将鼠标移到 gmail 页面的主体上时,状态将从非 Activity 更改为在线。
希望有人能识别出满足我需求的脚本。
提前致谢,
里奥·贝鲁特
将监听器附加到 document.body.onmousemove
。我正在使用类似的东西并取得了巨大的成功。
document.body.onmousemove = function () {
console.log("hey, you moved!");
}
当然,如果你继续移动,它会一直开火,所以也许你想要一个宽限期?
(function () {
var in_grace = false; timout_set = false, grace = 5 * 1000; // 5 seconds
document.onmousemove = function () {
if (false === in_grace) {
in_grace = true;
timout_set = false;
console.log("hey, you moved!"); // <-- your code here
} else if (false === timout_set) {
timout_set = true;
setTimeout(function () {
in_grace = false;
}, grace); // set in_grace to false after grace seconds
}
}
}());
这应该最多每 5 秒触发一次事件。