swipe_wev8.js 5.14 KB
function Swipe(e,r,i){"use strict";var t=function(){},s=function(n){setTimeout(n||t,0)},a={addEventListener:!!window.addEventListener,touch:"ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch,transitions:function(n){var t=["transitionProperty","WebkitTransition","MozTransition","OTransition","msTransition"];for(var e in t)if(void 0!==n.style[t[e]])return!0;return!1}(document.createElement("swipe"))};if(e){var u,c,d,o,l=e.children[0];r=r||{};var v=parseInt(r.startSlide,10)||0,h=r.speed||300;r.continuous=void 0===r.continuous||r.continuous;var n,f,w=r.auto||0,E={},p={},m={handleEvent:function(n){switch(n.type){case"touchstart":this.start(n);break;case"touchmove":this.move(n);break;case"touchend":s(this.end(n));break;case"webkitTransitionEnd":case"msTransitionEnd":case"oTransitionEnd":case"otransitionend":case"transitionend":s(this.transitionEnd(n));break;case"resize":s(x)}r.stopPropagation&&n.stopPropagation()},start:function(n){var t=n.touches[0];E={x:t.pageX,y:t.pageY,time:+new Date},f=void 0,p={},l.addEventListener("touchmove",this,!1),l.addEventListener("touchend",this,!1)},move:function(n){if(!(1<n.touches.length||n.scale&&1!==n.scale)){r.disableScroll&&n.preventDefault();var t=n.touches[0];p={x:t.pageX-E.x,y:t.pageY-E.y},void 0===f&&(f=!!(f||Math.abs(p.x)<Math.abs(p.y))),f||(n.preventDefault(),D(),r.continuous?(L(g(v-1),p.x+c[g(v-1)],0),L(v,p.x+c[v],0),L(g(v+1),p.x+c[g(v+1)],0)):(p.x=p.x/(!v&&0<p.x||v==u.length-1&&p.x<0?Math.abs(p.x)/d+1:1),L(v-1,p.x+c[v-1],0),L(v,p.x+c[v],0),L(v+1,p.x+c[v+1],0)))}},end:function(n){var t=+new Date-E.time,e=Number(t)<250&&20<Math.abs(p.x)||Math.abs(p.x)>d/2,i=!v&&0<p.x||v==u.length-1&&p.x<0;r.continuous&&(i=!1);var o=p.x<0;f||(e&&!i?(v=o?(r.continuous?(y(g(v-1),-d,0),y(g(v+2),d,0)):y(v-1,-d,0),y(v,c[v]-d,h),y(g(v+1),c[g(v+1)]-d,h),g(v+1)):(r.continuous?(y(g(v+1),d,0),y(g(v-2),-d,0)):y(v+1,d,0),y(v,c[v]+d,h),y(g(v-1),c[g(v-1)]+d,h),g(v-1)),r.callback&&r.callback(v,u[v])):r.continuous?(y(g(v-1),-d,h),y(v,0,h),y(g(v+1),d,h)):(y(v-1,-d,h),y(v,0,h),y(v+1,d,h))),l.removeEventListener("touchmove",m,!1),l.removeEventListener("touchend",m,!1)},transitionEnd:function(n){parseInt(n.target.getAttribute("data-index"),10)==v&&(w&&k(),r.transitionEnd&&r.transitionEnd.call(n,v,u[v]))}};return x(),w&&k(),a.addEventListener?(a.touch&&l.addEventListener("touchstart",m,!1),a.transitions&&(l.addEventListener("webkitTransitionEnd",m,!1),l.addEventListener("msTransitionEnd",m,!1),l.addEventListener("oTransitionEnd",m,!1),l.addEventListener("otransitionend",m,!1),l.addEventListener("transitionend",m,!1)),window.addEventListener("resize",m,!1)):window.onresize=function(){x()},{setup:function(){x()},slide:function(n,t){D(),T(n,t)},prev:function(){D(),r.continuous?T(v-1):v&&T(v-1)},next:function(){D(),b()},stop:function(){D()},getPos:function(){return v},getNumSlides:function(){return o},restart:function(){D(),k()},kill:function(){D(),l.style.width="",l.style.left="";for(var n=u.length;n--;){var t=u[n];t.style.width="",t.style.left="",a.transitions&&L(n,0,0)}a.addEventListener?(l.removeEventListener("touchstart",m,!1),l.removeEventListener("webkitTransitionEnd",m,!1),l.removeEventListener("msTransitionEnd",m,!1),l.removeEventListener("oTransitionEnd",m,!1),l.removeEventListener("otransitionend",m,!1),l.removeEventListener("transitionend",m,!1),window.removeEventListener("resize",m,!1)):window.onresize=null}}}function x(){u=l.children,o=u.length,u.length<2&&(r.continuous=!1),a.transitions&&r.continuous&&u.length<3&&(l.appendChild(u[0].cloneNode(!0)),l.appendChild(l.children[1].cloneNode(!0)),u=l.children),c=new Array(u.length),d=r.width||e.getBoundingClientRect().width||e.offsetWidth,l.style.width=u.length*d+"px";for(var n=u.length;n--;){var t=u[n];t.style.width=d+"px",t.setAttribute("data-index",n),a.transitions&&(t.style.left=n*-d+"px",y(n,n<v?-d:v<n?d:0,0))}r.continuous&&a.transitions&&(y(g(v-1),-d,0),y(g(v+1),d,0)),a.transitions||(l.style.left=v*-d+"px"),e.style.visibility="visible",s(i)}function b(){r.continuous?T(v+1):v<u.length-1&&T(v+1)}function g(n){return(u.length+n%u.length)%u.length}function T(n,t){if(v!=n){if(a.transitions){var e=Math.abs(v-n)/(v-n);if(r.continuous){var i=e;(e=-c[g(n)]/d)!==i&&(n=-e*u.length+n)}for(var o=Math.abs(v-n)-1;o--;)y(g((v<n?n:v)-o-1),d*e,0);n=g(n),y(v,d*e,t||h),y(n,0,t||h),r.continuous&&y(g(n-e),-d*e,0)}else n=g(n),function(t,e,i){if(!i)return l.style.left=e+"px";var o=+new Date,s=setInterval(function(){var n=+new Date-o;if(i<n)return l.style.left=e+"px",w&&k(),r.transitionEnd&&r.transitionEnd.call(event,v,u[v]),void clearInterval(s);l.style.left=(e-t)*(Math.floor(n/i*100)/100)+t+"px"},4)}(v*-d,n*-d,t||h);v=n,s(r.callback&&r.callback(v,u[v]))}}function y(n,t,e){L(n,t,e),c[n]=t}function L(n,t,e){var i=u[n],o=i&&i.style;o&&(o.webkitTransitionDuration=o.MozTransitionDuration=o.msTransitionDuration=o.OTransitionDuration=o.transitionDuration=e+"ms",o.webkitTransform="translate("+t+"px,0)translateZ(0)",o.msTransform=o.MozTransform=o.OTransform="translateX("+t+"px)")}function k(){n=setTimeout(b,w)}function D(){clearTimeout(n)}}(window.jQuery||window.Zepto)&&function(t){t.fn.Swipe=function(n){return this.each(function(){t(this).data("Swipe",new Swipe(t(this)[0],n))})}}(window.jQuery||window.Zepto);