我们可以监听和处理单个浏览器事件,但是我们如何按顺序监听事件组合?
在伪代码中,我们正在寻找这样的东西:
element.on('click followed by mouse move', function() {
// do something
})
AFAIK,我们一次只能捕获一个事件。有任何想法吗?
使用私有范围和简单的切换变量有一种简单的方法:
(function(){
// Nothing else can change this, because of it's private scope.
var clicked = false;
document.getElementById('test').addEventListener('click', function(){
clicked = clicked ? false : true;
});
document.getElementById('test').addEventListener('mousemove', function(){
if (clicked) {
console.log('moving while clicked.');
}
});
})();
#test {
width: 500px;
height: 500px;
border: 1px solid red;
background-color: #ccf;
}
<div id='test'></div>