手持设备响应速度优化之-click和touch

手持上click事件的响应速度远远不如touchstart。但是仅仅写touch事件很不方便web端开发测试,所以下边的判断很有必要。

var isSupportTouch = "ontouchend" in document ? true : false,
    touchEv = isSupportTouch ? 'touchstart' : 'mousedown',
    touchEndEv = isSupportTouch ? "touchend" : 'mouseup';

另外:

var isSupportTouch = "ontouchend" in document ? true : false,
    touchEv = isSupportTouch ? 'touchstart' : 'click';

用起来就很方便啦

$obj.on(touchEv,function(){
    \\事件
});

还有一种写法:

isTouch = !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/);,
eStart = isTouch ? 'touchstart'  : 'mousedown',
eMove = isTouch ? 'touchmove'   : 'mousemove',
eEnd  = isTouch ? 'touchend'    : 'mouseup';
    原文作者:dada86
    原文地址: https://segmentfault.com/a/1190000002483799
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞