The following is an introduction to the HTML5 touch event evolution tap event. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at the touch event as a HTML5 event unique to mobile browsers. Although the click event is more common on PCs and mobile terminals, there will be a Ms latency on mobile terminals, which affects user experience, ms latency is determined by double-click and long-press, because the click event is triggered only when the default wait time ends to determine that no subsequent action has occurred. Therefore, the touch event response is faster and the experience is better.
Touch event type:
There are multiple types of touch events to differentiate the state changes related to the touch. You can checkTouchEvent.type
Attribute to determine the type of the current event.
Note:In many cases, touch events and mouse events are triggered at the same time (the purpose is to make code that is not optimized for the touch device still work on the touch device ). If you use a touch event, you can callevent.preventDefault()
To prevent mouse events from being triggered.
Standard touch events
Event name |
Description |
Contains the touches Array |
Touchstart |
Triggered when a user places a contact on the touch plane. Event Targetelement It will be the target at the contact position.element |
Yes |
Touchmove |
Triggered when the user moves the contact on the touch plane. Event Targetelement And this touchmove EventTouchstart event Target element Same, Even whentouchmove When the event is triggered, the contact has been removed from thiselement . |
Yes |
Touchend |
Triggered when a contact is removed from the touch plane (when the user leaves the touch plane. It is also triggered when the contact is removed from the border of the touch plane. For example, you can draw your finger at the edge of the screen. Contacts that have been removed from the touch plane can beThe changedTouches attribute defines TouchList . |
Yes |
Touchenter |
When the contact enterselement .This event does not have a bubble process. |
Yes |
Touchleave |
When the contact leaveselement .This event does not have a bubble process. |
Yes |
Touchcancel |
Triggered when the contact is interrupted for some reason. There are several possible causes (depending on the device and browser ):
- Because an event cancels the touch, for example, the touch process is interrupted by a modal pop-up box.
- The contact leaves the document window and enters the browser's interface elements, plug-ins, or other external content areas.
- When the number of contacts generated by the user exceeds the number supported by the device
TouchList The oldestTouch The object is canceled.
|
Yes |
Touch Object Attributes
Touch.identifier |
Returns the value of a point that uniquely identifies the point in contact with the touch plane. This value remains consistent among all events triggered by this finger (or touch pen, etc.) until it leaves the touch plane. |
Touch.screenX |
The X coordinate of the contact relative to the left side of the screen.Read-only attribute. |
Touch.screenY |
Y coordinate of the contact relative to the edge of the screen.Read-only attribute. |
Touch.clientX |
X coordinate of the contact relative to the left side of the visible area. No scroll offset is included.Read-only attribute. |
Touch.clientY |
Y coordinate of the contact relative to the edge of the visible area. No scroll offset is included.Read-only attribute. |
Touch.pageX |
The X coordinate of the contact relative to the left side of the HTML document.When the level exists Rolling This value contains the offset of horizontal scrolling. .Read-only attribute. |
Touch.pageY |
Y coordinate of the contact relative to the edge of the HTML document.When a horizontal scroll offset exists, this value contains the vertical scroll offset. .Read-only attribute. |
Touch.radiusX |
The radius of the horizontal axis (X axis) of the smallest elliptical surface that can enclose the user and the touch plane. The unit of this value andSame as screenX. Read-only attribute. |
Touch.force
|
The pressure on the touch plane of the finger, from 0.0 (no pressure) to 1.0 (maximum pressure) floating point number.Read-only attribute. |
Touch.radiusY
|
The vertical axis (Y axis) radius of the smallest elliptical surface that can enclose the user and the touch plane. The unit of this value andSame as screenY. Read-only attribute. |
Touch.target
|
When this contact is first tracked (touchstart Event), the contact is located in the HTML element. Even when the contact moves, the contact position has left the valid interaction area of the element, Or this element has been removed from the document. Note that if this element is removed during the touch process, this event will still point to it, but it will not bubble upwindow Ordocument Object. Therefore, if an element may be removed during the touch process, the best practice is to bind the listener of the touch event to the element itself to prevent the element from being removed, events that bubble from the element cannot be detected from its upper-level element.Read-only attribute. |
IE10 + touch events
IE pointer event
Event name |
Description (on a touch device) |
MSPointerDown |
Start with touch |
MSPointerMove |
Touch point movement |
MSPointerUp |
Touch ends |
MSPointerOver |
The touch point moves to the element, which is equivalent to mouseover. |
MSPointerOut |
Touch Point exit element, equivalent to mouseout |
MSPointerEvent attributes
Attribute |
Description |
HwTimestamp |
Time when an event was created (MS) |
IsPrimary |
Indicates whether the pointer is a master pointer. |
PointerId |
Unique pointer ID (similar to the identifier of a touch event) |
PointerType |
An integer that identifies the event from the mouse, pen, or finger |
Pressure |
Pen pressure, 0-255, only available when writing |
Rotation |
An integer between 0 and, indicating the rotation of the cursor (if supported) |
TiltX/tiltY |
The tilt of the Pen. supported only when the pen is input. |
Equivalent event
Mouse |
Touch |
Keyboard |
Mousedown |
Touchstart |
Keydown |
Mousemove |
Touchmove |
Keydown |
Mouseup |
Touchend |
Keyup |
Mouseover |
|
Focus |
Apparently, the touch action sequence: touchstart-touchmove-touchend is similar to the mouse sequence column: mousedown-mousemove-mouseup and the keyboard sequence: keydown-keypress-keyup. This is not a coincidence, the three interaction modes can be described as start-move-stop.
In other words, the click process must go through the touchstart-touchmove-touchend process, with a latency of Ms. Therefore, the tap event is required, and the touch time at the same point is very short.
Encapsulated tap and longtap events
Copy XML/HTML Code to clipboard
- (Function (){
- Var TOUCHSTART, TOUCHEND;
- If (typeof (window. ontouchstart )! = 'Undefined '){
- TOUCHSTART = 'touchstart ';
- TOUCHEND = 'touchend ';
- TOUCHMOVE = 'touchmove ';
-
- } Else if (typeof (window. onmspointerdown )! = 'Undefined '){
- TOUCHSTART = 'mspointerlow ';
- TOUCHEND = 'mspointerup ';
- TOUCHMOVE = 'mspointermove ';
- } Else {
- TOUCHSTART = 'mousedown ';
- TOUCHEND = 'mouseup ';
- TOUCHMOVE = 'mousemove ';
- }
- Function NodeTouch (node ){
- This. _ node = node;
- }
- Function tap (node, callback, scope ){
- Node. addEventListener (TOUCHSTART, function (e ){
- X = e. touches [0]. pageX;
- Y = e. touches [0]. pageY;
- });
- Node. addEventListener (TOUCHEND, function (e ){
- E. stopPropagation ();
- E. preventDefault ();
- Var curx = e. changedTouches [0]. pageX;
- Var cury = e. changedTouches [0]. pageY;
- If (Math. abs (curx-x) <6 & Math. abs (cury-y) <6 ){
- Callback. apply (scope, arguments );
- }
- });
- }
- Function longTap (node, callback, scope ){
- Var x, y, startTime = 0, endTime = 0, in_dis = false;
- Node. addEventListener (TOUCHSTART, function (e ){
- X = e. touches [0]. pageX;
- Y = e. touches [0]. pageY;
- StartTime = (new Date (). getTime ();
- });
- Node. addEventListener (TOUCHEND, function (e ){
- E. stopPropagation ();
- E. preventDefault ();
- Var curx = e. changedTouches [0]. pageX;
- Var cury = e. changedTouches [0]. pageY;
- If (Math. abs (curx-x) <6 & Math. abs (cury-y) <6 ){
- In_dis = true;
- } Else {
- In_dis = false;
- }
- EndTime = (new Date (). getTime ();
- If (endTime-startTime> 300 & in_dis ){
- Callback. apply (scope, arguments );
- }
- });
- }
- NodeTouch. prototype. on = function (evt, callback, scope ){
- Var scopeObj;
- Var x, y;
- If (! Scope ){
- ScopeObj = this. _ node;
- } Else {
- ScopescopeObj = scope;
- }
- If (evt = 'tap '){
- Tap (this. _ node, callback, scope );
- } Else if (evt = 'longtap '){
- LongTap (this. _ node, callback, scope );
- } Else {
- This. _ node. addEventListener (evt, function (){
- Callback. apply (scope, arguments );
- });
- }
- Return this;
- }
- Window. $ = function (selector ){
- Var node = document. querySelector (selector );
- If (node ){
- Return new NodeTouch (node );
- } Else {
- Return null;
- }
- }
- })();
- Var box = $ ("# box ");
- Box. on ("longtap", function (){
- Console. log ("You have long-pressed ");
- }, Box)
The above introduction to the HTML5 touch event evolution tap event is all the content that I have shared with you. I hope you can give us a reference and support me a lot.
Address: http://www.cnblogs.com/hutuzhu/archive/2016/03/25/5315638.html