如果这是任何用户活动(例如移动或滚动鼠标或按键),如何执行功能?我想的是类似于以下内容,但是,可能还需要其他事件。理想情况下,不需要jQuery。
document.body.addEventListener('mouseover', resetTimer);
window.addEventListener('scroll', resetTimer);
document.addEventListener('keydown', resetTimer);
目的是实现基于JavaScript的页面旋转器,该旋转器将在用户不活动X秒后启动。
答案 0 :(得分:1)
有几个你可以听的事件,我相信这些应该涵盖最多:
mousedown
捕获点击事件mousemove
捕捉光标移动touchstart
捕获水龙头(在触摸屏上)keydown
捕获关键事件scroll
捕捉滚动请参阅下面的演示代码。
document.addEventListener('mousedown', viewEvent);
document.addEventListener('mousemove', viewEvent);
document.addEventListener('touchstart', viewEvent);
document.addEventListener('scroll', viewEvent);
document.addEventListener('keydown', viewEvent);
function viewEvent(evt){
document.getElementById('viewEvent').innerHTML = evt.type;
}
#viewEvent{
position: fixed;
padding: .2em;
background: #ccc;
}
<div id="viewEvent"></div>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>
dummy text<br>