最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if mousedown with an if statement? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Oct 8, 2011 at 22:03 android.nickandroid.nick 11.2k24 gold badges78 silver badges112 bronze badges 5
  • Do you want to know if the left mouse button is down on an item, or if they just pressed it anywhere on the page? – Jess Commented Oct 8, 2011 at 22:04
  • Are you trying to determine which event has fired a function/method? – Jared Farrish Commented Oct 8, 2011 at 22:05
  • Updated question, yes left button down on the div. – android.nick Commented Oct 8, 2011 at 22:05
  • Are you trying to see if someone is dragging or selecting an element/text? – Jared Farrish Commented Oct 8, 2011 at 22:08
  • use mousemove with e.which == 1; – Muhammad Umer Commented Oct 10, 2014 at 17:09
Add a comment  | 

5 Answers 5

Reset to default 27

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).

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 example of the above.

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:

if ( $('#id:active').length ) {
  //do something
}

Checking if the mouse was down 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.

http://jsfiddle.net/5HrWF/

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>

Alright, well the best option I can give you is this: http://jsfiddle.net/qK8rr/2/

Check out this one. http://jsfiddle.net/b1Lzo60n/

Mousedown starts a timer that checks if mouse is still down after 1 second.

<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');
});
发布评论

评论列表(0)

  1. 暂无评论