Home Web Front-end H5 Tutorial Analysis of Tap events and Tap point-through principles

Analysis of Tap events and Tap point-through principles

Jul 28, 2018 am 10:48 AM
html5 javascript

The content of this article is about the analysis of the Tap event and the Tap point-through principle. It has a good reference value and I hope it can help friends in need.

First introduce the tap event:

  1. The meaning of the tap event: On the mobile side, the click event will be delayed by 300ms because the browser determines whether the click will continue within 300ms. , to determine whether to zoom the web page. (That is, there is a 300ms time to judge the effect of double-clicking to enlarge the web page. After 300ms has passed, the click event will be triggered)

  2. #Implementation of the tap event: Use the touch event supported by the browser by default To simulate, tap events are simulated based on the three events of touchstart, touchmove, and touchend to achieve the effect of encapsulating tap events. The code below is a simple encapsulation I made.

//定义tap函数,传入需要绑定的元素,和一个回调函数
function tap(el,callBack){
    var startTime = 0;
    var maxTime = 250;
    var [startX,startY,endX,endY] = [0,0,0,0]; //es6解构赋值
    el.addEventListener('touchstart',function(e){                            
        console.log('touchstart');
        startTime = Date.now(); //开始触摸的事件  
        startX = e.touches[0].clientX; //手指在浏览器横坐标
        startY = e.touches[0].clientY; //手指在浏览器纵坐标
    })
    el.addEventListener('touchmove',function(e){
        console.log('touchmove');
        endX = e.touches[0].clientX; //手指在浏览器横坐标
        endY = e.touches[0].clientY; //手指在浏览器纵坐标
    })
    el.addEventListener('touchend',function(e){
        console.log('touchend');
        if( (Date.now()-startTime) > maxTime){ //如果超过了最大时间,不触发tap
               console.log('超时了');
            return ; 
        }
        //如果移动距离过大,则不是tap事件。为了大家在电脑上能看到效果,这里设置成了1000,因为在电脑上移动幅度不好控制。如果是在移动端,设置为30就差不多了。
        if(Math.abs(endX-startX) > 1000 || Math.abs(endY-startY) > 1000){
             return;
        }
        callBack(e);
     })
}
tap(document.documentElement,function(e){
    console.log(e);
});
Copy after login

To sort it out: the sequence of tap events is touchstart -> touchmove -> touchend -> Execute callback in touchend

The principle of tap penetration:

  1. It is common to see this application scenario. Click on the mask layer, and the mask layer disappears, revealing the bottom page. When an element in the bottom page is bound to a click event, and you click on the mask exactly at the position of the element, you will find that the click event of the element is triggered.

  2. The click triggering sequence on the PC side is mousedown -> mousemove -> mouseup -> click. The triggering sequence on the mobile side is touchstart -> touchmove - > touchend -> click is executed in this order. Because our mask has disappeared when touchend, it is equivalent to clicking on the element in the bottom page. So the bottom element will trigger the click event.

The solution for tap penetration:

  1. Use tap event or click event uniformly.

  2. Delay the disappearance of the mask layer to more than 300ms

  3. Get a transparent mask to block it (not recommended, too stupid, Still troublesome)

  4. Use fastclick library

Related recommendations:

HTML5 new attributes: five global Introduction to attributes

canvas native implementation of front-end web page mobile signature

The above is the detailed content of Analysis of Tap events and Tap point-through principles. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles