[英]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
,而是这样做:
mousedown
and mouseup
. mousedown
和mouseup
。 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). setTimeout
, again remembering the handle. setTimeout
,再次记住该句柄。 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: 两种解决方案:
Either listen for mouseup
on document
, or 监听
document
mouseup
,或者
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.