Javascript onMouseDown函数,在左键单击时进行测试

Javascript onMouseDown函数,在左键单击时进行测试,javascript,Javascript,这是我的代码的基本思想。当按下鼠标左键时,我希望它执行设置代码,然后当鼠标未按下时,它执行另一个任务。我该怎么做呢。谢谢:) 在上面的代码中,当用户在网页的任何地方按下鼠标左键时,代码将运行,当用户向上按下时,将执行另一组代码 了解更多 希望有帮助 在上面的代码中,当用户在网页的任何地方按下鼠标左键时,代码将运行,当用户向上按下时,将执行另一组代码 了解更多 希望能有所帮助。将mousedown和mouseup事件与setInterval/clearInterval一起使用 function d

这是我的代码的基本思想。当按下鼠标左键时,我希望它执行设置代码,然后当鼠标未按下时,它执行另一个任务。我该怎么做呢。谢谢:)

在上面的代码中,当用户在网页的任何地方按下鼠标左键时,代码将运行,当用户向上按下时,将执行另一组代码

了解更多

希望有帮助

在上面的代码中,当用户在网页的任何地方按下鼠标左键时,代码将运行,当用户向上按下时,将执行另一组代码

了解更多


希望能有所帮助。

将mousedown和mouseup事件与setInterval/clearInterval一起使用

function detectLeftButton(evt) {
   evt = evt || window.event;
   var btn = evt.which || evt.button;
   return (btn == 1);
}
document.onmousedown = function() {
   if(detectLeftButton(this.event)) {
   // Code when mouse is down
   }
}

document.onmouseup = function() {
   if(detectLeftButton(this.event)) {
   // Code when mouse is not held
   }
}

将mousedown和mouseup事件与setInterval/clearInterval一起使用

function detectLeftButton(evt) {
   evt = evt || window.event;
   var btn = evt.which || evt.button;
   return (btn == 1);
}
document.onmousedown = function() {
   if(detectLeftButton(this.event)) {
   // Code when mouse is down
   }
}

document.onmouseup = function() {
   if(detectLeftButton(this.event)) {
   // Code when mouse is not held
   }
}

如果我只想在某个画布上使用它,我会将文档更改为var c6=document.getElementById(“云”);c6@帕特里克·埃文斯这就是为什么我的答案(下面的答案)更好的原因。更灵活、更简单。:-)@reidjako如果希望在画布上执行此操作,请使用canvas元素替换它。@reidjako还请注意,如果是在画布上执行动画,请使用
requestAnimationFrame
而不是intervals@chris97ong,您的解决方案只允许在每次单击发生或放松时执行一次,当鼠标落下或向上时,它不会持续运行如果我只想在某个画布上运行,我会将文档更改为var c6=document.getElementById(“云”);c6@帕特里克·埃文斯这就是为什么我的答案(下面的答案)更好的原因。更灵活、更简单。:-)@reidjako如果希望在画布上执行此操作,请使用canvas元素替换它。@reidjako还请注意,如果是在画布上执行动画,请使用
requestAnimationFrame
而不是intervals@chris97ong,您的解决方案只允许在每次单击发生或放松时执行一次,当鼠标向下或向上时,它不会持续运行术语“左键”应该是“主键”,因为这是它使用的术语。定点设备可能没有逻辑左按钮或右按钮,并且可能有任何配置为主按钮的按钮。术语“左按钮”可能应该是“主按钮”,因为这是所使用的术语。定点设备可能没有逻辑左或右按钮,并且可能有任何配置为主按钮的按钮。该按钮和按钮不能像那样互换。对于按钮,表示主要是
0
,而对于哪个(以及Microsoft的按钮实现),则是
1
。在上有一篇好文章。哪个和按钮不能像那样互换。对于按钮,表示主要是
0
,而对于哪个(以及Microsoft的按钮实现),则是
1
。网站上有一篇好文章。
(function(){
   var downTimer = null;
   var upTimer = null;
   function runWhileDown(){
      //code to run while mouse is down
   }
   function runWhileUp(){
      //code to run while mouse is up
   }
   document.addEventListener("mousedown",function(){
      //Stop the runWhileUp code from running
      if(upTimer) clearInterval(upTimer);
      //make sure the previous interval is stopped
      if(downTimer) clearInterval(downTimer);
      //start the new timer
      downTimer = setInterval(runWhileDown,20);
   });
   document.addEventListener("mouseup",function(){
      //Stop the runWhileDown code from running
      if(downTimer) clearInterval(downTimer);
      //make sure the previous interval is stopped
      if(upTimer) clearInterval(upTimer);
      //start the new timer
      upTimer = setInterval(runWhileUp,20);
   });
   //If you need the runWhileUp code to run at the start
   upTimer = setInterval(runWhileUp,20);
})();