-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtouch-scroll.min.js
1 lines (1 loc) · 3.74 KB
/
touch-scroll.min.js
1
(function(b){var c={y:0,scrollHeight:0,elastic:!navigator.userAgent.match(/android/i),momentum:true,elasticDamp:0.6,elasticTime:50,reboundTime:400,momentumDamp:0.9,momentumTime:300,iPadMomentumDamp:0.95,iPadMomentumTime:1200};var a={init:function(d){return this.each(function(){if(!!this._init){return this.update()}this._init=true;var p=b(this),A=b.extend(c,d),D=-A.y,s=0,t=0,l=0,x=0,B=0,H=0,v=false,f=false,n=false,K,y=!!navigator.platform.match(/ipad/i),k="WebKitCSSMatrix" in window,u=k&&"m11" in new WebKitCSSMatrix();var r=this.update=function(){x=p.height();if(A.scrollHeight){H=A.scrollHeight}else{if(p.prop){H=p.prop("scrollHeight")}else{H=p.attr("scrollHeight")}}if(H<x){H=x}B=x-H;clearTimeout(K);z(false)};r();p.css({"-webkit-transition-property":"-webkit-transform","-webkit-transition-timing-function":"cubic-bezier(0,0,0.2,1)","-webkit-transition-duration":"0","-webkit-transform":j(D)});window.addEventListener("onorientationchange" in window?"orientationchange":"resize",r,false);p.bind("touchstart.touchScroll",e);p.bind("touchmove.touchScroll",G);p.bind("touchend.touchScroll touchcancel.touchScroll",i);p.bind("webkitTransitionEnd.touchScroll",q);var h=this.setPosition=function(o){D=o;p.css("-webkit-transform",j(D))};function j(o){return"translate"+(u?"3d(0,":"(0,")+o+"px"+(u?",0)":")")}function w(o){o=o||"0";p.css("-webkit-transition-duration",o+"ms")}function J(){if(k){var L=window.getComputedStyle(p[0]).webkitTransform;if(!!L&&L!=="none"){var o=new WebKitCSSMatrix(L);return o.f}}return D}this.getPosition=function(){return J()};function C(){if(D>0){g(0,A.reboundTime)}else{if(D<B){g(B,A.reboundTime)}}}function q(){if(f){f=false;C()}clearTimeout(K)}function z(L){if(!k||f){return}var o=l;l=J();if(l>0){if(A.elastic){f=true;D=0;F(l-o,A.elasticDamp,1,x,A.elasticTime)}else{w(0);h(0)}}else{if(l<B){if(A.elastic){f=true;D=B;F(l-o,A.elasticDamp,1,x,A.elasticTime)}else{w(0);h(B)}}else{if(L){K=setTimeout(z,20,true)}}}}function g(o,L){if(o===D){return}n=true;w(L);h(o)}function F(R,M,L,O,N){var P=Math.abs(R),o=0;while(P>0.1){P*=M;o+=P}if(o>O){o=O}if(o>L){if(R<0){o=-o}o+=D;if(x>0){if(o>x*2){var Q=o;o=x*2}else{if(o<B-x*2){o=B-x*2}}}g(Math.round(o),N)}z(true)}function E(o){if(o.originalEvent){if(o.originalEvent.touches&&o.originalEvent.touches.length){return o.originalEvent.touches}else{if(o.originalEvent.changedTouches&&o.originalEvent.changedTouches.length){return o.originalEvent.changedTouches}}}return o.touches}function m(o,N,M){var L=document.createEvent("MouseEvent");L.initMouseEvent(o,true,true,N.view,1,N.screenX,N.screenY,N.clientX,N.clientY,false,false,false,false,0,null);M.dispatchEvent(L)}function I(o){while(o.nodeType!==1){o=o.parentNode}return o}function e(o){if(o.target.tagName==="SELECT"||o.target.tagName==="INPUT"){return}o.preventDefault();o.stopPropagation();var M=E(o)[0];m("mousedown",M,I(M.target));v=true;n=false;t=0;clearTimeout(K);w(0);if(A.momentum){var L=J();if(L!==D){h(L);n=true}}s=M.pageY-D}function G(L){if(!v){return}var o=E(L)[0].pageY-s;if(o>0){if(A.elastic){o/=2}else{o=0}}else{if(o<B){if(A.elastic){o=(o+B)/2}else{o=B}}}t=o-D;n=true;h(o)}function i(L){if(!v){return}v=false;if(n){if(D>0||D<B){C()}else{if(A.momentum){F(t,y?A.iPadMomentumDamp:A.momentumDamp,40,2000,y?A.iPadMomentumTime:A.momentumTime)}}}else{var M=E(L)[0],o=I(M.target);m("mouseup",M,o);m("click",M,o)}}})},update:function(){return this.each(function(){this.update()})},getPosition:function(){var d=[];this.each(function(){d.push(-this.getPosition())});return d},setPosition:function(d){return this.each(function(){this.setPosition(-d)})}};b.fn.touchScroll=function(d){if(a[d]){return a[d].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof d==="object"||!d){return a.init.apply(this,arguments)}else{b.error("Method "+d+" does not exist on jQuery.touchScroll")}}}})(jQuery);