An encapsulated instance of the touch event in javascript mobile device Web development

  • 2020-03-30 03:11:14
  • OfStack

On touch devices, some of the more basic gestures require a secondary encapsulation of touch events.
Zepto is a class library with high usage on mobile terminal, but some events simulated by its touch module have some compatibility problems. For example, tap event has an event penetration bug on some android devices, and other types of events also have some compatibility problems more or less.

Therefore, I simply packaged these commonly used gesture events by myself. Since there were not many real devices to test, there might be some compatibility problems. The following code was only tested in some common browsers on iOS 7 and android 4.

Tap the event

The tap event is the equivalent of the click effect in a PC browser, and while it is still available on touch devices, there is some lag with click on many devices, and if you want a quick response to the "click" event, you need to use the touch event.


var startTx, startTy; element.addEventListener( 'touchstart', function( e ){
  var touches = e.touches[0];   startTx = touches.clientX;
  startTy = touches.clientY;
}, false ); element.addEventListener( 'touchend', function( e ){
  var touches = e.changedTouches[0],
    endTx = touches.clientX,
    endTy = touches.clientY;   //On some devices, the touch event is more sensitive, resulting in a slight change in the event coordinates when the finger is pressed and released
  if( Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6 ){
    console.log( 'fire tap event' );
  }
}, false );

DoubleTap event

LongTap event

The longTap event is an event that is triggered when a finger is held on the screen for an extended period of time.


var startTx, startTy, lTapTimer; element.addEventListener( 'touchstart', function( e ){
  if( lTapTimer ){
    clearTimeout( lTapTimer );
    lTapTimer = null;
  }   var touches = e.touches[0];   startTx = touches.clientX;
  startTy = touches.clientY;   lTapTimer = setTimeout(function(){
    console.log( 'fire long tap event' );
  }, 1000 );   e.preventDefault();
}, false ); element.addEventListener( 'touchmove', function( e ){
  var touches = e.touches[0],
    endTx = touches.clientX,
    endTy = touches.clientY;   if( lTapTimer && (Math.abs(endTx - startTx) > 5 || Math.abs(endTy - startTy) > 5) ){
    clearTimeout( lTapTimer );
    lTapTimer = null;
  }
}, false ); element.addEventListener( 'touchend', function( e ){
  if( lTapTimer ){
    clearTimeout( lTapTimer );
    lTapTimer = null;
  }
}, false );

Swipe events

Swipe is an event that is triggered when a finger slides on the screen. According to the direction of finger sliding, it can be divided into swipeLeft (left), swipeRight (right), swipeUp (up) and swipeDown (down).


var isTouchMove, startTx, startTy; element.addEventListener( 'touchstart', function( e ){
  var touches = e.touches[0];   startTx = touches.clientX;
  startTy = touches.clientY;
  isTouchMove = false;
}, false ); element.addEventListener( 'touchmove', function( e ){
  isTouchMove = true;
  e.preventDefault();
}, false ); element.addEventListener( 'touchend', function( e ){
  if( !isTouchMove ){
    return;
  }   var touches = e.changedTouches[0],
    endTx = touches.clientX,
    endTy = touches.clientY,
    distanceX = startTx - endTx
    distanceY = startTy - endTy,
    isSwipe = false;   if( Math.abs(distanceX) >= Math.abs(distanceY) ){
    if( distanceX > 20 ){
      console.log( 'fire swipe left event' );
      isSwipe = true;
    }
    else if( distanceX < -20 ){
      console.log( 'fire swipe right event' );   
      isSwipe = true;
    }
  }
  else{
    if( distanceY > 20 ){
      console.log( 'fire swipe up event' );       
      isSwipe = true;
    }
    else if( distanceY < -20 ){
      console.log( 'fire swipe down event' );        
      isSwipe = true;
    }
  }   if( isSwipe ){
    console.log( 'fire swipe event' );
  }
}, false );

The simulated events above are encapsulated in MonoEvent. The complete code address: https://github.com/chenmnkken/monoevent, see ~ need friends

  PS: here again for you to recommend an online query tool about JS events, summed up the common JS event types and functions:

Javascript events and function description:

(link: http://tools.jb51.net/table/javascript_event)


Related articles: