Javascript 用if语句检查mousedown是否?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7700066/
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
Check if mousedown with an if statement?
提问by android.nick
Is it possible to do something like this:
是否有可能做这样的事情:
if ($(this).mousedown() == true) {
I thought that would work but it doesn't.
我以为那会奏效,但没有。
Additional details:I'm trying to check if the mouse button is down when the mouse leaves a specific DIV
, so if the person is holding the mouse button down while their mouse leaves the div, do this, otherwise do that.
其他详细信息:我正在尝试检查鼠标离开特定位置时鼠标按钮是否按下DIV
,因此如果该人在鼠标离开 div 时按住鼠标按钮,请执行此操作,否则执行此操作。
回答by James Allardice
The easiest way I can think of is to bind mousedown
and mouseup
event listeners to the document
and update a global variable accordingly. In the mouseout
event of your element you can check the state of that variable and act as appropriate. (Note: this assumes that you don't care whether or not the mouse was pressed down while over the div
or not... you'll have to clarify your question around that).
我能想到的最简单的方法是绑定mousedown
和mouseup
监听事件的document
并相应地更新全局变量。对于mouseout
您的元素,您可以检查该变量的状态并采取适当的行动。(注意:这假设您不在乎鼠标是否被按下而超过div
...您必须澄清您的问题)。
var down = false;
$(document).mousedown(function() {
down = true;
}).mouseup(function() {
down = false;
});
$("#example").mouseout(function() {
if(down) {
console.log("down");
}
else {
console.log("up");
}
});
Here's a working exampleof the above.
这是上述的一个工作示例。
回答by Joshua Robinson
Answering your original question, yes this is possible. When the mousedown event fires over an element the element becomes active and is selectable by the pseudo selector :active. In jQuery if nothing is selected an empty array is returned, we can use this to our advantage in combination with the .length array property and turn the active pseudo selector into a check to see if the mouse is down over a particular element or not like so:
回答您最初的问题,是的,这是可能的。当 mousedown 事件在元素上触发时,该元素变为活动状态,并且可以通过伪选择器 :active 进行选择。在 jQuery 中,如果没有选择任何内容,则返回一个空数组,我们可以将其与 .length 数组属性结合使用以发挥我们的优势,并将活动伪选择器转换为检查鼠标是否在特定元素上所以:
if ( $('#id:active').length ) {
//do something
}
Checking if the mouse wasdown over a particular element at a given point in time is a different event and does not go with the title of this question and should be changed might I add. Google brought me here and this answer is for those that will inevitably follow.
检查如果鼠标是下跌超过一个特定的元素在给定时间点是不同的事件,不与这个问题的标题去,应该改变我想补充。谷歌把我带到了这里,这个答案是为那些不可避免的人准备的。
回答by Dzoki
something like this?
像这样的东西?
Code:
代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Press mouse and release here.</p>
<script>
$("p").mouseup(function(){
$(this).append('<span style="color:#F00;">Mouse up.</span>');
}).mousedown(function(){
$(this).append('<span style="color:#00F;">Mouse down.</span>');
});
</script>
</body>
</html>
回答by Ben
Alright, well the best option I can give you is this: http://jsfiddle.net/qK8rr/2/
好吧,我能给你的最好的选择是:http: //jsfiddle.net/qK8rr/2/
回答by Richard
Check out this one. http://jsfiddle.net/b1Lzo60n/
看看这个。 http://jsfiddle.net/b1Lzo60n/
Mousedown starts a timer that checks if mouse is still down after 1 second.
Mousedown 启动一个计时器,检查鼠标是否在 1 秒后仍然按下。
<button id="button">Press me</button>
<div id="log"></div>
Code:
代码:
var mousedown = false;
var mousedown_timer = '';
$('#button').mousedown(function(e) {
mousedown = true;
$('#log').text('mousedown...');
mousedown_timer = setTimeout(function () {
if(mousedown) {
$('#log').text('1 second');
}
}, 1000);
}).mouseup(function(e) {
mousedown = false;
clearTimeout(mousedown_timer);
$('#log').text('aborted');
});