Javascript jQuery 连续鼠标按下
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4961072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery continuous mousedown
提问by pion
I have the following code snippets
我有以下代码片段
$(document).mousedown(function(event) {
doSomething();
}
I can capture the mousedownevent successfully.
我可以mousedown成功捕获事件。
I am trying to do the following:
我正在尝试执行以下操作:
- Capture the first
mousedownevent - I want to detect if the user is still holding the mouse down so I can do something else.
- 捕获第一个
mousedown事件 - 我想检测用户是否仍然按住鼠标,以便我可以做其他事情。
回答by RichardTheKiwi
Something like
就像是
var mouseStillDown = false;
$(document).mousedown(function(event) {
mouseStillDown = true;
doSomething();
});
function doSomething() {
if (!mouseStillDown) { return; } // we could have come back from
// SetInterval and the mouse is no longer down
// do something
if (mouseStillDown) { setInterval("doSomething", 100); }
}
$(document).mouseup(function(event) {
mouseStillDown = false;
});
回答by Janos
var int00; // declared here to make it visible to clearInterval.
$('#trigger').mousedown(function(){
int00 = setInterval(function() { repeatingfunction(); }, 50);
}).mouseup(function() {
clearInterval(int00);
});
function repeatingfunction() {
// This will repeat //
}
You can also put a clearIntervalon the mouseleaveevent.
您也可以clearInterval在mouseleave事件上放置一个。
回答by Nick Spiers
You'd implement some recursion!
你会实现一些递归!
var mouseisdown = false;
$(document).mousedown(function(event) {
mouseisdown = true;
doSomething();
}).mouseup(function(event) {
mouseisdown = false;
});
function doSomething(){
//Code goes here
if (mouseisdown)
doSomething();
}
回答by ehudokai
use the mousedown event to set a flag, and mouseup to unset the flag. Then you can simply check the flag, to see if it is set.
使用 mousedown 事件设置标志,使用 mouseup 取消设置标志。然后你可以简单地检查标志,看看它是否被设置。
exmaple
例子
var mouseDownFlag = false;
$(document).mousedown(function(event) {
mouseDownFlag = true;
someFunc();
}
$(document).mouseup(function(event) {
mouseUpFlag = true;
}
var someFunc = function(){
if(mouseDownFLag){//only run this function when the mouse is clicked
// your code
setTimeout("somefunc()", 1000); //run this function once per second if your mouse is down.
}
}
Hope that helps!
希望有帮助!
回答by peakit
You would need to do something like on mouseDown, start doing something and continue doing it till mouseUpevent is fired.
你需要做类似 on 的事情mouseDown,开始做某事并继续做直到mouseUp事件被触发。
回答by Eric London
$(document).ready(function(){
var mouseStillDown = false;
$('#some_element').mousedown(function() {
do_something();
}).mouseup(function() {
clearInterval(mouseStillDown);
mouseStillDown = false;
});
function do_something() {
// add some code here that repeats while mouse down
if (!mouseStillDown) {
mouseStillDown = setInterval(do_something, 100);
}
}
});
回答by codeiffor
let target = document.querySelector(".targetElement");
target.addEventListener('mousedown', event => {
target.addEventListener('mousemove',doStuff);
});
target.addEventListener('mouseup', event => {
target.removeEventListener('mousemove',doStuff);
});
function doStuff(event){
// code
}
I found this much more convenient.
我发现这更方便。

