I want to be able to bind multiple sequential events using jquery. I want to do the following:
Click on
div1– aka mousedown event –
now, IF you start moving the mouse while you are STILL pressing down on the mouse, THEN do some function.
What’s the smoothest way of doing this? Just to put an if inside of a .on() call, or is there something simpler?
You can make use of
.on()and.off()to achieve this.Notice that
.on()and.off()are the recommended methods to bind and unbind events respectively.You can check a live example.
UPDATE
Alternatively, you can bind the
mouseupevent to thedocument. This way you can detect the release of the mouse even if it doesn’t occur while hovering your element.Also, a shorthand function for it. Let’s call it
.drag().You can see it live here.