简体   繁体   English

使用JQuery按下按钮或锚点时触发多个单击事件

[英]Trigger multiple click events while a button or anchor is pressed using JQuery

I have a situation in which I click the button, and it does some action like the following: 我遇到的情况是单击按钮,它会执行以下操作:

 var pos = 1; $('.right').click(function(){ $('.box').css({'left': pos++ }); }); $('.left').click(function(){ $('.box').css({'left': pos-- }); }); 
 .box{ background-color: gray; height:20px; width: 20px; left: 0; position: relative; margin-top: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <button class="left" button-held>Left</button> <button class="right" button-held>Right</button> <div class="box"></div> 

What I need is that when the user clicks on the button and leave it pressed, that it do this action multiple times until the button is released. 我需要的是,当用户单击按钮并保持按下状态时,它将多次执行此操作,直到释放按钮为止。

Any ideas on how to do this? 有关如何执行此操作的任何想法? I have already search the internet for a while but it's quite difficult to find an answer as there are lots of questions about events, also I found an Angular solution if someone wants to check it out ( Angular solution ). 我已经在互联网上搜索了一段时间,但是由于存在很多有关事件的问题,因此很难找到答案,而且如果有人想检查一下,我也找到了一个Angular解决方案( Angular解决方案 )。

Thanks in advance. 提前致谢。

Instead of listening for click , do this: 而不是监听click ,而是这样做:

  1. Listen for mousedown and mouseup . 监听mousedownmouseup
  2. On mousedown , perform the action and set a timer to perform it again in a moment via setTimeout . mousedown ,执行操作并设置一个计时器,以稍后通过setTimeout再次执行该操作。 Remember the timer handle (the return value). 记住计时器句柄(返回值)。
  3. When the timer goes off and you do the action again, schedule it to happen again in a moment, again via setTimeout , again remembering the handle. 当计时器关闭并且您再次执行操作时,将其安排在稍后再次发生,再次通过setTimeout ,再次记住该句柄。
  4. When you see mouseup , cancel the outstanding timer using the handle with clearTimeout . 当您看到mouseup ,请使用带有clearTimeout的句柄取消未完成的计时器。

Example: 例:

 var pos = 1; var handle = 0; function move(delta) { $('.box').css({'left': pos += delta }); } function moveRight() { move(1); clearTimeout(handle); // Just in case handle = setTimeout(moveRight, 50); } function moveLeft() { move(-1); clearTimeout(handle); // Just in case handle = setTimeout(moveLeft, 50); } $('.right').on("mousedown", moveRight); $('.left').on("mousedown", moveLeft); $('.left, .right').on("mouseup", function() { clearTimeout(handle); handle = 0; }); 
 .box{ background-color: gray; height:20px; width: 20px; left: 0; position: relative; margin-top: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <button class="left" button-held>Left</button> <button class="right" button-held>Right</button> <div class="box"></div> 


In a comment you've said: 在评论中,您说过:

I just noticed a bug within this, if I leave one of the buttons pressed, and then slide out of it while still keeping it pressed and then releasing it, it move the box indefinitely until you press again one of the buttons, do you maybe know why this is happening? 我只是注意到其中有一个错误,如果我按住一个按钮,然后滑出它,同时仍然按住它,然后放开它,它会无限期地移动盒子,直到再次按下其中一个按钮,你可能知道为什么会这样吗?

It's because we're only listening for mouseup on the button, so when you release it, we don't get that event. 这是因为我们只在听按钮上的mouseup ,所以当您释放它时,我们不会得到该事件。 Silly mistake on my part. 我这是愚蠢的错误。 :-) :-)

Two solutions: 两种解决方案:

  1. Either listen for mouseup on document , or 监听document mouseup ,或者

  2. Listen for mouseleave on the button as well as mouseup . 侦听按钮上的mouseleave以及mouseup

I think #1 is probably best, especially since when I just tried it on Chrome, it even gracefully handled the case where I pressed the mouse down over the button, then slid it right out of the browser window entirely (!) and released it. 我认为#1可能是最好的,尤其是因为当我在Chrome上尝试使用它时,它甚至可以很好地处理以下情况:我将鼠标按下按钮上方,然后将其完全滑出浏览器窗口(!),然后松开。 Chrome still gave us the mouseup on `document. Chrome仍然为我们提供了`document上的mouseup :-) :-)

Implementing #1 is just a matter of hooking mouseup on document instead of .left, .right : 实现#1只是将mouseup挂在document而不是.left, .right

 var pos = 1; var handle = 0; function move(delta) { $('.box').css({'left': pos += delta }); } function moveRight() { move(1); clearTimeout(handle); // Just in case handle = setTimeout(moveRight, 50); } function moveLeft() { move(-1); clearTimeout(handle); // Just in case handle = setTimeout(moveLeft, 50); } $('.right').on("mousedown", moveRight); $('.left').on("mousedown", moveLeft); // ONLY CHANGE is on the next line $(document).on("mouseup", function() { clearTimeout(handle); handle = 0; }); 
 .box{ background-color: gray; height:20px; width: 20px; left: 0; position: relative; margin-top: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <button class="left" button-held>Left</button> <button class="right" button-held>Right</button> <div class="box"></div> 

Implementing #2 is just a matter of adding mouseleave to the mouseout handler; 实施#2只是在mouseout处理程序中添加mouseleave即可。 but note that the button retains its "pushed" appearance even though we stop doing the movement as soon as the mouse leaves the button: 但请注意,即使我们在鼠标离开按钮后立即停止移动,该按钮仍保持其“按下”状态:

 var pos = 1; var handle = 0; function move(delta) { $('.box').css({'left': pos += delta }); } function moveRight() { move(1); clearTimeout(handle); // Just in case handle = setTimeout(moveRight, 50); } function moveLeft() { move(-1); clearTimeout(handle); // Just in case handle = setTimeout(moveLeft, 50); } $('.right').on("mousedown", moveRight); $('.left').on("mousedown", moveLeft); // ONLY CHANGE is on the next line $('.left, .right').on("mouseup mouseleave", function() { clearTimeout(handle); handle = 0; }); 
 .box{ background-color: gray; height:20px; width: 20px; left: 0; position: relative; margin-top: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <button class="left" button-held>Left</button> <button class="right" button-held>Right</button> <div class="box"></div> 

Something like this? 像这样吗 Here you go 干得好

var pos = 1;

$('.right').mousedown(function(){    
    myVar = setInterval(function(){ $('.box').css({'left': pos++ }); }, 100);
});
$('.right').mouseup(function(){    
    clearInterval(myVar);
});
$('.left').mousedown(function(){    
    myVar = setInterval(function(){ $('.box').css({'left': pos-- }); }, 100);
});

$('.left').mouseup(function(){    
    clearInterval(myVar);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM