-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackToTop.min.js
1 lines (1 loc) · 2.91 KB
/
backToTop.min.js
1
(function(b,a){if(typeof module==="object"&&typeof module.exports==="object"){module.exports=a(b)}else{a(b)}})(typeof window!=="undefined"?window:this,function(a){if(typeof $=="undefined"){throw new Error("Must be depencence Zepto, but not import or require");return}(function(j){var h={},d,m,i,b,f=750,n;function k(q,p,s,r){return Math.abs(q-p)>=Math.abs(s-r)?(q-p>0?"Left":"Right"):(s-r>0?"Up":"Down")}function o(){b=null;if(h.last){h.el.trigger("longTap");h={}}}function e(){if(b){clearTimeout(b)}b=null}function g(){if(d){clearTimeout(d)}if(m){clearTimeout(m)}if(i){clearTimeout(i)}if(b){clearTimeout(b)}d=m=i=b=null;h={}}function l(p){return(p.pointerType=="touch"||p.pointerType==p.MSPOINTER_TYPE_TOUCH)&&p.isPrimary}function c(q,p){return(q.type=="pointer"+p||q.type.toLowerCase()=="mspointer"+p)}j(document).ready(function(){var r,u,q=0,p=0,t,s;if("MSGesture" in a){n=new MSGesture();n.target=document.body}j(document).bind("MSGestureEnd",function(w){var v=w.velocityX>1?"Right":w.velocityX<-1?"Left":w.velocityY>1?"Down":w.velocityY<-1?"Up":null;if(v){h.el.trigger("swipe");h.el.trigger("swipe"+v)}}).on("touchstart MSPointerDown pointerdown",function(v){if((s=c(v,"down"))&&!l(v)){return}t=s?v:v.touches[0];if(v.touches&&v.touches.length===1&&h.x2){h.x2=undefined;h.y2=undefined}r=Date.now();u=r-(h.last||r);h.el=j("tagName" in t.target?t.target:t.target.parentNode);d&&clearTimeout(d);h.x1=t.pageX;h.y1=t.pageY;if(u>0&&u<=250){h.isDoubleTap=true}h.last=r;b=setTimeout(o,f);if(n&&s){n.addPointer(v.pointerId)}}).on("touchmove MSPointerMove pointermove",function(v){if((s=c(v,"move"))&&!l(v)){return}t=s?v:v.touches[0];e();h.x2=t.pageX;h.y2=t.pageY;q+=Math.abs(h.x1-h.x2);p+=Math.abs(h.y1-h.y2)}).on("touchend MSPointerUp pointerup",function(v){if((s=c(v,"up"))&&!l(v)){return}e();if((h.x2&&Math.abs(h.x1-h.x2)>30)||(h.y2&&Math.abs(h.y1-h.y2)>30)){i=setTimeout(function(){if(h.el){h.el.trigger("swipe");h.el.trigger("swipe"+(k(h.x1,h.x2,h.y1,h.y2)))}h={}},0)}else{if("last" in h){if(q<30&&p<30){m=setTimeout(function(){var w=j.Event("tap");w.cancelTouch=g;if(h.el){h.el.trigger(w)}if(h.isDoubleTap){if(h.el){h.el.trigger("doubleTap")}h={}}else{d=setTimeout(function(){d=null;if(h.el){h.el.trigger("singleTap")}h={}},250)}},0)}else{h={}}}}q=p=0}).on("touchcancel MSPointerCancel pointercancel",g);j(a).on("scroll",g)});["swipe","swipeLeft","swipeRight","swipeUp","swipeDown","doubleTap","tap","singleTap","longTap"].forEach(function(p){j.fn[p]=function(q){return this.on(p,q)}})})(Zepto);$.fn.backToTop=function(d){var b={type:"tap",target:0,duration:1000,easing:"ease-out"};var c=$.extend(b,d);this.on(c.type,function(){var f=Math.floor(c.duration/30);var j=document.body.scrollTop;var g=c.target;var e=g-j;var i=0;var h=setInterval(function(){i++;switch(c.easing){case"linear":break;case"ease-in":var k=i/f;var l=j+e*k*k*k;break;case"ease-out":var k=1-i/f;var l=j+e*(1-k*k*k);break}document.body.scrollTop=l;if(i==f){clearInterval(h)}},30)})};return $.fn.backToTop});