Resolve the conflict between onmousedown onmouseup and onclick events

Source: Internet
Author: User

Generally, onclick can be considered as a collection of onmousedown and onmouseup. Onclick is generally used to deal with the instant click effect, while onmousedown and onmouseup are mainly used to deal with effects with time intervals, such as the effect of dragging and parking the mouse.

Conflict scenarios:

If an object contains both onclick and onmousedown. If you click this object, these three objects are triggered at the same time. If we only want the single-host effect, but unfortunately, it triggers onmousedown and onmouseup, and I don't want to trigger onmousedown or onmouseup at all.

Solution:

Call the code in onmousedown asynchronously using setTimeout. The interval is 200 ms.

If the time between Down and up is

1)> 200 ms, call onmousedown and onmouseup

2) <= 200 ms, call the Click Event

Code: (using jquery code as an example)

 

VaR mousedownanduptimer = NULL; var onmousedownflag = false; $ (element ). click (function () {// click code in here}); $ (element ). mousedown (function () {onmousedownflag = false; mousedownanduptimer = setTimeout (function () {// onmousedown code in here onmousedownflag = true ;}, 200 );}). mouseup (function () {If (onmousedownflag) {// onmouseup code in here} else {cleartimeout (mousedownanduptimer); // clear delay time }});

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.