forked from shrekshrek/jstween
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstween.min.js
5 lines (4 loc) · 14.9 KB
/
jstween.min.js
1
2
3
4
5
/*!
* GIT: https://github.com/shrekshrek/jstween
**/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.JT=e()}(this,function(){"use strict";function t(t,e){if(t.length&&t.length>0)for(var n=0;n<t.length;n++)e.call(t[n],n,t[n]);else e.call(t,0,t)}function e(t){return t.replace(/([A-Z])/g,"-$1").toLowerCase()}function n(t){return t.replace(/\b(\w)|\s(\w)/g,function(t){return t.toUpperCase()})}function i(t){return Math.round(1e3*t)/1e3}function r(t){return X+(t?n(t):"")}function s(t){if(!t)throw"el is undefined, can't tween!!!";return"string"==typeof t?document.querySelectorAll(t):t}function a(t,e,n){for(var i=0,s=F.length;i<s;i++)if(e===F[i])return;if(n){for(var i=0,s=U.length;i<s;i++)if(e===U[i])return e;if(void 0!==t._jt_obj[e])return e;if(void 0!==t.style[e])return e;if(e=r(e),void 0!==t.style[e])return e}else if("string"==typeof t[e]||"number"==typeof t[e])return e}function o(t,e){var n={};if(Array.isArray(e)){n.num=[t.num];for(var i=0,r=e.length;i<r;i++){var s=u(t,e[i]);n.num.push(s.num),n.unit=s.unit}}else n=u(t,e);return n}function u(t,e){var n=c(e);"rem"===t.unit&&"rem"!==n.unit?(_(),t.num=i(t.num*Y),t.unit="px"):"rem"!==t.unit&&"rem"===n.unit&&(_(),t.num=i(t.num/Y),t.unit="rem");var r;switch(n.ext){case"+=":r=t.num+n.num;break;case"-=":r=t.num-n.num;break;default:r=n.num}return{num:r,unit:n.unit}}function h(t){void 0===t._jt_obj&&(t._jt_obj={perspective:0,x:0,y:0,z:0,rotationX:0,rotationY:0,rotationZ:0,scaleX:1,scaleY:1,scaleZ:1,skewX:0,skewY:0})}function c(t){var e=/(\+=|-=|)(-|)(\d+\.\d+|\d+)(e[+-]?[0-9]{0,2}|)(rem|px|%|)/i,n=e.exec(t);return n?{num:i(n[2]+n[3]+n[4]),unit:n[5],ext:n[1]}:{num:0,unit:"px",ext:""}}function l(t){return/(,| |jpeg|jpg|png|gif|-3d)/g.test(t)||!/\d/g.test(t)}function f(t,e){switch(e){case"perspective":case"x":case"y":case"z":case"rotationX":case"rotationY":case"rotationZ":case"scaleX":case"scaleY":case"scaleZ":case"skewX":case"skewY":return t._jt_obj[e];case"rotation":return t._jt_obj.rotationZ;case"scale":return t._jt_obj.scaleX;case"autoAlpha":return p(t,"opacity");default:return p(t,e)}}function p(t,n){if(t.style[n])return t.style[n];var i=e(n),r=window.getComputedStyle(t,null);return r[i]||r.getPropertyValue(i)}function m(t,n,i,r){switch(n){case"x":case"y":case"z":return t._jt_obj[n]=i+(r||"px"),!0;case"rotationX":case"rotationY":case"rotationZ":case"skewX":case"skewY":return t._jt_obj[n]=i%360+"deg",!0;case"perspective":case"scaleX":case"scaleY":case"scaleZ":return t._jt_obj[n]=i,!0;case"rotation":return t._jt_obj.rotationZ=i%360+"deg",!0;case"scale":return t._jt_obj.scaleX=i,t._jt_obj.scaleY=i,!0;case"autoAlpha":return d(t,"opacity",i),d(t,"display",i>0?"block":"none"),!1;default:return void 0!==r&&(i=z[e(n)]?i:i+(r||"px")),d(t,n,i),!1}}function d(t,e,n){t.style[e]=n}function v(t){return"object"==typeof t&&1===t.nodeType}function y(t){var e="";t._jt_obj.perspective&&(e+="perspective("+t._jt_obj.perspective+") "),(t._jt_obj.x||t._jt_obj.y||t._jt_obj.z)&&(e+="translate3d("+t._jt_obj.x+","+t._jt_obj.y+","+t._jt_obj.z+") "),t._jt_obj.rotationX&&(e+="rotateX("+t._jt_obj.rotationX+") "),t._jt_obj.rotationY&&(e+="rotateY("+t._jt_obj.rotationY+") "),t._jt_obj.rotationZ&&(e+="rotateZ("+t._jt_obj.rotationZ+") "),1===t._jt_obj.scaleX&&1===t._jt_obj.scaleY&&1===t._jt_obj.scaleZ||(e+="scale3d("+t._jt_obj.scaleX+", "+t._jt_obj.scaleY+", "+t._jt_obj.scaleZ+") "),(t._jt_obj.skewX||t._jt_obj.skewY)&&(e+="skew("+t._jt_obj.skewX+","+t._jt_obj.skewY+") "),t.style[r("transform")]=e}function _(){A||(A=document.createElement("div"),A.style.cssText="border:0 solid; position:absolute; line-height:0px;"),E||(E=document.getElementsByTagName("body")[0]),E.appendChild(A),A.style.borderLeftWidth="1rem",Y=parseFloat(A.offsetWidth),E.removeChild(A)}function j(){var t=D.length;if(0===t)return void(q=!1);var e=x.now(),n=e-K;K=e,n>500&&(n=33),Z=D.slice(0);for(var i=0;i<t;i++){var r=Z[i];r&&r.isPlaying&&!r._update(n)&&r.pause()}V(j)}function T(){this.initialize.apply(this,arguments)}function S(t){if(!t.isInited){t.isInited=!0;for(var e in t.fromVars){var n=c(t.isDom?f(t.el,e):t.el[e]);t.fromVars[e]=null===t.fromVars[e]?n:o(n,t.fromVars[e]),t.toVars[e]=null===t.toVars[e]?n:o(t.fromVars[e],t.toVars[e])}}}function b(e,n,i,r,o){if("number"!=typeof i)throw"The second parameter must be a number!";M(o);var u=s(n),c=[];return t(u,function(t,n){var s,u={},l={},f=v(n);switch(e){case"fromTo":s=o,s.isFrom=!0;break;case"from":s=r,s.isFrom=!0;break;case"to":s=o,s.isFrom=!1}f&&h(n);for(var p in s){var m=a(n,p,f);if(m)switch(e){case"fromTo":u[m]=r[p],l[m]=o[p];break;case"from":u[m]=r[p],l[m]=null;break;case"to":u[m]=null,l[m]=o[p]}else l[p]=s[p]}c.push(new T(n,i,u,l,f))}),0===c.length?null:1===c.length?c[0]:c}function g(e,n,i){var r=s(e),a=D.length;t(r,function(t,e){for(var r=a-1;r>=0;r--){var s=D[r];s.el===e&&s[n](i)}})}function w(t,e){for(var n=D.length,i=n-1;i>=0;i--){D[i][t](e)}}function M(t){t.bezier&&(k(t,t.bezier),t.interpolation=R,delete t.bezier),t.through&&(k(t,t.through),t.interpolation=I,delete t.through),t.linear&&(k(t,t.linear),t.interpolation=P,delete t.linear)}function k(t,e){for(var n=0,i=e.length;n<i;n++)for(var r in e[n])0===n?t[r]=[e[n][r]]:t[r].push(e[n][r])}function P(t,e){var n=t.length-1,i=n*e,r=Math.floor(i),s=B.Linear;return e<0?s(t[0],t[1],i):e>1?s(t[n],t[n-1],n-i):s(t[r],t[r+1>n?n:r+1],i-r)}function R(t,e){var n,i=0,r=t.length-1,s=Math.pow,a=B.Bernstein;for(n=0;n<=r;n++)i+=s(1-e,r-n)*s(e,n)*t[n]*a(r,n);return i}function I(t,e){var n=t.length-1,i=n*e,r=Math.floor(i),s=B.Through;return t[0]===t[n]?(e<0&&(r=Math.floor(i=n*(1+e))),s(t[(r-1+n)%n],t[r],t[(r+1)%n],t[(r+2)%n],i-r)):e<0?t[0]-(s(t[0],t[0],t[1],t[1],-i)-t[0]):e>1?t[n]-(s(t[n],t[n],t[n-1],t[n-1],i-n)-t[n]):s(t[r?r-1:0],t[r],t[n<r+1?n:r+1],t[n<r+2?n:r+2],i-r)}var x={};Date.now=Date.now||function(){return(new Date).getTime()};var O=Date.now();x.now=function(){return Date.now()-O};var E,A,Y,X=function(){var t=document.createElement("div"),e=["Webkit","Moz","Ms","O"];for(var n in e)if(e[n]+"Transform"in t.style)return e[n]}(),V=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)},F=["ease","delay","yoyo","repeat","repeatDelay","onStart","onStartScope","onStartParams","onRepeat","onRepeatScope","onRepeatParams","onEnd","onEndScope","onEndParams","onUpdate","onUpdateScope","onUpdateParams","interpolation","isReverse","timeScale","isFrom","isPlaying"],U=["rotation","scale","autoAlpha"],z={"column-count":1,columns:1,"font-weight":1,"line-height":1,opacity:1,"z-index":1,zoom:1},D=[],Z=[],q=!1,K=0;Object.assign(T.prototype,{initialize:function(t,e,n,i,r){this.fromVars=n,this.curVars={},this.toVars=i,this.el=t,this.duration=1e3*Math.max(e,0),this.ease=i.ease||x.Linear.None,this.delay=1e3*Math.max(i.delay||0,0),this.yoyo=i.yoyo||!1,this.repeat=i.repeat||0,this.repeatDelay=1e3*Math.max(i.repeatDelay||0,0),this.onStart=i.onStart||null,this.onStartScope=i.onStartScope||this,this.onStartParams=i.onStartParams||[],this.onRepeat=i.onRepeat||null,this.onRepeatScope=i.onRepeatScope||this,this.onRepeatParams=i.onRepeatParams||[],this.onEnd=i.onEnd||null,this.onEndScope=i.onEndScope||this,this.onEndParams=i.onEndParams||[],this.onUpdate=i.onUpdate||null,this.onUpdateScope=i.onUpdateScope||this,this.onUpdateParams=i.onUpdateParams||[],this.isPlaying=!1,this.interpolation=i.interpolation||null,this.isReverse=i.isReverse||!1,this.timeScale=i.timeScale||1,this.isFrom=i.isFrom||!1,this.isInited=!1,this.isSeek=!1,this.isKeep=!1,this.isYoReverse=!1,this.isDom=r,this.repeat=this.repeat<0?999999999999:Math.floor(this.repeat),this.curRepeat=0,this.elapsed=null,this.startTime=this.delay,this.endTime=this.startTime+this.repeatDelay*this.repeat+this.duration*(this.repeat+1),this.curTime=null,this.lastTime=null,!1!==i.isPlaying&&this.play()},_update:function(t){this.isKeep=!1,t=(this.isReverse?-1:1)*t*this.timeScale;var e=this.curTime,n=Math.min(this.endTime,Math.max(0,e+t));if(n===this.curTime)return!0;this.lastTime=e,this.curTime=n;var i=Math.min(this.repeat,Math.max(0,Math.floor((this.curTime-this.startTime)/(this.duration+this.repeatDelay)))),r=!1;if(i!==this.curRepeat&&(this.curRepeat=i,this.yoyo&&(this.isYoReverse=this.curRepeat%2!=0),r=!0),this.isFrom&&(S(this),this._updateProp()),this.lastTime<this.startTime&&this.curTime<this.startTime)return!0;if(this.isFrom||(S(this),this._updateProp()),this.lastTime<this.curTime){if(this.lastTime<=this.startTime&&this.curTime>this.startTime&&!this.isSeek&&this.onStart&&this.onStart.apply(this.onStartScope,this.onStartParams),r&&!this.isSeek&&this.onRepeat&&this.onRepeat.apply(this.onRepeatScope,this.onRepeatParams),this.lastTime<this.endTime&&this.curTime>=this.endTime)return!this.isSeek&&this.onEnd&&this.onEnd.apply(this.onEndScope,this.onEndParams),this.isKeep}else if(this.lastTime>=this.endTime&&this.curTime<this.endTime&&!this.isSeek&&this.onEnd&&this.onEnd.apply(this.onEndScope,this.onEndParams),r&&!this.isSeek&&this.onRepeat&&this.onRepeat.apply(this.onRepeatScope,this.onRepeatParams),this.lastTime>this.startTime&&this.curTime<=this.startTime)return!this.isSeek&&this.onStart&&this.onStart.apply(this.onStartScope,this.onStartParams),this.isKeep;return!0},_updateProp:function(){var t=Math.min(1,Math.max(0,(this.curTime===this.endTime?this.duration:(this.curTime-this.startTime)%(this.duration+this.repeatDelay))/this.duration));if(this.isYoReverse&&(t=1-t),t!==this.elapsed){this.elapsed=t;var e=this.ease(t),n=!1;for(var r in this.fromVars){var s,a=this.fromVars[r],o=this.toVars[r];s=Array.isArray(o.num)?this.interpolation(o.num,e):a.num+(o.num-a.num)*e,s=i(s),this.curVars[r]={num:s,unit:o.unit},this.isDom?m(this.el,r,s,o.unit)&&(n=!0):this.el[r]=s+(o.unit||0)}n&&y(this.el),!this.isSeek&&this.onUpdate&&this.onUpdate.apply(this.onUpdateScope,this.onUpdateParams)}},_addSelf:function(){D.push(this),q||(K=x.now(),q=!0,V(j))},_removeSelf:function(){var t=D.indexOf(this);-1!==t&&D.splice(t,1)},play:function(t){if(this.isReverse=!1,void 0!==t&&this.seek(t,!0),this.curTime===this.endTime)return this.isKeep=!1;this.isKeep=!0,this.isPlaying||(this.isPlaying=!0,this._addSelf())},pause:function(){this.isKeep=!1,this.isPlaying&&(this.isPlaying=!1,this._removeSelf())},stop:function(){this.pause(),this.seek(0,!0)},reverse:function(t){if(this.isReverse=!0,void 0!==t&&this.seek(t,!0),0===this.curTime)return this.isKeep=!1;this.isKeep=!0,this.isPlaying||(this.isPlaying=!0,this._addSelf())},seek:function(t,e){var n=Math.max(0,Math.min(this.endTime,1e3*t));this.curTime!==n&&(this.isSeek=e||!1,this._update((this.isReverse?-1:1)*(n-this.curTime)),this.isSeek=!1)},setTimeScale:function(t){this.timeScale=t},kill:function(t){this.pause(),t&&this.seek(this.endTime),this.duration=null,this.curTime=this.lastTime=this.startTime=this.endTime=null,this.el=this.onStart=this.onRepeat=this.onEnd=this.onUpdate=null}}),Object.assign(x,{get:function(t,e){var n=s(t);void 0!==n.length&&(n=n[0]);var i=v(n);if(i){h(n);var r=a(n,e,i);return r?f(n,r):null}return n[e]},set:function(e,n){t(s(e),function(t,e){var i=v(e);if(i){h(e);var r=!1;for(var s in n){var u=a(e,s,i);if(u)if(l(n[s]))m(e,u,n[s])&&(r=!0);else{var p=o(c(f(e,u)),n[s]);m(e,u,p.num,p.unit)&&(r=!0)}}r&&y(e)}else for(var s in n){var p=o(c(e[s]),n[s]);e[s]=p.num+(p.unit||0)}})},fromTo:function(t,e,n,i){return e||i.delay?b("fromTo",t,e,n,i):this.set(t,i)},from:function(t,e,n){return e||n.delay?b("from",t,e,n,{}):this.set(t,n)},to:function(t,e,n){return e||n.delay?b("to",t,e,{},n):this.set(t,n)},kill:function(e,n){t(s(e),function(t,e){for(var i=D.length,r=i-1;r>=0;r--){var s=D[r];s.el===e&&s.kill(n)}})},killAll:function(t){for(var e=D.length,n=e-1;n>=0;n--){D[n].kill(t)}},play:function(t,e){g(t,"play",e)},playAll:function(t){w("play",t)},pause:function(t){g(t,"pause")},pauseAll:function(){w("pause")},stop:function(t){g(t,"stop")},stopAll:function(){w("stop")},reverse:function(t,e){g(t,"reverse",e)},reverseAll:function(t){w("reverse",t)},seek:function(t,e){g(t,"seek",e)},seekAll:function(t){w("seek",t)},setTimeScale:function(t,e){g(t,"setTimeScale",e)},setTimeScaleAll:function(t){w("setTimeScale",t)},isTweening:function(t){var e=s(t);e=e[0]||e;for(var n=D.length,i=n-1;i>=0;i--){if(D[i].el===e)return!0}return!1},call:function(t,e,n,i){return t?new T({},Math.max(0,t),{},{onEnd:e,onEndParams:n,isPlaying:i},!1):e.apply(e,n)}}),Object.assign(x,{path:function(t){M(t);for(var e,n=t.ease||x.Linear.None,i=t.step||1,r=[],s=0;s<=i;s++){e=n(s/i);var a={};for(var o in t)Array.isArray(t[o])&&(a[o]=t.interpolation(t[o],e));r.push(a)}return r}});var B={Linear:function(t,e,n){return(e-t)*n+t},Bernstein:function(t,e){var n=B.Factorial;return n(t)/n(e)/n(t-e)},Factorial:function(){var t=[1];return function(e){var n,i=1;if(t[e])return t[e];for(n=e;n>1;n--)i*=n;return t[e]=i}}(),Through:function(t,e,n,i,r){var s=.5*(n-t),a=.5*(i-e),o=r*r;return(2*e-2*n+s+a)*(r*o)+(-3*e+3*n-2*s-a)*o+s*r+e}};return Object.assign(x,{Linear:{None:function(t){return t}},Quad:{In:function(t){return t*t},Out:function(t){return t*(2-t)},InOut:function(t){return(t*=2)<1?.5*t*t:-.5*(--t*(t-2)-1)}},Cubic:{In:function(t){return t*t*t},Out:function(t){return--t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t:.5*((t-=2)*t*t+2)}},Quart:{In:function(t){return t*t*t*t},Out:function(t){return 1- --t*t*t*t},InOut:function(t){return(t*=2)<1?.5*t*t*t*t:-.5*((t-=2)*t*t*t-2)}},Quint:{In:function(t){return t*t*t*t*t},Out:function(t){return--t*t*t*t*t+1},InOut:function(t){return(t*=2)<1?.5*t*t*t*t*t:.5*((t-=2)*t*t*t*t+2)}},Sine:{In:function(t){return 1-Math.cos(t*Math.PI/2)},Out:function(t){return Math.sin(t*Math.PI/2)},InOut:function(t){return.5*(1-Math.cos(Math.PI*t))}},Expo:{In:function(t){return 0===t?0:Math.pow(1024,t-1)},Out:function(t){return 1===t?1:1-Math.pow(2,-10*t)},InOut:function(t){return 0===t?0:1===t?1:(t*=2)<1?.5*Math.pow(1024,t-1):.5*(2-Math.pow(2,-10*(t-1)))}},Circ:{In:function(t){return 1-Math.sqrt(1-t*t)},Out:function(t){return Math.sqrt(1- --t*t)},InOut:function(t){return(t*=2)<1?-.5*(Math.sqrt(1-t*t)-1):.5*(Math.sqrt(1-(t-=2)*t)+1)}},Elastic:{In:function(t){var e,n=.1;return 0===t?0:1===t?1:(!n||n<1?(n=1,e=.1):e=.4*Math.asin(1/n)/(2*Math.PI),-n*Math.pow(2,10*(t-=1))*Math.sin((t-e)*(2*Math.PI)/.4))},Out:function(t){var e,n=.1;return 0===t?0:1===t?1:(!n||n<1?(n=1,e=.1):e=.4*Math.asin(1/n)/(2*Math.PI),n*Math.pow(2,-10*t)*Math.sin((t-e)*(2*Math.PI)/.4)+1)},InOut:function(t){var e,n=.1;return 0===t?0:1===t?1:(!n||n<1?(n=1,e=.1):e=.4*Math.asin(1/n)/(2*Math.PI),(t*=2)<1?n*Math.pow(2,10*(t-=1))*Math.sin((t-e)*(2*Math.PI)/.4)*-.5:n*Math.pow(2,-10*(t-=1))*Math.sin((t-e)*(2*Math.PI)/.4)*.5+1)}},Back:{In:function(t){var e=1.70158;return t*t*((e+1)*t-e)},Out:function(t){var e=1.70158;return--t*t*((e+1)*t+e)+1},InOut:function(t){var e=2.5949095;return(t*=2)<1?t*t*((e+1)*t-e)*.5:.5*((t-=2)*t*((e+1)*t+e)+2)}},Bounce:{In:function(t){return 1-x.Bounce.Out(1-t)},Out:function(t){return t<1/2.75?7.5625*t*t:t<2/2.75?7.5625*(t-=1.5/2.75)*t+.75:t<2.5/2.75?7.5625*(t-=2.25/2.75)*t+.9375:7.5625*(t-=2.625/2.75)*t+.984375},InOut:function(t){return t<.5?.5*x.Bounce.In(2*t):.5*x.Bounce.Out(2*t-1)+.5}}}),x});