IPhone Web development touch events

Source: Internet
Author: User

IPhone Web developmentTouchEventThis is what we will introduce. Of course, your iPhone uses your fingers instead of the mouse. Also, you can touch and knock with some fingers. ThereforeIPhoneUp, mouseEventTouchedEventInstead. These mouseEventInclude:

Touchstart

Touchend

Touchmove

Touchcancel when the system cancels the touch)

If you subscribe to any of these events, your event listener will receive an event object. It has some important attributes, such:

* Touches-a set of touch objects, one-to-one with the fingers that touch the screen. The Touch object has the pageX and pageY attributes to contain the coordinates of the touch on the page.

* TargetTouches-a set of touch targets. Unlike a set of touch objects, targetTouches only acts on the target elements of the Object Storage, rather than the entire page.

The following example shows a simple drag-and-drop implementation. Let's draw a box on the black background page and drag it down. Now you have to subscribe to the touchmove event and update the position of the box when the finger moves, like this:

 
 
  1. window.addEventListener('load', function() {    
  2.  var b = document.getElementById('box'),  
  3.      xbox = b.offsetWidth  / 2, // half the box width  
  4.      ybox = b.offsetHeight / 2, // half the box height  
  5.      bbstyle = b.style; // cached access to the style object    
  6.  b.addEventListener('touchmove', function(event) {  
  7.    event.preventDefault(); // the default behaviour is scrolling  
  8.    bstyle.left =  event.targetTouches[0].pageX - xbox + 'px';  
  9.    bstyle.top  =  event.targetTouches[0].pageY - ybox + 'px';  
  10.  }, false);    
  11. }, false); 

TouchmoveEventThe listener first removes the default behavior of finger movement-otherwise Safari will scroll the page. The event.tar getTouches set contains a list of data of all fingers on the div element. Now we only need to care about one finger, so we use event.tar getTouches [0]. Then, pageX tells the X coordinate of the finger. If you subtract half of the div width from this value, it is the middle of the box.

Summary: DetailsIPhone Web developmentTouchEventI hope this article will help you.

Related Article

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.