You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10 lines
1.4 KiB
JavaScript

2 years ago
/**
* 移动端触摸滑动
* @ author 鹿文学
* @ date 2017年5月19日
* 必须先引入 jQuery
* 需要滑动的控件例如 elem 需加类名 touch; 例如使用 slider.init('.elem')
*/
;var slider=function(b,c){c.touch=function(){return"ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch};c.init=function(a){a=b(a);c.touch()&&b.each(a,function(a,b){b.addEventListener("touchstart",c.events,!1)})};c.events={goal:null,endPos:{x:0,y:0},handleEvent:function(a){target=a.target;b(target).hasClass("touch")?this.goal=target:this.goal=b(target).closest(".touch")[0];"touchstart"==a.type?this.start(a):"touchmove"==a.type?this.move(a):"touchend"==a.type&&this.end(a)},
start:function(a){a=a.targetTouches[0];startPos={x:a.pageX,y:a.pageY,time:+new Date};isScrolling=0;this.goal.addEventListener("touchmove",this,!1);this.goal.addEventListener("touchend",this,!1)},move:function(a){if(!(1<a.targetTouches.length||a.scale&&1!==a.scale)){var b=a.targetTouches[0];this.endPos={x:b.pageX-startPos.x,y:b.pageY-startPos.y};0===isScrolling&&a.preventDefault()}},end:function(a){a=this.endPos;Math.abs(a.x)<Math.abs(a.y)&&(isScrolling=1);0==isScrolling&&(10<a.x?b(this.goal).removeClass("cur"):
-10>a.x&&b(this.goal).addClass("cur"));this.goal.removeEventListener("touchmove",this,!1);this.goal.removeEventListener("touchend",this,!1)}};return c}(jQuery,window.slider||{});