JavaScript event analysis in touch screen _ javascript skills

Source: Internet
Author: User
This article mainly introduces the JavaScript events in the touch screen. The example analyzes the types, principles, and related usage skills of the touch screen events. For more information, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

I. touch events

Ontouchstart
Ontouchmove
Ontouchend
Ontouchcancel Currently, mobile browsers support these four touch events, including IE. Because the touch screen also supports MouseEvent, their order should be noted: touchstart → mouseover → mousemove → mousedown → mouseup → click1

Example:

/*** OnTouchEvent */var p = document. getElementById ("p"); // touchstart is similar to mousedownp. ontouchstart = function (e) {// The touches attribute of the event is an array. one element represents a touch point at the same time, // you can use touches to obtain each touch point of multi-touch. // since we only have one touch point, we direct it to [0] var touch = e. touches [0]; // Obtain the coordinates of the current touch point, equivalent to clientX/clientYvar x = touch of the MouseEvent event. clientX; var y = touch. clientY;}; // touchmove is similar to mousemovep. ontouchmove = function (e) {// you can add preventDefault to the touchstart and touchmove events to prevent the browser from scaling or scrolling e. preventDefault () ;}; // touchend is similar to mouseupp. ontouchup = function (e) {// nothing to do };

2. gesture event gestures refer to the use of multi-touch rotation, stretching, and other operations, such as the enlargement and rotation of samples and webpages. A gesture event is triggered only when two or more fingers are simultaneously touched. We need to pay attention to the coordinates of the element when scaling: We usually use offsetX, getBoundingClientRect, and other methods to obtain the coordinates of the element, however, in mobile browsers, pages are often scaled during use. Will the scaled element coordinates change? The answer is yes. This problem is illustrated in a scenario: After Page A is loaded, JavaScript obtains the coordinates of the element in the document as (100,100), and the user zooms in the page, at this time, the element coordinates are output again in JavaScript, and still (100,100), but the element will be offset in the response area of the screen according to the zoom ratio. You can open the brick-and-mortar game demo and zoom in after the page is fully loaded. then, you will find that even if your fingers touch the outside of the "touch here" area, you can control the ball board, this is because an offset occurs in the region. The offset will always exist unless the page is refreshed or scaled back.

/*** OnGestureEvent */var p = document. getElementById ("p"); p. ongesturechange = function (e) {// scale represents the scaling ratio generated by the gesture. if it is smaller than 1, it is scaled down. if it is greater than 1, it is scaled up. the original value is 1var scale = e. scale; // rotation indicates the angle of the rotation gesture. the value range is [0,360], and the positive value rotates clockwise. the negative value is counterclockwise var angle = e. rotation ;};

3. gravity sensing is relatively simple. you only need to add the onorientationchange event to the body node. In this event, the window. orientation attribute is used to obtain the value representing the current mobile phone direction. The value list of window. orientation is as follows:

0: consistent with the direction when the page is loaded for the first time
-90: 90 ° clockwise relative to the original direction
180: switched to 180 °
90: it turns 90 ° counter-clockwise. according to my test, Android2.1 does not support gravity sensing. The above is the current touch screen events. these events have not yet been incorporated into the standard, but have been widely used. I am Android2.1 and have not been tested in other environments.

I hope this article will help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.