当前位置: 首页 > Web前端 > HTML5

完整版移动端滑动事件包

时间:2023-04-05 17:34:59 HTML5

touchEvent是基于jquery扩展在移动端产生的事件,包括单点触摸事件,双点触摸事件,长按事件,滑动事件,上滑事件,下滑事件,左滑事件,右滑事件预览地址预览https://hangjob.github.io/touchEvent/index.html事件类型单点触摸事件$(el).taptap:function(element,fn){varstartTx,startTy;v_on(element,'touchstart',function(e){vartouches=e.touches[0];startTx=touches.clientX;startTy=touches.clientY;},false);v_on(element,'touchend',function(e){vartouches=e.changedTouches[0],endTx=touches.clientX,endTy=touches.clientY;//在某些设备上,触摸事件比较敏感,导致如果(Math.abs(startTx-endTx)<6&&Math.abs(startTy-endTy)<6){fn();}},false);}双触摸事件$(el).doubleTapdoubleTap:function(element,fn){varisTouchEnd=false,lastTime=0,lastTx=null,lastTy=null,firstTouchEnd=true,body=document.body,dTapTimer,startTx,startTy,startTime;v_on(element,'touchstart',function(e){if(dTapTimer){clearTimeout(dTapTimer);dTapTimer=null;}vartouches=e.touches[0];startTx=touches.clientX;startTy=touches.clientY;},错误的);v_on(element,'touchend',function(e){vartouches=e.changedTouches[0],endTx=touches.clientX,endTy=touches.clientY,now=Date.now(),duration=now-lastTime;//首先确保单次点击事件可以被触发if(Math.abs(startTx-endTx)<6&&Math.abs(startTx-endTx)<6){//两次点击的间隔保证在500毫秒if(duration<301){//本次tap的位置和上一次tap的位置允许有一定范围的误差if(lastTx!==null&&Math.abs(lastTx-endTx)<45&&Math.abs(lastTy-endTy)<45){firstTouchEnd=true;lastTx=lastTy=空;fn();}}else{lastTx=endTx;lastTy=endTy;}}else{firstTouchEnd=true;lastTx=lastTy=空;}lastTime=现在;},错误的);//在iOSsafari上,手指点击屏幕的速度过快,//有一定几率会导致秒不会响应touchstart和touchend事件//同时,手指长时间触摸也不会触发点击if(~navigator.userAgent.toLowerCase().indexOf('iphoneos')){v_on(body,'touchstart',function(e){startTime=Date.now();},true);v_on(body,'touchend',function(e){varnoLongTap=Date.now()-startTime<501;如果(firstTouchEnd){firstTouchEnd=false;if(noLongTap&&e.target===element){dTapTimer=setTimeout(function(){firstTouchEnd=true;lastTx=lastTy=null;fn();},400);}}else{firstTouchEnd=true;}},真的);//在iOS上,当手指多次快速点击屏幕时,不会触发点击事件v_on(element,'click',function(e){if(dTapTimer){clearTimeout(dTapTimer);dTapTimer=null;firstTouchEnd=true;}},false);}}长按事件$(el).longTaplongTap:function(element,fn){varstartTx,startTy,lTapTimer;v_on(element,'touchstart',function(e){if(lTapTimer){清除超时(lTapTimer);lTapTimer=null;}vartouches=e.touches[0];startTx=touches.clientX;startTy=touches.clientY;lTapTimer=setTimeout(function(){fn(startTx,startTy);},1000);},错误的);v_on(element,'touchmove',function(e){vartouches=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);v_on(element,'touchend',function(e){if(lTapTimer){clearTimeout(lTapTimer);lTapTimer=null;}},false);}滑动屏幕事件$(el).向上滑动事件$(el).swipeUp向下滑动事件$(el).swipeDown向左滑动事件$(el).swipeLeft向右滑动事件$(el).swipeRight$.fn.extend()extendsjQuery.fn是jQuery的原型对象,它的extend()方法用于给jQuery的原型添加新的属性和方法。这些方法可以在jQuery实例对象上调用jQuery.fn.extend({tap:function(fn){returntouchEvent.tap(jQuery(this)[0],fn);},doubleTap:function(fn){returntouchEvent.doubleTap(jQuery(this)[0],fn);},longTap:function(fn){returntouchEvent.longTap(jQuery(this)[0],fn);},swipe:function(fn){returntouchEvent.swipe(jQuery(this)[0],fn);},swipeLeft:function(fn){returntouchEvent.swipeLeft(jQuery(this)[0],fn);},swipeRight:function(fn){返回touchEvent.swipeRight(jQuery(this)[0],fn);},swipeUp:function(fn){returntouchEvent.swipeUp(jQuery(this)[0],fn);},swipeDown:function(fn){返回touchEvent.swipeDown(jQuery(this)[0],fn);}});如何使用$('.container').swipeRight((res)=>{ulDom.append(createLi(`向右滑动${res}px`));})$('.container').swipe((x,y)=>{ulDom.append(createLi(`滑屏事件,X轴滑动${x}px,Y轴滑动${y}px`));})...Githubhttps://github.com/hangjob/touchEvent如If有问题扫描公众号,回复:加群https://www.vipbic.com/weixin.html