0 javascript jquery javascript-events
我如何处理jQuery,以便只有在按下鼠标左键的情况下才能激活mousemove事件,并且只要按钮被释放,mousemove事件就会停止?
在这里,我将mousemove事件绑定在mousedown上,并将其解除绑定mouseup.我在jQuery中使用了一个很酷的功能,因此可以在此选择器中添加其他mousemove事件,但只删除了要取消绑定的事件.它们被称为命名空间事件.
$("#yourdivid").mousedown(function(e){
if(e.which == 1){
$(this).bind('mousemove.coolnamespace', function(e){
// Your mousemove event
});
}
}).mouseup(function(e){
if(e.which == 1) $(this).unbind('mousemove.coolnamespace');
})
Run Code Online (Sandbox Code Playgroud)
编辑:我更新了我的答案以使用规范化字段which.jQuery规范化为1 =左,2 =中,3 =右.button根据浏览器的不同,该值会有所不同.