UNPKG

tinyjs-plugin-scroller

Version:
8 lines (7 loc) 13.1 kB
/*! * Name: tinyjs-plugin-scroller * Description: 滚动插件 * Author: fangjun.yfj * Version: v0.2.0 */ !function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t.Tiny=t.Tiny||{},t.Tiny.Scroller=i())}(this,function(){"use strict";var t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var i,o=(function(i,o){var e="undefined"==typeof window?t:window,s=Date.now||function(){return+new Date},n={},_=1;o.requestAnimationFrame=function(){var t=e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame,i=!!t;if(t&&!/requestAnimationFrame\(\)\s*\{\s*\[native code\]\s*\}/i.test(t.toString())&&(i=!1),i)return function(i,o){t(i,o)};var o={},s=1,n=null,_=+new Date;return function(t,i){var e=s++;return o[e]=t,null===n&&(n=setInterval(function(){var t=+new Date,i=o;for(var e in o={},i)i.hasOwnProperty(e)&&(i[e](t),_=t);t-_>2500&&(clearInterval(n),n=null)},1e3/60)),e}}(),o.stop=function(t){var i=null!==n[t];return i&&(n[t]=null),i},o.isRunning=function(t){return null!==n[t]},o.start=function(t,i,e,l,h,a){var r=s(),c=r,p=0,m=0,u=_++;if(u%20==0){var g={};for(var f in n)g[f]=!0;n=g}return n[u]=!0,o.requestAnimationFrame(function _(g){var f=!0!==g,d=s();if(!n[u]||i&&!i(u))return n[u]=null,void e(60-m/((d-r)/1e3),u,!1);if(f)for(var T=Math.round((d-c)/(1e3/60))-1,v=0;v<Math.min(T,4);v++)_(!0),m++;l&&(p=(d-r)/l)>1&&(p=1);var L=h?h(p):p;!1!==t(L,d,f)&&1!==p||!f?f&&(c=d,o.requestAnimationFrame(_,a)):(n[u]=null,e(60-m/((d-r)/1e3),u,1===p||void 0===l))},a),u}}(i={exports:{}},i.exports),i.exports),e=(o.requestAnimationFrame,o.stop,o.isRunning,o.start,function(){}),s=function(t,i){for(var o in this.__callback=t,this.options={scrollingX:!0,scrollingY:!0,animating:!0,animationDuration:250,bouncing:!0,locking:!0,paging:!1,snapping:!1,zooming:!1,minZoom:.5,maxZoom:3,speedMultiplier:1,scrollingComplete:e,penetrationDeceleration:.03,penetrationAcceleration:.08,easingFunction:[_,n]},i)this.options[o]=i[o]},n=function(t){return Math.pow(t-1,3)+1},_=function(t){return(t/=.5)<1?.5*Math.pow(t,3):.5*(Math.pow(t-2,3)+2)};return s.prototype={__isSingleTouch:!1,__isTracking:!1,__didDecelerationComplete:!1,__isGesturing:!1,__isDragging:!1,__isDecelerating:!1,__isAnimating:!1,__clientLeft:0,__clientTop:0,__clientWidth:0,__clientHeight:0,__contentWidth:0,__contentHeight:0,__snapWidth:100,__snapHeight:100,__zoomLevel:1,__scrollLeft:0,__scrollTop:0,__maxScrollLeft:0,__maxScrollTop:0,__scheduledLeft:0,__scheduledTop:0,__scheduledZoom:0,__lastTouchLeft:null,__lastTouchTop:null,__lastTouchMove:null,__positions:null,__minDecelerationScrollLeft:null,__minDecelerationScrollTop:null,__maxDecelerationScrollLeft:null,__maxDecelerationScrollTop:null,__decelerationVelocityX:null,__decelerationVelocityY:null,setDimensions:function(t,i,o,e){null!==t&&(this.__clientWidth=t),null!==i&&(this.__clientHeight=i),null!==o&&(this.__contentWidth=o),null!==e&&(this.__contentHeight=e),this.__computeScrollMax(),this.scrollTo(this.__scrollLeft,this.__scrollTop,!0)},setPosition:function(t,i){this.__clientLeft=t||0,this.__clientTop=i||0},stopAnimation:function(){this.__isDecelerating&&(o.stop(this.__isDecelerating),this.__isDecelerating=!1,this.__interruptedAnimation=!0),this.__isAnimating&&(o.stop(this.__isAnimating),this.__isAnimating=!1,this.__interruptedAnimation=!0)},setSnapSize:function(t,i){this.__snapWidth=t,this.__snapHeight=i},getValues:function(){return{left:this.__scrollLeft,top:this.__scrollTop,right:this.__scrollLeft+this.__clientWidth/this.__zoomLevel,bottom:this.__scrollTop+this.__clientHeight/this.__zoomLevel,zoom:this.__zoomLevel}},getPoint:function(t,i){var o=this.getValues();return{left:t/o.zoom,top:i/o.zoom}},getScrollMax:function(){return{left:this.__maxScrollLeft,top:this.__maxScrollTop}},zoomTo:function(t,i,e,s,n){if(!this.options.zooming)throw new Error("Zooming is not enabled!");n&&(this.__zoomComplete=n),this.__isDecelerating&&(o.stop(this.__isDecelerating),this.__isDecelerating=!1);var _=this.__zoomLevel;void 0===e&&(e=this.__clientWidth/2),void 0===s&&(s=this.__clientHeight/2),t=Math.max(Math.min(t,this.options.maxZoom),this.options.minZoom),this.__computeScrollMax(t);var l=t/_,h=l*(this.__scrollLeft+e)-e,a=l*(this.__scrollTop+s)-s;h>this.__maxScrollLeft?h=this.__maxScrollLeft:h<0&&(h=0),a>this.__maxScrollTop?a=this.__maxScrollTop:a<0&&(a=0),this.__publish(h,a,t,i)},zoomBy:function(t,i,o,e,s){this.zoomTo(this.__zoomLevel*t,i,o,e,s)},scrollTo:function(t,i,e,s){if(this.__isDecelerating&&(o.stop(this.__isDecelerating),this.__isDecelerating=!1),void 0!==s&&s!==this.__zoomLevel){if(!this.options.zooming)throw new Error("Zooming is not enabled!");t*=s,i*=s,this.__computeScrollMax(s)}else s=this.__zoomLevel;this.options.scrollingX?this.options.paging?t=Math.round(t/this.__clientWidth)*this.__clientWidth:this.options.snapping&&(t=Math.round(t/this.__snapWidth)*this.__snapWidth):t=this.__scrollLeft,this.options.scrollingY?this.options.paging?i=Math.round(i/this.__clientHeight)*this.__clientHeight:this.options.snapping&&(i=Math.round(i/this.__snapHeight)*this.__snapHeight):i=this.__scrollTop,t=Math.max(Math.min(this.__maxScrollLeft,t),0),i=Math.max(Math.min(this.__maxScrollTop,i),0),t===this.__scrollLeft&&i===this.__scrollTop&&(e=!1),this.__publish(t,i,s,e)},scrollBy:function(t,i,o){var e=this.__isAnimating?this.__scheduledLeft:this.__scrollLeft,s=this.__isAnimating?this.__scheduledTop:this.__scrollTop;this.scrollTo(e+(t||0),s+(i||0),o)},doMouseZoom:function(t,i,o,e){var s=t>0?.97:1.03;return this.zoomTo(this.__zoomLevel*s,!1,o-this.__clientLeft,e-this.__clientTop)},doTouchStart:function(t,i){if(void 0===t.length)throw new Error("Invalid touch list: "+t);if(i instanceof Date&&(i=i.valueOf()),"number"!=typeof i)throw new Error("Invalid timestamp value: "+i);var e,s;this.__interruptedAnimation=!0,this.__isDecelerating&&(o.stop(this.__isDecelerating),this.__isDecelerating=!1,this.__interruptedAnimation=!0),this.__isAnimating&&(o.stop(this.__isAnimating),this.__isAnimating=!1,this.__interruptedAnimation=!0);var n=1===t.length;n?(e=t[0].pageX,s=t[0].pageY):(e=Math.abs(t[0].pageX+t[1].pageX)/2,s=Math.abs(t[0].pageY+t[1].pageY)/2),this.__initialTouchLeft=e,this.__initialTouchTop=s,this.__zoomLevelStart=this.__zoomLevel,this.__lastTouchLeft=e,this.__lastTouchTop=s,this.__lastTouchMove=i,this.__lastScale=1,this.__enableScrollX=!n&&this.options.scrollingX,this.__enableScrollY=!n&&this.options.scrollingY,this.__isTracking=!0,this.__didDecelerationComplete=!1,this.__isDragging=!n,this.__isSingleTouch=n,this.__positions=[]},doTouchMove:function(t,i,o){if(void 0===t.length)throw new Error("Invalid touch list: "+t);if(i instanceof Date&&(i=i.valueOf()),"number"!=typeof i)throw new Error("Invalid timestamp value: "+i);if(this.__isTracking){var e,s;2===t.length?(e=Math.abs(t[0].pageX+t[1].pageX)/2,s=Math.abs(t[0].pageY+t[1].pageY)/2):(e=t[0].pageX,s=t[0].pageY);var n=this.__positions;if(this.__isDragging){var _=e-this.__lastTouchLeft,l=s-this.__lastTouchTop,h=this.__scrollLeft,a=this.__scrollTop,r=this.__zoomLevel;if(void 0!==o&&this.options.zooming){var c=r;if(r=r/this.__lastScale*o,c!==(r=Math.max(Math.min(r,this.options.maxZoom),this.options.minZoom))){var p=e-this.__clientLeft,m=s-this.__clientTop;h=(p+h)*r/c-p,a=(m+a)*r/c-m,this.__computeScrollMax(r)}}if(this.__enableScrollX){h-=_*this.options.speedMultiplier;var u=this.__maxScrollLeft;(h>u||h<0)&&(this.options.bouncing?h+=_/2*this.options.speedMultiplier:h=h>u?u:0)}if(this.__enableScrollY){a-=l*this.options.speedMultiplier;var g=this.__maxScrollTop;(a>g||a<0)&&(this.options.bouncing?a+=l/2*this.options.speedMultiplier:a=a>g?g:0)}n.length>60&&n.splice(0,30),n.push(h,a,i),this.__publish(h,a,r)}else{var f=this.options.locking?3:0,d=Math.abs(e-this.__initialTouchLeft),T=Math.abs(s-this.__initialTouchTop);this.__enableScrollX=this.options.scrollingX&&d>=f,this.__enableScrollY=this.options.scrollingY&&T>=f,n.push(this.__scrollLeft,this.__scrollTop,i),this.__isDragging=(this.__enableScrollX||this.__enableScrollY)&&(d>=5||T>=5),this.__isDragging&&(this.__interruptedAnimation=!1)}this.__lastTouchLeft=e,this.__lastTouchTop=s,this.__lastTouchMove=i,this.__lastScale=o}},doTouchEnd:function(t){if(t instanceof Date&&(t=t.valueOf()),"number"!=typeof t)throw new Error("Invalid timestamp value: "+t);if(this.__isTracking){if(this.__isTracking=!1,this.__isDragging)if(this.__isDragging=!1,this.__isSingleTouch&&this.options.animating&&t-this.__lastTouchMove<=100){for(var i=this.__positions,o=i.length-1,e=o,s=o;s>0&&i[s]>this.__lastTouchMove-100;s-=3)e=s;if(e!==o){var n=i[o]-i[e],_=this.__scrollLeft-i[e-2],l=this.__scrollTop-i[e-1];this.__decelerationVelocityX=_/n*(1e3/60),this.__decelerationVelocityY=l/n*(1e3/60);var h=this.options.paging||this.options.snapping?4:1;(Math.abs(this.__decelerationVelocityX)>h||Math.abs(this.__decelerationVelocityY)>h)&&this.__startDeceleration(t)}else this.options.scrollingComplete()}else t-this.__lastTouchMove>100&&this.options.scrollingComplete();this.__isDecelerating||((this.__interruptedAnimation||this.__isDragging)&&this.options.scrollingComplete(),this.scrollTo(this.__scrollLeft,this.__scrollTop,!0,this.__zoomLevel)),this.__positions.length=0}},__publish:function(t,i,e,s){var n=this.__isAnimating;if(n&&(o.stop(n),this.__isAnimating=!1),s&&this.options.animating){this.__scheduledLeft=t,this.__scheduledTop=i,this.__scheduledZoom=e;var _=this.__scrollLeft,l=this.__scrollTop,h=this.__zoomLevel,a=t-_,r=i-l,c=e-h,p=function(t,i,o){o&&(this.__scrollLeft=_+a*t,this.__scrollTop=l+r*t,this.__zoomLevel=h+c*t,this.__callback&&this.__callback(this.__scrollLeft,this.__scrollTop,this.__zoomLevel))}.bind(this),m=function(t){return this.__isAnimating===t}.bind(this),u=function(t,i,o){i===this.__isAnimating&&(this.__isAnimating=!1),(this.__didDecelerationComplete||o)&&this.options.scrollingComplete(),this.options.zooming&&(this.__computeScrollMax(),this.__zoomComplete&&(this.__zoomComplete(),this.__zoomComplete=null))}.bind(this),g=n&&this.options.easingFunction[1]||this.options.easingFunction[0];this.__isAnimating=o.start(p,m,u,this.options.animationDuration,g)}else this.__scheduledLeft=this.__scrollLeft=t,this.__scheduledTop=this.__scrollTop=i,this.__scheduledZoom=this.__zoomLevel=e,this.__callback&&this.__callback(t,i,e),this.options.zooming&&(this.__computeScrollMax(),this.__zoomComplete&&(this.__zoomComplete(),this.__zoomComplete=null))},__computeScrollMax:function(t){void 0===t&&(t=this.__zoomLevel),this.__maxScrollLeft=Math.max(this.__contentWidth*t-this.__clientWidth,0),this.__maxScrollTop=Math.max(this.__contentHeight*t-this.__clientHeight,0)},__startDeceleration:function(t){if(this.options.paging){var i=Math.max(Math.min(this.__scrollLeft,this.__maxScrollLeft),0),e=Math.max(Math.min(this.__scrollTop,this.__maxScrollTop),0),s=this.__clientWidth,n=this.__clientHeight;this.__minDecelerationScrollLeft=Math.floor(i/s)*s,this.__minDecelerationScrollTop=Math.floor(e/n)*n,this.__maxDecelerationScrollLeft=Math.ceil(i/s)*s,this.__maxDecelerationScrollTop=Math.ceil(e/n)*n}else this.__minDecelerationScrollLeft=0,this.__minDecelerationScrollTop=0,this.__maxDecelerationScrollLeft=this.__maxScrollLeft,this.__maxDecelerationScrollTop=this.__maxScrollTop;var _=function(t,i,o){this.__stepThroughDeceleration(o)}.bind(this),l=this.options.snapping?4:.1,h=function(){var t=Math.abs(this.__decelerationVelocityX)>=l||Math.abs(this.__decelerationVelocityY)>=l;return t||(this.__didDecelerationComplete=!0),t}.bind(this),a=function(t,i,o){this.__isDecelerating=!1,this.__didDecelerationComplete&&this.options.scrollingComplete(),this.scrollTo(this.__scrollLeft,this.__scrollTop,this.options.snapping)}.bind(this);this.__isDecelerating=o.start(_,h,a)},__stepThroughDeceleration:function(t){var i=this.__scrollLeft+this.__decelerationVelocityX,o=this.__scrollTop+this.__decelerationVelocityY;if(!this.options.bouncing){var e=Math.max(Math.min(this.__maxDecelerationScrollLeft,i),this.__minDecelerationScrollLeft);e!==i&&(i=e,this.__decelerationVelocityX=0);var s=Math.max(Math.min(this.__maxDecelerationScrollTop,o),this.__minDecelerationScrollTop);s!==o&&(o=s,this.__decelerationVelocityY=0)}if(t?this.__publish(i,o,this.__zoomLevel):(this.__scrollLeft=i,this.__scrollTop=o),!this.options.paging){this.__decelerationVelocityX*=.95,this.__decelerationVelocityY*=.95}if(this.options.bouncing){var n=0,_=0,l=this.options.penetrationDeceleration,h=this.options.penetrationAcceleration;i<this.__minDecelerationScrollLeft?n=this.__minDecelerationScrollLeft-i:i>this.__maxDecelerationScrollLeft&&(n=this.__maxDecelerationScrollLeft-i),o<this.__minDecelerationScrollTop?_=this.__minDecelerationScrollTop-o:o>this.__maxDecelerationScrollTop&&(_=this.__maxDecelerationScrollTop-o),0!==n&&(n*this.__decelerationVelocityX<=0?this.__decelerationVelocityX+=n*l:this.__decelerationVelocityX=n*h),0!==_&&(_*this.__decelerationVelocityY<=0?this.__decelerationVelocityY+=_*l:this.__decelerationVelocityY=_*h)}}},s});