Mobile era has come, as the front-end development we have no reason and should not sit idle, but bravely jumped out of the heart of that well, to embrace the blue sky. The coming will come, all we have to do is accept the unknown challenge. As you can see, this is an article on mobile touch events, which is the most commonly used action in a mobile phone: touch. Now let's start with touch touch!
Touch Events
First touch consists of three types of events: Touchstart, Touchmove, and Touchend. Words too literally this instinct to believe you should have, but here I still have to make a needless explanation of these three words.
Time of instruction
Touchstart: Triggered when the finger touches a DOM element.
Touchmove: Triggered when the finger slides on a DOM element.
Touchend: Triggered when a finger is moved off a DOM element.
These three events correspond to three identical touch lists, respectively:
Time of instruction
Touches: A list of all the fingers that are touching the screen.
Targettouches: Touching a list of fingers on the current DOM element.
Changedtouches: A list of fingers that involve the current event.
Touch Properties
Three lists of events the names are not the same, but they contain almost everything, including some information about the current event, such as: some coordinate information.
TouchList {0: Touch, length: 1}length:10:Touch clientX:65 // 触摸点在浏览器窗口中的横坐标 clientY:18 // 触摸点在浏览器窗口中的纵坐标 force:1 // 触摸点压力大小 identifier:0 // 触摸点唯一标识(ID) pageX:65 // 触摸点在页面中的横坐标 pageY:18 // 触摸点在页面中的纵坐标 radiusX:11.5 // 触摸点椭圆的水平半径 radiusY:11.5 // 触摸点椭圆的垂直半径 rotationAngle:0 // 旋转角度 screenX:560 // 触摸点在屏幕中的横坐标 screenY:175 // 触摸点在屏幕中的纵坐标target:div#touchLog 触摸目标__proto__:Touch__proto__:TouchList
Above is a list of touchlist. It corresponds to one of the three previously mentioned events (Touchstart, Touchmove, touchend), a list of objects generated at the time of the trigger. The most useful thing in the list is the touch object, and the touch object holds some relevant information about the corresponding event, and we can achieve various effects through the organic combination of these attributes in this event.
The three properties of the above radiusx,radiusy,rotationangle can be used to calculate the touch surface of your finger when touching the screen of the phone, except that the contact surface is represented by an approximate ellipse, which means that it is not a real contact surface, It's an approximate contact surface. Believe cautious friends should see a Touchlist object has a length property, and its value is 1, which means that only one finger is currently triggering an event (such as the Touchstart event), in other words, you only have one finger on the phone screen, Some of the information that this finger corresponds to is stored in the Touch object. Because there is only one finger on the screen, there is only one Touch object in this touchlist, and the first subscript is 0. The Touchlist list also has a target property, which should be well understood, which is the goal of touch.
In order to give you a more solid understanding of these attributes, I have specifically taken a message from the Internet to add:
from Mozilla
1.touch.identifier: Unique identifier for this Touch object. A touch action (we mean the touch of a finger) is the entire process of moving on a plane, and the identifier remains the same. It can be used to determine if the trace is the same touch process, and this value is read-only.
2.touch.screenx: The x-coordinate of the contact relative to the left edge of the screen. Read-only property.
3.touch.screeny: The y-coordinate of the contact relative to the edge of the screen. Read-only property.
4.touch.clientx: The x-coordinate of the contact point relative to the left edge of the visible viewport (visual viewport). Does not include any scrolling offsets. Read-only property.
5.touch.clienty: The y-coordinate of the contact point relative to the edge on the visible viewport (visual viewport). Does not include any scrolling offsets. Read-only property.
6.touch.pagex: The x-coordinate of the contact point relative to the left edge of the HTML document. When there is a horizontal scrolling offset, this value contains the offset of the horizontal scroll. Read-only property.
7.touch.pagey: The y-coordinate of the contact relative to the edge of the HTML document. When there is a horizontal scrolling offset, this value contains the offset of the vertical scroll. Read-only property.
8.touch.radiusx: The horizontal axis (x-axis) radius of the smallest ellipse that can surround the contact surface of the user and touch plane. The unit of this value is the same as ScreenX. Read-only property.
9.touch.radiusy: The vertical axis (y-axis) radius of the smallest ellipse that can surround the contact surface of the user and touch plane. The unit of this value is the same as ScreenY. Read-only property.
10.touch.rotationangle: It is an angle value: the positive-direction ellipse described by RadiusX and radiusy, which needs to be rotated clockwise to be able to cover the contact surface of the user and touch plane most accurately. Read-only property.
11.touch.force: The pressure of the finger to squeeze the touch plane, from 0.0 (no pressure) to 1.0 (maximum pressure) floating point number. Read-only property.
12.touch.target: The HTML element where the contact is located when the contact is first tracked (in the Touchstart event). Even when the contact is moving, the position of the contact has left the effective interactive area of the element, or the element has been removed from the document. It is important to note that if this element is removed during the touch process, the event will still point to it, but it will not bubble up the event to the window or Document object. Therefore, if there are elements that may be removed during the touch process, it is a best practice to bind the listener of the touch event to the element itself, preventing the element from being removed from being able to detect events bubbling from that element on its previous level element. Read-only property.
Touch probe single finger operation
To get a deeper understanding of the Touch event, let's now do a simple DEMO.
HTML Code
<!DOCTYPE html>
JavaScript Code
<script>var obj = document.getElementById("touchLog");obj.addEventListener("touchstart", showMsg);function showMsg(ev) { console.log(ev.touches); console.log(ev.targetTouches); console.log(ev.changedTouches);}</script>
The above is bound to the Touchstart event, the same as the other two events, but the trigger is not the same point in time. Running the above code will output the results as follows:
You might ask: Why are their data all the same, and actually very well understood when you press the screen to trigger Touchstart at this point and then execute the ShowMsg () method. What about the change to Touchmove? Not surprisingly, when you move your finger, these three touchlist in the first group are the same, why say the first group, because Touchmove is triggered when you move your finger, so when you move your finger more than once, there will naturally be more than one set of data. But it's a little different for touchend. You can see the difference between Touchend and the other two by running the following code:
<script>var touchLog = document.getElementById("touchLog");touchLog.addEventListener("touchstart", showMsgTouchstart);function showMsgTouchstart(ev) { console.log("---touchstart---"); console.log(ev.touches); console.log(ev.targetTouches); console.log(ev.changedTouches);}touchLog.addEventListener("touchmove", showMsgTouchmove);function showMsgTouchmove(ev) { console.log("---touchmove---"); console.log(ev.touches); console.log(ev.targetTouches); console.log(ev.changedTouches);}touchLog.addEventListener("touchend", showMsgTouchend);function showMsgTouchend(ev) { console.log("---touchend---"); console.log(ev.touches); console.log(ev.targetTouches); console.log(ev.changedTouches);}</script>
After running you will find Touchend touches, targettouches is no touch object, the length value is zero, such as:
Here we can also do a more interesting test, that is, touch the target element, the finger and then slide out of the target element. What does the touches, targettouches, and changedtouches that correspond to the above three events output? Will it be the same?
if it's not an accident,
1.touchstart events: Touches, targettouches, and Changedtouches are the same.
2.touchmove events: Touches, targettouches, and Changedtouches are the same (data will still have multiple groups, for reasons that have already been analyzed).
3.touchend event: When your finger leaves the screen, that is, the Touchend event triggers, the length of touches, targettouches Touchlist is also 0, meaning there is no touch object.
So when using the Touchend event, be aware that only changedtouches will have touch objects. However, the value of the target property in the Touchlist object is Div#touchlog, meaning that the value of the target property is Div#touchlog regardless of whether your finger is on the target element or is sliding out of the destination element after touching the screen. As long as you touch the screen, force has a value of 1, so there's no sense in this force.
Multi-finger operation
Let's take a look at the following section of code, and if the conditions permit, please use the phone to access: http://yunkus.com/demo/mobile-touch-event/multi-finger-touchstart.html Look at the effect of the Touchstart event when it is triggered, with the following code:
<script>var obj = document.getElementById ("Touchlog"); Obj.addeventlistener ("Touchstart", SHOWMSG) ; function showmsg (ev) {obj.innerhtml = ""; var touchesstr = ""; var targettouchesstr = ""; for (var i = 0; i < ev.touches.length; i++) {touchesstr + = "Identifier:" + Ev.touches[i].identifier + ", X-axis Coordinates: "+ ev.touches[i].clientx +" <br> "; Targettouchesstr + = "Identifier:" + Ev.targettouches[i].identifier + ", x-axis coordinates:" + ev.targettouches[i]. Clien TX + "<br>"; } obj.innerhtml = "Below is ev.touches data:<br>" + touchesstr + "<br> below is ev.targettouches data: <b R> "+ targettouchesstr +" <br> below is ev.changedtouches data:<br> "+" identifier: "+ Ev.cha Ngedtouches[0].identifier + ", x-axis coordinates:" + ev.changedtouches[0].clientx + "<br>";} </script>
Access http://yunkus.com/demo/mobile-touch-event/multi-finger-touchmove.html View the effect when Touchmove event is triggered, after Touchmove event is triggered There is more than one touch object in Changedtouches, but it is the same as the other two touchlist lists.
Access http://yunkus.com/demo/mobile-touch-event/multi-finger-touchend.html View the effect when the Touchend event is triggered, only after the Touchmove event is triggered Changedtouches has touch and only one touch object, no matter how many fingers you put on the screen at the same time, the touch object corresponds to the last finger you left the screen.
The above three demos will output the identifier values in the touches, targettouches, and Changedtouches when the event is triggered, as well as a clientx value. ClientX is used to let you determine which finger corresponds to which identifier by X's coordinates. For Touchstart events, because a touch object is always saved in changedtouches, it is not traversed, but is accessed directly from the subscript. From the above we can learn that there are touchstart events in touches, targettouches and changedtouches are different: Changedtouches under only a touch object, This object corresponds to the finger that triggered the last change in the event (for example, the last touch of the screen). This is why there is only one touch object in changedtouches because there is always only one finger changing at a time.
But can not be biased, because the Touchmove event in the changedtouches in more than one, but with touches and targettouches also have multiple touch objects.
As mentioned earlier, Touchend only has a touch object in the Changedtouches list, which corresponds to the finger of the last finger (e.g., the last left screen).
By studying single finger operation and multi-finger operation, we can understand the touch event more stereoscopic and in place.
Default Event
Some behaviors are triggered by default when moving fingers, such as scrolling and zooming. The above example does not prevent the default behavior of touch events. So when you test the example above multi-finger-touchmove.html, you will find that sometimes you feel helpless, the page is prone to zoom behavior, and even affect the test results. To prevent the default behavior of touch events is also very simple just add the following code to it:
document.addEventListener("touchstart", function (ev) { ev.preventDefault();});
The benefit of adding a blocking default behavior for touch events is not the only one.
1. Setting meta-Disable user scaling under iOS 10 is not effective, use Ev.preventdefault (); Can be implemented to prohibit users from zooming the page.
2. Resolve the issue of overflow hiding (not working) under IOS 10.
3. Disable the system's default scroll bar (e.g. horizontal scroll bar), and rubber band effect.
4. Do not long press the selected text, select the picture, the system default menu.
5. Solve the problem of point penetration.
While there are so many benefits, it is important to note that there are some problems at this point, such as: input cannot get the focus. However, you can regenerate the input tag by adding the Touchstart event separately to the input tag and preventing it from bubbling.
var inputObj = document.getElementsByTagName("input")[0];document.addEventListener("touchstart", function (ev) { ev.preventDefault();});inputObj.addEventListener("touchstart", function (ev) { ev.stopPropagation();});
Here is a Demo, a mobile focus graph switch effect implemented by touch related events: http://yunkus.com/demo/mobile-touch-event/.
Mobile-Touch Events