Reprint – Mobile Internet terminal Touch event, touchstart, touchend, touchmove

Source: Internet
Author: User
Tags macbook
<span id="Label3"></p><p><p><strong>reproduced please specify:</strong> reproduced from the Web front-end development (www.css119.com)-focus on common Web front-end development issues, the latest Web front-end development technology (webapp development, mobile Site development), The best Web Front-End development tool and the most comprehensive web front-end development W3cschool manual</p></p><p><p><strong>This article link address:</strong> Web front-end development (www.css119.com) – Touch events for mobile internet terminals, touchstart, touchend, touchmove</p></p><p><p><br>Mobile devices such as smartphones <span class="wp_keywordlink">and tablets <span class="wp_keywordlink">typically have a capacitive touchscreen (capacitive touch-sensitive Screen) to capture the interaction of the User's Finger. With the development of mobile networks, which can support more and more complex applications,<span class="wp_keywordlink">Web developers need a way to handle these Events. For example, almost all fast-paced games require the player to press multiple buttons at once, in this way, in the case of a touchscreen, meaning Multi-touch. Apple introduced touch event api,android in iOS 2.0 and is catching up to this fact standard to narrow the gap. A recent work group is working together to develop this touch event Specification.<br><br>Safari on iOS also supports traditional interactive events like Click and mouseover, but it's not recommended to use Click and mouseover on iOS browser apps, because these two events are designed to support mouse clicks. The Click event has a delay of about half a second on iOS because iOS wants to highlight the element that receives the CLICK. Events such as Mouseover/out are triggered by the click of a finger. therefore, on ios, You should discard the traditional interactive event model and accept a new event model. Touch events and more advanced gesture events allow your Web pages to interact like native apps.<br><br><strong>There are three basic touch events that are listed in the specification and have been widely implemented across mobile devices:</strong></span></span></span></p></p><p><p>1. <span class="wp_keywordlink_affiliate">touchstart: Place your finger on a DOM Element.<br>2. <span class="wp_keywordlink_affiliate">touchmove: Drag a DOM element with your Finger.<br>3. <span class="wp_keywordlink_affiliate">touchend: Move the finger away from a DOM Element.<br><br><strong>Each touch event includes three touch lists:</strong></span></span></span></p></p><p><p>1. Touches: A list of all the fingers that are currently on the Screen.<br>2. Targettouches: A list of the fingers that are located on the current Dom Element.<br>3. Changedtouches: A list of fingers that involve the current Event.</p></p><p><p>For example, in a <span class="wp_keywordlink_affiliate">touchend event, this would be a moving finger.<br><br><strong>These lists consist of objects that contain touch information:</strong></span></p></p><p><p>1. Identifier: A numeric value that uniquely identifies the current finger in a touch session.<br>2. The target:dom element is the target of the Action.<br>3. Client/page/screen coordinates: The position where the action takes place on the Screen.<br>4. RADIUS coordinates and rotationangle: draw an ellipse roughly equivalent to the shape of a finger.<br><br>Before you begin to describe the touch event, you need to describe a touch object that is unique to a multi-touch system (android and ios, and even Nokia's newest Meego system simulates a similar object, only for ios, because <span class="wp_keywordlink">I only have the ipad available for testing. )。 This object encapsulates a screen touch, usually from a finger. It is generated when the touch event is triggered and can be picked up by the event object of the touch events handler (typically via the Event.changedtouches property). This object includes some important attributes:</span></p></p><p><p>Client/clienty: the location of the touch point relative to the browser window viewport</p></p><p><p>Pagex/pagey: the position of the touch point relative to the page</p></p><p><p>Screenx/screeny: the position of the touch point relative to the screen</p></p><p><p>Unique ID of the Identifier:touch object</p></p><p><p>We start the world of Multi-Touch Web pages from an instance touched by a single finger. When a finger is lowered, a block appears on the screen, and the finger moves the block as it moves, and the finger mentions the block Disappears. first, Let's define the CSS for the Block <span class="wp_keywordlink">:</span></p></p><pre><pre>*{margin:0;padding:0}<span class="wp_keywordlink">html,body{height:100%}.spirit{position:absolute;width:50px;height:50px; background-color:red;} #canvas {position:relative;width:100%;height:200px;background-color: #ccc}</span></pre></pre><p><p>then, under body, define a container to receive the Event:</p></p><pre><pre><div id= "canvas" ></div></pre></pre><p><p>Define <span class="wp_keywordlink_affiliate">the event handler for Touchstart and bind the event:</span></p></p><pre><pre>var canvas = document.getElementById ("canvas"), spirit, startX, starty;function touchstart (event) {//block page Default action    (ie Web page Scrolling) event.preventdefault ();    If (spirit | |!event.touches.length) return;    var touch = event.touches[0];    StartX = touch.pagex;    Starty = touch.pagey;    Spirit = Document.createelement ("div");    Canvas.appendchild (spirit);    Spirit.classname = "spirit";    Spirit.style.left = StartX + "px"; Spirit.style.top = Starty + "px";} Canvas.addeventlistener ("touchstart", touchstart, false);</pre></pre><p><p>first, we'll Use the Block spirit as a global object, because we're going to test a single finger now so it's better to have only one object on the screen moving (and so on with a multi-touch instance). In Touchstart This event handler, We also first determine whether a spirit has been generated, that is, whether a finger has been placed on the screen, and if so, return directly.</p></p><p><p>Like the traditional event listener, a multi-touch system produces an event object, except that the object has more properties, such as the event.touches here, which gets all the touch on the Screen. Notice here the Event.preventdefault (), in the traditional event handler, this method blocks the default action of the event, the default action of the touch event is the scroll screen, we do not want the screen to move around, so call this function First. Let's take the first touch and pagex/y it as the initial position when the spirit was Created. next, we create a div and set classname,left,top three Properties. finally, we appendchild the Spirit object into the Container. so, when the first finger is down, a red, 50px square block is placed on the Screen.</p></p><p><p>then, we're going to start processing the events that the finger moves on the screen:</p></p><pre><pre>function TouchMove (event) {event.preventdefault ();    If (!spirit | |!event.touches.length) return;    var touch = event.touches[0], x = touch.pagex-startx, y = touch.pagey-starty;         This is for the finger must be horizontal scrolling, the principle is to calculate the offset of x position is greater than the offset of y if (math.abs (x) > math.abs (y)) {spirit.style.left = Touch.pagex + "px";    Spirit.style.top = Touch.pagey + "px"; }}canvas.addeventlistener ("<span class="wp_keywordlink_affiliate">touchmove", touchmove, false);</span></pre></pre><p><p>In touch move listener, we use webkit-specific CSS properties: webkittransform to move the block, this property specifically how to use Google. It is recommended to use Webkit's own features when constructing Web pages for iOS devices, not only to dazzle, but also to use hardware directly to improve PERFORMANCE.</p></p><p><p>finally, We handle the Touchend Event. When the finger is lifted, The block is removed from the Screen.</p></p><pre><pre>function Touchend (event) {if (!spirit) return;    Canvas.removechild (spirit); Spirit = null;} Canvas.addeventlistener ("touchend", touchend, false);</pre></pre><p><p>Test the above code on your ipad or Iphone. If not unexpected, a complete multi-touch web program is Born.<br><strong>Device Support</strong><br>unfortunately, the implementation of touch events varies greatly in terms of completeness and Quality. I have written a diagnostic script to show some basic information about the touch API implementation, including which events are supported, and the Touchmove event-triggered Solution. I tested Android 2.3.3 on Nexus One and Nexus S hardware, tested Android 3.0.1 on the xoom, and tested iOS 4.2 on ipad and Iphone.</p></p><p><p>In short, all tested browsers support touchstart, touchend, and Touchmove Events.</p></p><p><p>The specification provides an additional three touch events, but the browser being tested does not support them:</p></p><p><p>1. Touchenter: move the finger into a DOM Element.<br>2. toucheleave: Move your finger away from a DOM Element.<br>3. Touchcancel: Touch interrupted (implementation specification).</p></p><p><p>The tested browser also provides a list of touches, targettouches, and changedtouches within each touch List. however, The browser being tested does not support radiusx, radiusy, or rotationangle properties, which indicate the shape of the finger touching the Screen. During a touchmove, the event is triggered about 60 times a second, as is the case with all the tested Devices.<br><strong>Developer Tools</strong><br>In mobile development, It is easier to start prototyping on the desktop and then process the mobile-specific parts on the devices that you intend to support. Multi-Touch is one of those features that is difficult to test on a PC because most PCs do not have touch Input.</p></p><p><p>Testing that has to be done on a mobile device may lengthen your development cycle, because every change you make requires that the code be submitted to the server and then loaded onto the Device. then, once run, there's not much debugging on the app, because tablets and smartphones lack the tools that Web developers use.</p></p><p><p>One solution to this problem is to simulate triggering events on the development Machine. For single touch, touch events can be simulated based on mouse events. If you have a touch input device, such as a modern app MacBook, then multi-touch can also be simulated.</p></p><p><p>Single Touch events</p></p><p><p>If you want to simulate a single touch event on your desktop, try Phantom limb, which simulates touch events on a Web page and provides a giant hand to guide it.</p></p><p><p>There's also touchable, a <span class="wp_keywordlink">jquery plugin that unifies touch and mouse events across Platforms. </span></p></p><p><p>Multi-Touch events</p></p><p><p>To make your multi-touch web app work on your browser or multi-touch trackpad (like Apple MacBook or magicpad), I created this magictouch.js fill tool that captures touch events from the trackpad, They are then converted to standard-compatible touch Events.</p></p><p><p>1. Download the nptuioclient NPAPI plugin and install it into the ~/library/internet plug-ins/directory.</p></p><p><p>2. Download the Tongseng Tuio app for this Mac Magicpad and start the Server.</p></p><p><p>3. Download the magictouch.js <span class="wp_keywordlink">JavaScript library to emulate the Specification-compliant touch events based on the Nptuioclient Callback. </span></p></p><p><p><strong>Introduction to touch Event we donuts, here to recommend two Documents:</strong></p></p><p><p>Safari Dom Additions reference:http://developer.apple.com/library/safari/#documentation/appleapplications/ Reference/safarijsref/intro/intro.html#//apple_ref/doc/uid/tp40001482-ch2g-bajdajag</p></p><p><p>Safari Web content guide:</p></p><p><p>http://developer.apple.com/library/safari/#documentation/appleapplications/reference/safariwebcontent/ Introduction/introduction.html</p></p><p><p>For programmers interested in developing Multi-Touch Web applications, Apple's developer site is a place that should be frequented.</p></p><p><p><strong>A simple example of what I do:</strong><br>Http://www.css119.com/demo/touchmove.html</p></p><p><p><strong>reproduced please specify:</strong> reproduced from the Web front-end development (www.css119.com)-focus on common Web front-end development issues, the latest Web front-end development technology (webapp development, mobile Site development), The best Web Front-End development tool and the most comprehensive web front-end development W3cschool manual</p></p><p><p><strong>This article link address:</strong> Web front-end development (www.css119.com) – Touch events for mobile internet terminals, touchstart, touchend, touchmove</p></p><p><p>Reprint – Mobile Internet terminal Touch event, touchstart, touchend, touchmove</p></p></span>

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.