forked from yuya/flickable-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickable.min.js
3 lines (3 loc) · 13.4 KB
/
flickable.min.js
1
2
3
// Flickable.js 0.1.7 Copyright (c) 2013 @yuya
// See https://github.com/yhmt/flickable-js
(function(){var t,e,i,s,o,r;i="Flickable",o=this,s=null!=(r="undefined"!=typeof window&&null!==window?window:s)?r:this,s[i]=null,e=function(){function t(){this.div=document.createElement("div"),this.prefixes=["webkit","moz","o","ms"],this.saveProp={}}return t.prototype.getPage=function(t,e){return t.changedTouches?t.changedTouches[0][e]:t[e]},t.prototype.hasProp=function(t){var e,i,s;if(t instanceof Array){for(i=0,s=t.length;s>i;i++)if(e=t[i],void 0!==this.div.style[e])return!0;return!1}if("string"==typeof t)return void 0!==this.div.style[e]?!0:!1;throw new TypeError("Must be a Array or String")},t.prototype.selector=function(t,e){var i,s;return e=e||document,s=/^(.+[\#\.\s\[\*>:,]|[\[:])/,i=t.substring(1,t.length),s.test(t)?e.querySelectorAll(t):"#"===t[0]?e.getElementById(i):"."===t[0]?e.getElementsByClassName(i):e.getElementsByTagName(t)},t.prototype.setStyle=function(t,e){var i,s,o,r,n,h=this;o=t.style,i=this.saveProp[s],n=function(t,e,s){var o,r,n,a,u;if(i)t[i]=s;else{if(void 0===t[e]){for(u=h.prefixes,r=0,n=u.length;n>r;r++)if(o=u[r],a=h.ucFirst(o)+h.ucFirst(e),void 0!==t[a])return h.saveProp[e]=a,t[a]=s,!0;return!1}h.saveProp[e]=e,t[e]=s}},r=[];for(s in e)r.push(n(o,s,e[s]));return r},t.prototype.getCSSVal=function(t){var e,i,s,o,r,n;if("string"!=typeof t)throw new TypeError("Must be a String");if(void 0!==this.div.style[t])return t;for(n=this.prefixes,s=0,o=n.length;o>s;s++)e=n[s],r=this.ucFirst(e)+this.ucFirst(t),void 0!==this.div.style[r]&&(i="-"+e+"-"+t);return i},t.prototype.ucFirst=function(t){if("string"!=typeof t)throw new TypeError("Must be a String");return t.charAt(0).toUpperCase()+t.substr(1)},t.prototype.triggerEvent=function(t,e,i,s,o){var r,n;if("object"!=typeof t)throw Error("Must be a Element");if(n=document.createEvent("Event"),n.initEvent(e,i,s),o)for(r in o)n[r]=o[r];return t.dispatchEvent(n)},t.prototype.checkBrowser=function(){var t,e,i,s,o;return o=navigator.userAgent.toLowerCase(),s=o.match(/(?:iphone\sos|ip[oa]d.*os)\s([\d_]+)/),t=o.match(/(android)\s+([\d.]+)/),e=s?"ios":t?"android":"pc",i=function(){return s||t?parseFloat((s||t).pop().split(/\D/).join(".")):null}(),{name:e,version:i,isLegacy:!!t&&3>i}},t.prototype.checkSupport=function(){var t,e,i;return e=this.hasProp(["perspectiveProperty","WebkitPerspective","MozPerspective","msPerspective","OPerspective"]),t=this.hasProp(["transformProperty","WebkitTransform","MozTransform","msTransform","OTransform"]),i=this.hasProp(["transitionProperty","WebkitTransitionProperty","MozTransitionProperty","msTransitionProperty","OTransitionProperty"]),{touch:"ontouchstart"in window,eventListener:"addEventListener"in window,transform3d:e,transform:t,transition:i,cssAnimation:e||t&&i?!0:!1}},t.prototype.checkTouchEvents=function(){var t;return t=this.checkSupport().touch,{start:t?"touchstart":"mousedown",move:t?"touchmove":"mousemove",end:t?"touchend":"mouseup"}},t.prototype.getDeviceWidth=function(){return window.innerWidth},t.prototype.getParentNodeWidth=function(t){if(void 0===t)throw Error("Element Not Found");return t.parentNode.offsetWidth},t.prototype.getElementWidth=function(t){var e,i,s,o,r,n,h;if(void 0===t)throw Error("Element Not Found");return s=window.getComputedStyle(t),i=void 0,o=function(){var e,s,o,r;for(s=["-webkit-box-sizing","-moz-box-sizing","-o-box-sizing","-ms-box-sizing","box-sizing"],o=0,r=s.length;r>o;o++)if(e=s[o],void 0!==t.style[e])return i=t.style[e],!0;return!1}(),o&&"content-box"!==i?0===t.scrollWidth?(h=parseFloat(t.style.width.match(/\d+/)),t.style.boxSizing&&t.style.webkitBoxSizing||(t.style.paddingRight&&(h+=parseFloat(t.style.paddingRight.match(/\d+/))),t.style.paddingLeft&&(h+=parseFloat(t.style.paddingLeft.match(/\d+/)))),h):h=t.scrollWidth:(n=function(t){var e,i,o,r,n,h;for(r=[],o=0,e=n=0,h=t.length;h>n;e=++n)i=t[e],s[i]&&(r[e]=parseFloat(s[t[0]].match(/\d+/)),o+=r[e]);return o},e=n(["border-right-width","border-left-width"]),r=n(["padding-right","padding-left"]),h=t.scrollWidth+e+r)},t.prototype.getTranslate=function(t,e,i,s){return null==t&&(t=!0),null==i&&(i=0),null==s&&(s=0),this.opts.use3d?"translate3d("+e+"px, 0, 0)":"translate("+e+"px, 0)"},t.prototype.getTransitionEndEventName=function(){var t,e,i,s;switch(i=navigator.userAgent.toLowerCase(),e=/(webkit)[ \/]([\w.]+)/.exec(i||/(firefox)[ \/]([\w.]+)/.exec(i||/(msie) ([\w.]+)/.exec(i||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(i||[])))),t=e[1],s=parseFloat(e[2]),"msie"===t&&s>=10&&(t="modernIE"),t){case"webkit":return"webkitTransitionEnd";case"opera":return"oTransitionEnd";case"firefox":case"modernIE":return"transitionend";default:return void 0}},t}(),o.Helper=e,t=function(){function t(t,e,i){var s,r=this;if(!t)throw Error("Element Not Found");if("object"==typeof t&&t.length&&(t=t[0]),this.helper=new o.Helper,this.el="string"==typeof t?this.helper.selector(t):t,this.opts=e||{},this.browser=this.helper.checkBrowser(),this.support=this.helper.checkSupport(),this.events=this.helper.checkTouchEvents(),this.opts.use3d=this.opts.disable3d?!1:this.support.transform3d,this.opts.useJsAnimate=!1,this.opts.disableTouch=this.opts.disableTouch||!1,this.opts.disable3d=this.opts.disable3d||!1,this.opts.setWidth=this.opts.setWidth||!0,this.opts.fitWidth=this.opts.fitWidth||!1,this.opts.autoPlay=this.opts.autoPlay||!1,this.opts.interval=this.opts.interval||6600,this.opts.clearInterval=this.opts.clearInterval||this.opts.interval/2,this.opts.loop=this.opts.loop||(this.opts.autoPlay?!0:!1),this.opts.transition=this.opts.transition||{},this.opts.transition={timingFunction:this.opts.transition.timingFunction||"cubic-bezier(0.23, 1, 0.32, 1)",duration:function(){return r.opts.transition.duration||(r.browser.isLegacy?"200ms":"330ms")}()},this.currentPoint=this.opts.currentPoint||0,this.childElementCount=this.el.childElementCount,this.maxPoint=this.currentX=this.maxX=0,this.gestureStart=this.moveReady=this.scrolling=this.didCloneNode=!1,this.startTime=this.timerId=this.basePageX=this.startPageX=this.startPageY=this.distance=this.visibleSize=null,this.support.cssAnimation&&!this.browser.isLegacy?this.helper.setStyle(this.el,{transitionProperty:this.helper.getCSSVal("transform"),transitionDuration:"0ms",transitionTimingFunction:this.opts.transition.timingFunction,transform:this._getTranslate(0)}):this.browser.isLegacy?this.helper.setStyle(this.el,{position:"relative",left:"0px",transitionProperty:"left",transitionDuration:"0ms",transitionTimingFunction:this.opts.transition.timingFunction}):(this.helper.setStyle(this.el,{position:"relative",left:"0px"}),document.addEventListener("gesturestart",function(){r.gestureStart=!0},!1),document.addEventListener("gestureend",function(){r.gestureStart=!1},!1)),this.opts.autoPlay&&(this._startAutoPlay(),window.addEventListener("blur",function(){return r._clearAutoPlay()},!1),window.addEventListener("focus",function(){return r._startAutoPlay()},!1)),this.opts.fitWidth&&(s="pc"===this.browser.name?"resize":"orientationchange",window.addEventListener(s,function(){return r.refresh()},!1)),this.el.addEventListener(this.events.start,this,!1),i&&"function"!=typeof i)throw new TypeError("Must be a Function");i&&i(),this.opts.loop&&this._cloneNode(),this.refresh()}return t.prototype.handleEvent=function(t){switch(t.type){case this.events.start:return this._touchStart(t);case this.events.move:return this._touchMove(t);case this.events.end:return this._touchEnd(t);case"click":return this._click(t)}},t.prototype.refresh=function(){var t,e=this;return this.opts.fitWidth?this._setTotalWidth(this.helper.getParentNodeWidth(this.el)):this.opts.setWidth&&this._setTotalWidth(),t=function(){var t,i,s,o,r;for(i=0,r=e.el.childNodes,s=0,o=r.length;o>s;s++)t=r[s],1===t.nodeType&&i++;return i},this.maxPoint=void 0===this.opts.maxPoint?t():this.opts.maxPoint,this.distance=void 0===this.opts.distance?this.el.scrollWidth/this.maxPoint:this.opts.distance,this.maxX=-this.distance*this.maxPoint,this.moveToPoint()},t.prototype.hasPrev=function(){return this.currentPoint>0},t.prototype.hasNext=function(){return this.currentPoint<this.maxPoint},t.prototype.toPrev=function(){return this.hasPrev()?this.moveToPoint(this.currentPoint-1):void 0},t.prototype.toNext=function(){return this.hasNext()?this.moveToPoint(this.currentPoint+1):void 0},t.prototype.moveToPoint=function(t,e){var i;return null==t&&(t=this.currentPoint),null==e&&(e=this.opts.transition.duration),i=this.currentPoint,this.currentPoint=0>t?0:t>this.maxPoint?this.maxPoint:parseInt(t,10),this.support.cssAnimation?this.helper.setStyle(this.el,{transitionDuration:e}):this.opts.useJsAnimate=!0,this._setX(-this.currentPoint*this.distance,e),i!==this.currentPoint&&(this.helper.triggerEvent(this.el,"flpointmove",!0,!1),this.opts.loop)?this._loop():void 0},t.prototype._setX=function(t,e){return null==e&&(e=this.opts.transition.duration),t=parseInt(t,10),this.currentX=t,this.support.cssAnimation&&!this.browser.isLegacy?this.helper.setStyle(this.el,{transform:this._getTranslate(t)}):!this.browser.isLegacy&&this.otps.useJsAnimate?this._jsAnimate(t,e):(this.el.style.left=""+t+"px",void 0)},t.prototype._touchStart=function(t){return this.opts.disableTouch||this.gestureStart?void 0:(this.opts.loop&&(this.currentPoint===this.maxPoint?this.moveToPoint(1,0):0===this.currentPoint&&this.moveToPoint(this.maxPoint-1,0)),this.el.addEventListener(this.events.move,this,!1),document.addEventListener(this.events.end,this,!1),this.support.touch||t.preventDefault(),this.support.cssAnimation?this.helper.setStyle(this.el,{transitionDuration:"0ms"}):this.opts.useJsAnimate=!1,this.scrolling=!0,this.moveReady=!1,this.startPageX=this.helper.getPage(t,"pageX"),this.startPageY=this.helper.getPage(t,"pageY"),this.basePageX=this.startPageX,this.directionX=0,this.startTime=t.timeStamp,this.helper.triggerEvent(this.el,"fltouchstart",!0,!1))},t.prototype._touchMove=function(t){var e,i,s,o,r,n,h;return this.opts.autoPlay&&this._clearAutoPlay(),this.scrolling||this.gestureStart?(n=this.helper.getPage(t,"pageX"),h=this.helper.getPage(t,"pageY"),this.moveReady?(t.preventDefault(),t.stopPropagation(),s=n-this.basePageX,r=this.currentX+s,(r>=0||this.maxX>r)&&(r=Math.round(this.currentX+s/3)),this.directionX=0===s?this.directionX:s>0?-1:1,o=!this.helper.triggerEvent(this.el,"fltouchmove",!0,!0,{delta:s,direction:this.directionX}),o?this._touchAfter({moved:!1,originalPoint:this.currentPoint,newPoint:this.currentPoint,cancelled:!0}):this._setX(r)):(e=Math.abs(n-this.startPageX),i=Math.abs(h-this.startPageY),e>5?(t.preventDefault(),t.stopPropagation(),this.moveReady=!0,this.el.addEventListener("click",this,!0)):i>5&&(this.scrolling=!1)),this.basePageX=n,this.opts.autoPlay?this._startAutoPlay():void 0):void 0},t.prototype._touchEnd=function(){var t,e=this;return this.el.removeEventListener(this.events.move,this,!1),document.removeEventListener(this.events.end,this,!1),this.scrolling?(t=function(){var t;return t=-e.currentX/e.distance,e.directionX>0?Math.ceil(t):0>e.directionX?Math.floor(t):Math.round(t)}(),0>t?t=0:t>this.maxPoint&&(t=this.maxPoint),this._touchAfter({moved:t!==this.currentPoint,originalPoint:this.currentPoint,newPoint:t,cancelled:!1}),this.moveToPoint(t)):void 0},t.prototype._touchAfter=function(t){var e=this;return this.scrolling=!1,this.moveReady=!1,setTimeout(function(){return e.el.removeEventListener("click",e,!0)},200),this.helper.triggerEvent(this.el,"fltouchend",!0,!1,t)},t.prototype._click=function(t){return t.stopPropagation(),t.preventDefault()},t.prototype._getTranslate=function(t){return this.opts.use3d?"translate3d("+t+"px, 0, 0)":"translate("+t+"px, 0)"},t.prototype._cloneNode=function(){var t,e,i,s,o,r=this;if(this.opts.loop||this.didCloneNode){for(s=function(){var t,e,i,s,o;for(e=[],o=r.el.childNodes,i=0,s=o.length;s>i;i++)t=o[i],1===t.nodeType&&e.push(t);return e}(),o=this.helper.getParentNodeWidth(this.el),i=0,e=function(t,e){var i,o;return i=s[t],o=s[s.length-e],r.el.insertBefore(o.cloneNode(!0),s[0]),r.el.appendChild(i.cloneNode(!0))},this.childNodes=s,this.visibleSize=parseInt(o/s[0].offsetWidth,10)+1;this.visibleSize>i;)t=i,e(t,this.visibleSize-t),i++;this.currentPoint=this.visibleSize,this.didCloneNode=!0}},t.prototype._startAutoPlay=function(){var t,e,i=this;if(this.opts.autoPlay)return e=function(){return i.toNext()},t=this.opts.interval,function(){i.timerId=setInterval(e,t)}()},t.prototype._clearAutoPlay=function(){return clearInterval(this.timerId)},t.prototype._setTotalWidth=function(t){var e,i,s,o,r,n,h;if(t&&"number"!=typeof t)throw new TypeError("Must be a Number");for(e=this.el.childNodes,i=0!==e.length?[]:[this.el],n=0,h=e.length;h>n;n++)o=e[n],1===o.nodeType&&i.push(o);s=t?t:this.helper.getElementWidth(i[0]),r=i.length*s,this.el.style.width=""+r+"px"},t.prototype._loop=function(){var t,e,i,s=this;return t=function(){return s.currentPoint<=s.visibleSize?s.moveToPoint(s.currentPoint+s.childElementCount,0):s.currentPoint>=s.maxPoint-s.visibleSize?s.moveToPoint(s.currentPoint-s.childElementCount,0):void 0},i=this.helper.getTransitionEndEventName(),void 0!==i?(this.el.addEventListener(i,t,!1),setTimeout(function(){return s.el.removeEventListener(i,t,!1)},this.opts.clearInterval)):(e=t,clearTimeout(function(){return t()},this.opts.clearInterval))},t.prototype._jsAnimate=function(t,e){var i,s,o,r,n;i=+new Date,o=parseInt(this.el.style.left,10),n=t,e=parseInt(e,10),s=function(t,e){return-(t/=e)*(t-2)},r=setInterval(function(){var t,h,a;a=new Date-i,a>e?(clearInterval(r),t=n):(h=s(a,e),t=h*(n-o)+o),this.el.style.left=""+t+"px"},10)},t.prototype.destroy=function(){return this.opts.autoPlay&&this._clearAutoPlay(),this.el.removeEventListener(this.events.start,this,!1)},t}(),s[i]=t}).call(this);