Analysis of Tap events and Tap point-through principles
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:
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)
#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); });
To sort it out: the sequence of tap events is touchstart -> touchmove -> touchend -> Execute callback in touchend
The principle of tap penetration:
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.
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:
Use tap event or click event uniformly.
Delay the disappearance of the mask layer to more than 300ms
Get a transparent mask to block it (not recommended, too stupid, Still troublesome)
Use fastclick library
Related recommendations:
HTML5 new attributes: five global Introduction to attributes
canvas native implementation of front-end web page mobile signatureThe 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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.

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

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

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

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

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