简体   繁体   中英

What's the difference between 'mouseup' and 'click' events?

Using jQuery, I often like to use mousedown and mouseup events in conjunction for pushable buttons.

However, in every case I've used the mouseup event, binding the click event instead seemed to produce identical results.

Is there any substantial difference between the two methods below?

// Method 1
$('.myButton').bind('click', callback);

// Method 2
$('.myButton').bind('mouseup', callback);

Please note I'm seeking a technical explanation on the differences between using both methods. This has no relation to the question that has been flagged as a dupe: Differentiate click vs mousedown/mouseup

With a mouseup event, you can click somewhere else on the screen, hold down the click button, and move the pointer to your mouseup element, and then release the mouse pointer.

A click event requires the mousedown and mouseup event to happen on that element.

The normal expectation is that a click requires both the mousedown and mouseup event, so I'd recommend the click event.

From the possible duplicate, it appears that mouseup and mousedown events can also be caused by mouse buttons other than the left click button. Which is very different from what a generic user would expect.

My understanding is that "click" hides lots of complexities (such as making sure that mousedown/up occur on the same element, cancelling with ESC/right click). Using "click" over "mousedown/up" should be preferred.

One scenario where "click" does not seem to work when app updates content very often in such a way that underlying DOM elements get replaced. In this case "click" will not be triggered and it might result in poor customer experience.

I think Mouse Down and Mouse Up events give you further control over the click event. It divides the click event into two more events so that more details can be coded for each event. Click event restricts the mouse click and force you to code both the events in the same function.

You can understand this restriction if you ever try to make your own dragging behavior.

  1. Dragging needs a mouse-down event to start a drag behavior. You cannot do it with click event. And since you need a separate mouse-down event. The requirement of a separate mouse-up event becomes obvious.
  2. Once the dragging starts ( you have not yet release the mouse button) you need the object to change position as per the cursor position. This too needs to be coded only in mouse-down event.

However you can use click event too if we could change the way how people drag the objects. For example click-1 starts the drag and click-2 stops the drag and puts the object on another position. But there are two problems I see:

  1. It does not look natural. As in the real world we are in habit of pressing the object and dragging it.
  2. It can be process intensive to move heavy graphics just by clicking and mouse-move.

The biggest difference that affects the way I code is: the click event on an a HTML tag is responsible for changing the URL. In contrast, the mousedown and mouseup events will not acheive this

I would like to add to the other answers that click event works on touch-enabled devices while mouseup / mousedown do not (obviously because there's no "mouse")

Note that there's a 300ms delay on touch devices with the click event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM