Android Phone Touchend event does not trigger a problem

Source: Internet
Author: User

Problem description

$ (document). On ("Touchstart Touchmove", ". Btn-highlight",function(event) {        $ (this). AddClass ("hover");    }). On ("Touchend touchcancel", ". Btn-highlight",function(event) {        $ (this). Removeclass ("hover");    });

At first, I wanted to use this piece of code to simulate the highlight effect of some buttons (that is, after clicking a button there will be a different style, like PC hover)

But there is a problem, that is, on the Android phone, the following method is often not triggered, very occasionally, really make our troubles.

After reviewing the data, we found that the browser's default event affected the triggering of our event. Then it involves blocking the default event, very simply, plus event.preventdefault ();

The final available code is

$ (document). On ("Touchstart Touchmove", ". Btn-highlight",function(event) {        $ (this). AddClass ("hover");        Event.preventdefault ();    }). On ("Touchend touchcancel", ". Btn-highlight",function(event) {        $ (this). Removeclass ("hover");    });

In the CSS inside the. Btn-highlight plus the corresponding style can see the effect, such as button A and button B want to show different effects, then

A and B are added first. btn-highlight

In CSS:

a.hover{/* First style */}

b.hover{/* the second style of */}

The sequel to the problem ~ ~ ~

After discovering this, what if the button is a link??? Skip, because the default event is disabled, now put Touchstart and touchmove separate write OK

$ (document). On ("Touchstart", ". Btn-highlight",function() {        $ (this). AddClass (" Hover ");    }). On ("Touchmove", ". Btn-highlight",function(event) {        event.preventdefault ();    }). On ("Touchcancel touchend", ". Btn-highlight",function(event) {        $ (this). Removeclass ("hover");    });

Android Phone Touchend event does not trigger a problem

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.