我不希望mousemove
事件发生任何不必要的循环.所以我对性能/最佳实践感兴趣,什么mousemove
才是最好的运行方式mousedown == true
呢?目前我正在使用:
var pressedMouse = false; myObject.addEventListener("mousedown", function(e){ mouseDownFunction(e); pressedMouse = true; myObject.onmousemove = function(e) { if(pressedMouse == true){ mouseMoveFunction(e); } } }); myObject.addEventListener("mouseup", function(e){ pressedMouse = false; });
mouseMoveFunction()
因为pressedMouse
变量而没有被调用.可onmousemove
事件从如果触发防止mousedown
不用呢?
您可以通过从侦听器中删除onMouseMove-Function来阻止它被调用:
myObject.addEventListener("mousedown", function(e){ mouseDownFunction(e); myObject.onmousemove = function(e) { mouseMoveFunction(e); } }); myObject.addEventListener("mouseup", function(e){ myObject.onmousemove = null });