您好,欢迎访问一九零五行业门户网

javascript移动设备Web开发中对touch事件的封装实例_javascript技巧

在触屏设备上,一些比较基础的手势都需要通过对 touch 事件进行二次封装才能实现。
zepto 是移动端上使用率比较高的一个类库,但是其 touch 模块模拟出来的一些事件存在一些兼容性问题,如 tap 事件在某些安卓设备上存在事件穿透的 bug,其他类型的事件也或多或少的存在一些兼容性问题。
于是乎,干脆自己动手对这些常用的手势事件进行了封装,由于没有太多真实的设备来进行测试,可能存在一些兼容性问题,下面的代码也只是在 ios 7、andorid 4 上的一些比较常见的浏览器中测试通过。
tap事件
tap 事件相当于 pc 浏览器中的 click 效果,虽然在触屏设备上 click 事件仍然可用,但是在很多设备上,click 会存在一些延迟,如果想要快速响应的 “click” 事件,需要借助 touch 事件来实现。
复制代码 代码如下:
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;
  // 在部分设备上 touch 事件比较灵敏,导致按下和松开手指时的事件坐标会出现一点点变化
  if( math.abs(starttx - endtx)     console.log( 'fire tap event' );
  }
}, false );
doubletap事件
doubletap 事件是当手指在相同位置范围内和极短的时间内两次敲击屏幕时触发的事件。在部分浏览器下,doubletap 事件会选中文本,如果不希望选中文本,可以给元素添加 user-select:none 的 css 属性。
复制代码 代码如下:
var istouchend = false,
  lasttime = 0,
  lasttx = null,
  lastty = null,
  firsttouchend = true,
  body = document.body,
  dtaptimer, starttx, startty, starttime;element.addeventlistener( 'touchstart', function( e ){
  if( dtaptimer ){
    cleartimeout( dtaptimer );
    dtaptimer = null;
  }
  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,
    now = date.now(),
    duration = now - lasttime;
  // 首先要确保能触发单次的 tap 事件
  if( math.abs(starttx - endtx)     // 两次 tap 的间隔确保在 500 毫秒以内
    if( duration       // 本次的 tap 位置和上一次的 tap 的位置允许一定范围内的误差
      if( lasttx !== null &&
        math.abs(lasttx - endtx)         math.abs(lastty - endty)
        firsttouchend = true;
        lasttx = lastty = null;
        console.log( 'fire double tap event' );
      }
    }
    else{
      lasttx = endtx;
      lastty = endty;
    }
  }
  else{
    firsttouchend = true;
    lasttx = lastty = null;
  }
  lasttime = now;
}, false );
// 在 ios 的 safari 上手指敲击屏幕的速度过快,
// 有一定的几率会导致第二次不会响应 touchstart 和 touchend 事件
// 同时手指长时间的touch不会触发click
if( ~navigator.useragent.tolowercase().indexof('iphone os') ){
  body.addeventlistener( 'touchstart', function( e ){
      starttime = date.now();
  }, true );
  body.addeventlistener( 'touchend', function( e ){
      var nolongtap = date.now() - starttime
      if( firsttouchend ){
          firsttouchend = false;
          if( nolongtap && e.target === element ){
              dtaptimer = settimeout(function(){
                  firsttouchend = true;
                  lasttx = lastty = null;
                  console.log( 'fire double tap event' );
              }, 400 );
          }
      }
      else{
          firsttouchend = true;
      }
  }, true );
// ios 上手指多次敲击屏幕时的速度过快不会触发 click 事件
element.addeventlistener( 'click', function( e ){
  if( dtaptimer ){
    cleartimeout( dtaptimer );
    dtaptimer = null;
    firsttouchend = true;
  }
}, false );
}
longtap事件
longtap 事件是当手指长时间按住屏幕保持不动时触发的事件。
复制代码 代码如下:
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事件
swipe 事件是当手指在屏幕上滑动后触发的事件,根据手指滑动的方向又分为 swipeleft (向左)、swiperight (向右)、swipeup (向上)、swipedown (向下)。
复制代码 代码如下:
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       console.log( 'fire swipe right event' );   
      isswipe = true;
    }
  }
  else{
    if( distancey > 20 ){
      console.log( 'fire swipe up event' );       
      isswipe = true;
    }
    else if( distancey       console.log( 'fire swipe down event' );        
      isswipe = true;
    }
  }
  if( isswipe ){
    console.log( 'fire swipe event' );
  }
}, false );
上面模拟的事件都封装在 monoevent 中了。完整代码地址:https://github.com/chenmnkken/monoevent,需要的朋友看看吧~
其它类似信息

推荐信息