Home >Web Front-end >H5 Tutorial >Introduction to the use of advanced gestures in Javascript_html5 tutorial skills
The newly added recognition support for advanced user input in IE10, for example: register a click operation, and through the sentence addEventListener, you can know which device the current user clicks on, whether it is a finger click or a mouse click. Or click with a stylus (tablet devices all come with a stylus).
Create Gesture Object
The first step in handling gestures in your website is to instantiate a gesture object.
<span style="COLOR: blue">var<p> myGesture = <span style="COLOR: blue">new</span></p> MSGesture();</span>
Next, provide a target element for the gesture. The browser will trigger the gesture event on the element. At the same time, this element can also determine the coordinate space of the event.
elm = document.getElementById(<span style="COLOR: maroon">"someElement"</span>
<span style="COLOR: blue">var</span> myGesture = <span style="COLOR: blue">new</span> MSGesture();
elm.addEventListener(<span style="COLOR: maroon">"MSGestureChange"</span>
Finally, tell the gesture object which pointers to handle during gesture recognition.
elm.addEventListener(<span style="COLOR: maroon">"MSPointerDown"</span>, <span style="COLOR: blue">function</span> (evt) {
<span style="COLOR: rgb(0,100,0)">// adds the current mouse, pen, or touch contact for gesture recognition</span>
myGesture.addPointer(evt.pointerId);
});
Note: Don't forget that you need to use –ms-touch-action
to configure the element to prevent it from performing default touch actions (e.g., pan and zoom) and to provide pointer events for input.
Once a gesture object has a valid target and at least one pointer added, it will start firing gesture events. Gesture events can be divided into two types: static gestures (for example, click or hold) and dynamic gestures (for example, pinch, rotate, and swipe).
ClickThe most basic gesture recognition is click. When a click is detected, the MSGestureTap
event will be fired on the target element of the gesture object. Unlike click events, tap gestures can only be triggered when the user touches, presses a mouse button, or touches with a stylus without moving. This is often useful if you want to distinguish between a user clicking on an element and a user dragging it.
The long press gesture refers to an operation in which the user touches the screen with one finger, holds it for a moment and lifts it without moving. During a long press interaction, the MSGestureHold
event will be triggered multiple times for various states of the gesture:
Dynamic gestures (e.g. pinch or rotate) are reported as transitions, much like CSS 2D transitions. Dynamic gestures can trigger three events: MSGestureStart
, MSGestureChange
(which fires repeatedly as the gesture continues), and MSGestureEnd
. Each event contains information about scaling (shrinking), rotation, transformation, and speed.
Since dynamic gestures are reported as transitions, it's easy to manipulate elements like photos or puzzles using MSGesture
that includes CSS 2D transitions. For example, you can enable scaling, rotating, and dragging elements via: