Mobile Event Point penetration problem

Source: Internet
Author: User

In mobile Web development, there may be some problems, this essay will focus on the tap point through the problem, the following points in detail:

What exactly is a point of penetration? Why does it appear to be a bit transparent? How do I avoid the point of penetration? If unavoidable, how to solve the problem of point penetration? ( I'm talking about the mobile side, not the PC side .)

1, what is the point of penetration?

In the following cases, there is a translucent gray covering layer A on the B element, and a clickable link within the yellow B element.

The layout code is as follows :

On the mobile side, click events typically use touch -related events to get high efficiency. Existing requirements, click on cover layer A to hide a (click to hide, which is important), then the relevant JavaScript code is as follows:

var div1 = document.getElementById ("A"), var div2 = document.getElementById (' B '); var con = document.getElementById (' Console '); function handle (e) {    var tar = E.target, Eve = E.type;   var ele = document.createelement ("P");   ele.innerhtml = "target:" + tar.id + "event:" + Eve;   Con.appendchild (ele);   if (tar.id = = = "Div1") {        Div1.style.display = "None";   }} Div1.addeventlistener ("Touchend", handle);d Iv1.addeventlistener ("Touchstart", handle);

This simple code is not a problem when clicked outside the link area, the following content appears in the log record console below the page:
Target:div1 Event:touchstart
Target:div2 Event:touchend

When in the link area to click on the cover layer A, you will find the jump to my blog page, that is, the original click a, but through a, click on the link to the B! This is the legendary click-through.

In order to see this process more clearly, we bind the click event for B, that is, if B triggers the Click event, then the click End point on A is on B, the code is as follows:

Div2.addeventlistener (' click ', handle);

Click on the B area and you can see the following in the log record console below the page:
Target:div1 Event:touchstart
Target:div1 Event:touchend
Target:div2 Event:click
As you can see, after the Div1 event is triggered, the div2 is captured by the Click event in the B area, and in fact we only clicked on the Div1.

2, why will there be a point of penetration?

First, the appearance of the scene:

A, A/b two layers up and down z-axis overlap.

b, the upper layer of a click to disappear or move away. (*)

The C, B element itself has a default click event (such as a tag) or B binds the Click event.

In the above case, click the A/b overlapping section, there will be a point-penetrating phenomenon.

Second, why will appear:

Click Delay! Click Delay! Click Delay!
The touch event instead of touching is not used on the mobile side because the Click event has a noticeable delay, and the specific Touchstart and click differ as follows:
Touchstart: The touch of a finger on this dom (or bubbling to this DOM) can trigger immediately
Click: In this Dom (or bubbling to this DOM) the finger Touch begins, and the finger never moves on the screen (some browsers allow a very small displacement value to be moved), and on this dom the finger leaves the screen, And the time between touch and leave screen is short (some browsers do not detect the interval and trigger click) to trigger
That is, the trigger time for an event is arranged as early as late as: Touchstart is earlier than touchend earlier than click. That is, the click Trigger has a delay, about 300ms in length.
Since we have hidden the overlay a in the Touchstart phase, when click is triggered, the element that can be clicked is the B element below it, according to the trigger rule of the Click event

3, how to avoid the point of penetration and solve the problem of point penetration?

For cases where the B element itself does not have a default click event (no a tag, etc.), you should use the touch event uniformly, unify the code style, and because the click event is much more delayed on the mobile side, which is detrimental to the user experience, so touch-related events should be used whenever possible.
For cases where the B element itself has a default click event, the default click event for the A element should be canceled in time to prevent the Click event from being generated. That is, you should add the following code in the handle function of the example above:

if (Eve = = "Touchsend") E.preventdefault ();

For the cover floating layer, because the cover floating layer of the Click even if there is a small delay is not related, but there will be a better user experience, so this situation, can be used to cover the floating layer itself with the click event, so there will be no point through the problem.

4. Existing settlement framework:

Zepto's tap event is a bit of a problem, but the latest version of Zepto has fixed the problem.
Before Zepto fix the problem, there are fastclick, hammer and other common libraries can be used.

Mobile Event Point penetration 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.