Skip to main content

HTML5 Touch Events

Free2015-03-30#HTML#触摸事件#touch#HTML5

HTML5 supports some special mobile events, such as touch events, which are more efficient than traditional events on mobile devices

I. Why Use Touch Events?

Touch events are HTML5 events specific to mobile browsers, but this is certainly not the main reason to use them. Because click on mobile has significant delay (about 300ms), the 300ms delay comes from determining double-tap and long-press, because only when the default waiting time ends and it is determined that there are no subsequent actions will the click event be triggered. The delay of touch events is very short, using touch events can improve page response speed and bring better user experience.

II. Standard Touch Events

Touch Events in Webkit
Event NameDescriptionContains touches Array
touchstartTouch starts (user places finger on screen)Yes
touchmoveTouch point changes (user moves finger on screen without lifting)Yes
touchendTouch ends (user lifts finger from screen)Yes
touchcancelTouch is cancelled (touch is interrupted by something, such as a notification)No

P.S. The touches array is an array of touch objects, touch objects carry the data we want

Properties of Touch Objects
PropertyDescription
identifierUnique identifier for this touch, this value does not change as long as the user's finger remains on the screen
screenXDistance of touch point relative to the left edge of the screen
screenYDistance of touch point relative to the top of the screen
clientXDistance of touch point relative to the left edge of the browser window
clientYDistance of touch point relative to the top of the browser window
pageXDistance of touch point relative to the left edge of the page
pageYDistance of touch point relative to the top of the page
targetGet event source, similar to event.target

P.S. There are actually three more properties: radiusX/radiusY, rotationAngle, force but due to poor compatibility and differences in browser implementation, they are not introduced here

III. IE10 Touch Events

IE Pointer Events
Event NameDescription (on touch devices)
MSPointerDownTouch starts
MSPointerMoveTouch point moves
MSPointerUpTouch ends
MSPointerOverTouch point moves into element, equivalent to mouseover
MSPointerOutTouch point leaves element, equivalent to mouseout

P.S. IE calls it pointer events because this event has three trigger forms: mouse click, stylus tap and finger touch

MSPointerEvent Properties
PropertyDescription
hwTimestampTime when event was created (ms)
isPrimaryIdentifies whether this pointer is the primary pointer
pointerIdUnique ID of the pointer (similar to identifier in touch events)
pointerTypeAn integer that identifies whether the event comes from mouse, stylus or finger
pressurePen pressure, 0-255, only available when inputting with stylus
rotationInteger from 0-359, rotation of cursor (if supported)
tiltX/tiltYTilt of stylus, only supported when using stylus input

Detecting pointerType:

function handleEvent(event){
  switch(event.pointerType){
    case event.MSPOINTER_TYPE_TOUCH:
  //finger
  break;
case event.MSPOINTER_TYPE_PEN:
  //stylus
  break;
case event.MSPOINTER_TYPE_MOUSE:
  //mouse
  break;
 default:
   break;
  }
}
document.body.addEventListener("MSPointerDown", handleEvent, false);

Actually there is a simpler way to detect fingers: directly judge whether the touches array exists, for example:

document.addEventListener('MSPointerDown', function(e){
  switch(e.type){
    case 'touchstart':var startPos = e.touches ? e.touches[0].pageX : e.screenX;
    //...
  }
}, false);

IV. Encapsulated tap Event Source Code: (tap is click on mobile)

(function(){

    var TOUCHSTART, TOUCHEND;

    //normal touch events
    if (typeof(window.ontouchstart) != 'undefined') {
    
        TOUCHSTART = 'touchstart';
        TOUCHEND   = 'touchend';
    
    //microsoft touch events
    } else if (typeof(window.onmspointerdown) != 'undefined') {
        TOUCHSTART = 'MSPointerDown';
        TOUCHEND   = 'MSPointerUp';
    } else {
        TOUCHSTART = 'mousedown';
        TOUCHEND   = 'mouseup';
    }


    function NodeFacade(node){
    
        this._node = node;
    
    }

    NodeFacade.prototype.getDomNode = function() {
        return this._node;
    }

    NodeFacade.prototype.on = function(evt, callback, scope) {
    
        var scopeObj;
    
        if(!scope) {
            scopeObj = this._node;
        } else{
            scopeObj = scope;
        }

    
        if (evt === 'tap') {
            this._node.addEventListener(TOUCHSTART, function() {
                callback.apply(scope, arguments);
            });
        } else if (evt === 'tapend') {
            this._node.addEventListener(TOUCHEND, function() {
                callback.apply(scope, arguments);
            });
        } else {
            this._node.addEventListener(evt, function() {
                callback.apply(scope, arguments);
            });
        }
    
        return this;
    
    }

    window.$ = function(selector) {

        var node = document.querySelector(selector);

        if(node) {
            return new NodeFacade(node);
        } else {
            return null;
        }
    }


})();

P.S. The above source code comes from touch-interfaces.com

Reference Materials

  • "HTML5 Touch Interface Design and Development"

Comments

No comments yet. Be the first to share your thoughts.

Leave a comment