Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of click, touch, and tap events in mobile WEB development

Detailed explanation of the use of click, touch, and tap events in mobile WEB development

php中世界最好的语言
php中世界最好的语言Original
2018-06-04 11:54:145829browse

This time I will bring you a detailed explanation of the use of click, touch, and tap events in mobile WEB development, and precautions for the use of click, touch, and tap events in mobile WEB development. What are they? The following is a practical case. Let’s take a look.

1. Comparison between click and tap

Both will be triggered when clicked, but on the mobile WEB side, click will have 200~300 ms, so please use tap instead of click as the click event.

singleTap and doubleTap Represents single click and double click respectively.

2. Regarding the click-through processing of tap

When using the tap of the zepto framework to move the click event in the device browser, to avoid When the click event is delayed in response, there may be a click-through situation, that is, the click will trigger a click event on a non-current layer.

Processing method:

(1),

There is a library called fastclick on github, which can also avoid delayed response to click events on mobile devices, https:// github.com/ftlabs/fastclick
Introduce it into the page with a script tag (the library supports AMD, so you can also use AMD specifications such as
require.js module loader introduction), and initialize it on the body when the dom is ready, such as:

$(function(){
    newFastClick(document.body);
})

Then bind the click event to the element that requires "no delay click" (Note that it is no longer the tap event bound to zepto).
Of course, you can also initialize it not on the body, but on a certain dom. In this way, only this dom and its sub-elements can enjoy "delay-free" clicks

In practical development, it was found that when the element is bound to fastclick, the click response speed is slightly faster than tap. Haha

(2) Bind the touchend event to the element and add e.preventDefault();

$demo.on('touchend',function(e){//改变了事件名称,tap是在body上才被触发,而touchend是原生的事件,在dom本身上就会被捕获触发
    $demo.hide()
    e.preventDefault();//阻止“默认行为”
})

<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;'></span>

3. Touch event touch is for touch events on touch screen mobile phones. Most touch screen mobile phone webkit kernels today provide touch event monitoring, allowing developers to obtain some information when the user touches the screen.

Including: touchstart, touchmove, touchend, touchcancel These four events

touchstart, touchmove, and touchend events can be compared to the triggering of mousedown, mouseover
, and mouseup.

touchstart: Triggered when the finger touches the screen;

touchmove: Triggered when the finger moves on the screen;

touchend: It will be triggered when the finger leaves the screen;

And touchcancel, many people don’t know when it will be triggered and ignore it. In fact, When your finger has not left the screen, touchcancel will be triggered when a system-level operation occurs, such as alert and confirm pop-ups, or android system function pop-ups.

The triggering order of these four events is:

touchstart -> touchmove
-> …… -> touchmove ->touchend

But just monitoring the above single event is not enough for us to complete the monitoring of some gestures operations that are common on touch screen phones, such as double-click, long press, left and right sliding, zooming and other gestures. operate. It is necessary to combine monitoring of these events to encapsulate this type of gesture action.

In fact, many frameworks on the market have encapsulated these gestures for mobile browsers, such as jqmobile, zepto, and jqtouch. However, tragedy happened. For some android systems (I tested it myself In android 4.0.x), touchmove and touchend events cannot be triggered well. Here is an example:

For example, when a finger drags the page from top to bottom on the screen, theoretically Yes will trigger a touchstart , many times touchmove
, and the final touchend, but on Android 4.0, touchmove is only triggered once, the triggering time is about the same as touchstart
, and touchend is not triggered directly. This is a very serious bug. Many people have raised it in google Issue http://code.google.com/p/android/issues/detail?id=19827

## For the time being, I only found that this bug exists in Android 4.0, and it is said that the iOS 3.x version will also have it.

Obviously jqmobile, zepto, etc. are not aware of the serious impact of this bug on the monitoring implementation, so when directly using the events of these frameworks, there will be more or less compatibility. Sex problem!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;'></span>

Recommended reading:<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;'></span>

Commonly used e8e496c15ba93d81f6ea4fe5f55a2244 code summary<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;'></span>

How to use JS to get function parameter names<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;'></span>

The above is the detailed content of Detailed explanation of the use of click, touch, and tap events in mobile WEB development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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