zepto源码之touch.js

一、前言

touch.js主要提供滑动(swipe)与点击(tap:模拟click)的事件封装,针对手机常用浏览器(touchstart,touchmove,touchend)和IE10(msPointDown)
的触摸事件兼容处理以及手势的事件处理。

二、源码

    //touch事件 在触摸设备上触发tap–和swipe–相关事件。这适用于所有的`touch`(iOS, Android)和`pointer`事件(Windows Phone)。
    (function ($) {
   
        var touch = {},
            //延迟触发(ms): 250, 0, 0, 750
            touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,
            longTapDelay = 750,
            gesture;

        //判断滑动方向,返回Left, Right, Up, Down
        function swipeDirection(x1, x2, y1, y2) {
   
            return Math.abs(x1 - x2) >=
            Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
        }

        //长按
        function longTap() {
   
            //定时器执行了没有清空的必要
            longTapTimeout = null;
            if (touch.last) {
                touch.el.trigger('longTap');
                touch = {}
            }
        }

        //取消长按
        function cancelLongTap() {
   
            if (longTapTimeout) clearTimeout(longTapTimeout);
            longTapTimeout = null
        }

        //取消所有
        function cancelAll() {
   
            if (touchTimeout) clearTimeout(touchTimeout);
            if (tapTimeout) clearTimeout(tapTimeout);
            if (swipeTimeout) clearTimeout(swipeTimeout);
            if (longTapTimeout) clearTimeout(longTapTimeout);
            touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null;
            touch = {}
        }

        //判断是否是点击指针是否为主指针(http://www.ayqy.net/blog/html5%E8%A7%A6%E6%91%B8%E4%BA%8B%E4%BB%B6/)
        function isPrimaryTouch(event) {
   
            return (event.pointerType == 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值