AJ Naidas
AJ Naidas

Reputation: 1424

mousehold event while doing mousemove

I've downloaded a JQuery plugin for a mousehold event.

http://remysharp.com/2006/12/15/jquery-mousehold-event/

I have this div that calls for mousemove event:

div.addEventListener('mousemove',function() {
//things
});

whenever I call the mousehold event like this:

var value = 0;
$(div).mousehold(function() {
value += 20;
$(div).html(value);
});

it would work. But if I start moving (thus, calling the mousemove event) while onmousehold, the value does not increase anymore, meaning, it stopped calling the mousehold event even if I got my left click still on hold.

How can I make it happen that when I do a mousemove, the mousehold event still works? tnx!

Upvotes: 4

Views: 5917

Answers (2)

yes
yes

Reputation: 261

You could use the event object like so:

div.addEventListener('mousemove', function(event) {

  // `event.buttons` gets the state of the mouse buttons
  // 0 = not pressed, 1 = left click pressed, 2 = right click pressed
  if (event.buttons === 1) {
    value += 20;
  }

});

Upvotes: 0

Phix
Phix

Reputation: 9890

What you're asking (at least, what I think you're asking), is simple enough with some basic logic.

By binding the mousedown, mousemove, and mouseup and setting a flag for the "down" state of the mouse, this can easily be done: JSFiddle

Upvotes: 5

Related Questions