移动端touch事件

var touchEvent = {}

touchEvent =function() {
    var self=this;
    /*单次触摸事件*/
    self.tap=function(element, fn) {
        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) < 6 && Math.abs(startTy - endTy) < 6) {
                fn();
            }
        }, false);
    },

    /*两次触摸事件*/
    self.doubleTap=function(element, fn) {
        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) < 6 && Math.abs(startTx - endTx) < 6) {
                // 两次 tap 的间隔确保在 500 毫秒以内
                if(duration < 301) {
                    // 本次的 tap 位置和上一次的 tap 的位置允许一定范围内的误差
                    if(lastTx !== null &&
                        Math.abs(lastTx - endTx) < 45 &&
                        Math.abs(lastTy - endTy) < 45) {
                        firstTouchEnd = true;
                        lastTx = lastTy = null;
                        fn();
                    }
                } 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 < 501;
                if(firstTouchEnd) {
                    firstTouchEnd = false;
                    if(noLongTap && e.target === element) {
                        dTapTimer = setTimeout(function() {
                            firstTouchEnd = true;
                            lastTx = lastTy = null;
                            fn();
                        }, 400);
                    }
                } else {
                    firstTouchEnd = true;
                }
            }, true);
            // iOS 上手指多次敲击屏幕时的速度过快不会触发 click 事件
            element.addEventListener('click', function(e) {
                if(dTapTimer) {
                    clearTimeout(dTapTimer);
                    dTapTimer = null;
                    firstTouchEnd = true;
                }
            }, false);
        }
    },

    /*长按事件*/
    self.longTap=function(element, fn) {
        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() {
                fn();
            }, 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);
    },

    /*滑屏事件*/
    self.swipe= function(element, fn) {
        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) > 20 || Math.abs(distanceY) > 20) {
                fn();
            }
        }, false);
    },

    /*向上滑动事件*/
    self.swipeUp=function(element, fn) {
        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(distanceY > 20) {
                    fn();
                    isSwipe = true;
                }
            }
        }, false);
    },

    /*向下滑动事件*/
    self.swipeDown= function(element, fn) {
        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(distanceY < -20) {
                    fn();
                    isSwipe = true;
                }
            }
        }, false);
    },

    /*向左滑动事件*/
    self.swipeLeft= function(element, fn) {
        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) {
                    fn();
                    isSwipe = true;
                }
            }
        }, false);
    },

    /*向右滑动事件*/
    self.swipeRight= function(element, fn) {
        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) {
                    fn();
                    isSwipe = true;
                }
            }
        }, false);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现移动端touch事件的横向滑动列表效果可以使用原生的JavaScript和CSS3来实现。 首先,我们需要在HTML中创建一个容器元素,用来包含列表项。容器元素需要设置overflow-x属性为scroll,使得内容超出容器范围时可以滚动。 ```html <div class="container"> <ul class="list"> <li>项1</li> <li>项2</li> <li>项3</li> <li>项4</li> <li>项5</li> </ul> </div> ``` 然后,在CSS中,我们需要设置容器元素和列表项的样式,以及使用CSS3的transition属性来实现平滑的过渡效果。 ```css .container { width: 100%; overflow-x: scroll; -webkit-overflow-scrolling: touch; /* 添加iOS滚动效果 */ } .list { display: flex; flex-wrap: nowrap; /* 设置列表项不换行 */ transition: transform 0.3s ease; /* 添加平滑的过渡效果 */ } .list li { width: 100px; height: 100px; margin-right: 10px; background-color: #ccc; } ``` 最后,在JavaScript中,我们需要监听容器元素的touchstart、touchmove和touchend事件,计算滑动距离并通过改变列表项的transform属性来实现横向滑动效果。 ```javascript const container = document.querySelector('.container'); const list = document.querySelector('.list'); let isDragging = false; let startPosition = 0; let currentTranslate = 0; let prevTranslate = 0; let animationId = 0; container.addEventListener('touchstart', touchStart); container.addEventListener('touchmove', touchMove); container.addEventListener('touchend', touchEnd); container.addEventListener('touchcancel', touchEnd); function touchStart(event) { if (event.target.classList.contains('list')) { isDragging = true; startPosition = event.touches[0].clientX; animationId = requestAnimationFrame(updateAnimation); container.classList.add('grabbing'); } } function touchMove(event) { if (isDragging) { const currentPosition = event.touches[0].clientX; currentTranslate = prevTranslate + currentPosition - startPosition; } } function touchEnd() { isDragging = false; cancelAnimationFrame(animationId); prevTranslate = currentTranslate; container.classList.remove('grabbing'); } function updateAnimation() { list.style.transform = `translateX(${currentTranslate}px)`; animationId = requestAnimationFrame(updateAnimation); } ``` 通过以上代码,我们就成功地实现了移动端touch事件的横向滑动列表效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值