麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > WEB開發 > 正文

移動端touch事件

2024-04-27 15:19:30
字體:
來源:轉載
供稿:網友
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); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美性受xxxxxx黑人xyx性爽 | 成人毛片视频免费 | 五月天影院,久久综合, | 精品亚洲夜色av98在线观看 | 一本一道久久久a久久久精品91 | 九九精品在线播放 | 特级毛片a级毛片100免费 | 国产一级大片在线观看 | 精品呦女| 综合在线一区 | 亚洲第一精品在线 | 日韩视频在线视频 | 久久精品日产第一区二区三区 | 国产高潮失禁喷水爽到抽搐视频 | 久久久www免费看片 亚洲综合视频一区 | 成年人黄色免费网站 | 男人的天堂色偷偷 | 92看片淫黄大片欧美看国产片 | 免费播放av | 在线成人免费观看视频 | 亚洲av一级毛片特黄大片 | 春光影院理论片 | 国产成人在线网站 | 麻豆自拍偷拍视频 | 成人 日韩 | 久久精品一区二区三区四区五区 | 亚洲男人的天堂在线视频 | 黄在线免费 | 性欧美极品xxxx欧美一区二区 | 97香蕉超级碰碰久久免费软件 | 最新av网址在线观看 | 亚洲福利在线免费观看 | 久久情爱网 | 中国7777高潮网站 | 一区二区三区欧美在线观看 | 午夜热门福利 | 国产亚洲精品久久久久久网站 | 黄视频网站免费观看 | 久久精品视频8 | 国产成人免费精品 | 天天干天天透 |