animation.js

来自「国外很不错的一个开源OA系统Group-Office」· JavaScript 代码 · 共 1,273 行 · 第 1/3 页

JS
1,273
字号
    * Begins slowly and accelerates towards end. (quartic)    * @method easeInStrong    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   easeInStrong: function (t, b, c, d) {   	return c*(t/=d)*t*t*t + b;   },   /**    * Begins quickly and decelerates towards end.  (quartic)    * @method easeOutStrong    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   easeOutStrong: function (t, b, c, d) {   	return -c * ((t=t/d-1)*t*t*t - 1) + b;   },   /**    * Begins slowly and decelerates towards end. (quartic)    * @method easeBothStrong    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   easeBothStrong: function (t, b, c, d) {   	if ((t/=d/2) < 1) return c/2*t*t*t*t + b;   	return -c/2 * ((t-=2)*t*t*t - 2) + b;   },   /**    * Snap in elastic effect.    * @method elasticIn    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} p Period (optional)    * @return {Number} The computed value for the current animation frame    */   elasticIn: function (t, b, c, d, a, p) {   	if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;   	if (!a || a < Math.abs(c)) { a=c; var s=p/4; }   	else var s = p/(2*Math.PI) * Math.asin (c/a);   	return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;   },   /**    * Snap out elastic effect.    * @method elasticOut    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} p Period (optional)    * @return {Number} The computed value for the current animation frame    */   elasticOut: function (t, b, c, d, a, p) {   	if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;   	if (!a || a < Math.abs(c)) { a=c; var s=p/4; }   	else var s = p/(2*Math.PI) * Math.asin (c/a);   	return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;   },   /**    * Snap both elastic effect.    * @method elasticBoth    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} p Period (optional)    * @return {Number} The computed value for the current animation frame    */   elasticBoth: function (t, b, c, d, a, p) {   	if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);   	if (!a || a < Math.abs(c)) { a=c; var s=p/4; }   	else var s = p/(2*Math.PI) * Math.asin (c/a);   	if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;   	return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;   },   /**    * Backtracks slightly, then reverses direction and moves to end.    * @method backIn    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} s Overshoot (optional)    * @return {Number} The computed value for the current animation frame    */   backIn: function (t, b, c, d, s) {   	if (typeof s == 'undefined') s = 1.70158;   	return c*(t/=d)*t*((s+1)*t - s) + b;   },   /**    * Overshoots end, then reverses and comes back to end.    * @method backOut    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} s Overshoot (optional)    * @return {Number} The computed value for the current animation frame    */   backOut: function (t, b, c, d, s) {   	if (typeof s == 'undefined') s = 1.70158;   	return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;   },   /**    * Backtracks slightly, then reverses direction, overshoots end,    * then reverses and comes back to end.    * @method backBoth    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @param {Number} s Overshoot (optional)    * @return {Number} The computed value for the current animation frame    */   backBoth: function (t, b, c, d, s) {   	if (typeof s == 'undefined') s = 1.70158;   	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;   	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;   },   /**    * Bounce off of start.    * @method bounceIn    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   bounceIn: function (t, b, c, d) {   	return c - YAHOO.util.Easing.bounceOut(d-t, 0, c, d) + b;   },   /**    * Bounces off end.    * @method bounceOut    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   bounceOut: function (t, b, c, d) {   	if ((t/=d) < (1/2.75)) {   		return c*(7.5625*t*t) + b;   	} else if (t < (2/2.75)) {   		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;   	} else if (t < (2.5/2.75)) {   		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;   	} else {   		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;   	}   },   /**    * Bounces off start and end.    * @method bounceBoth    * @param {Number} t Time value used to compute current value    * @param {Number} b Starting value    * @param {Number} c Delta between start and end values    * @param {Number} d Total length of animation    * @return {Number} The computed value for the current animation frame    */   bounceBoth: function (t, b, c, d) {   	if (t < d/2) return YAHOO.util.Easing.bounceIn(t*2, 0, c, d) * .5 + b;   	return YAHOO.util.Easing.bounceOut(t*2-d, 0, c, d) * .5 + c*.5 + b;   }};/** * Anim subclass for moving elements along a path defined by the "points" * member of "attributes".  All "points" are arrays with x, y coordinates. * <p>Usage: <code>var myAnim = new YAHOO.util.Motion(el, { points: { to: [800, 800] } }, 1, YAHOO.util.Easing.easeOut);</code></p> * @class Motion * @namespace YAHOO.util * @requires YAHOO.util.Anim * @requires YAHOO.util.AnimMgr * @requires YAHOO.util.Easing * @requires YAHOO.util.Bezier * @requires YAHOO.util.Dom * @requires YAHOO.util.Event * @requires YAHOO.util.CustomEvent * @constructor * @extends YAHOO.util.Anim * @param {String | HTMLElement} el Reference to the element that will be animated * @param {Object} attributes The attribute(s) to be animated. * Each attribute is an object with at minimum a "to" or "by" member defined. * Additional optional members are "from" (defaults to current value), "units" (defaults to "px"). * All attribute names use camelCase. * @param {Number} duration (optional, defaults to 1 second) Length of animation (frames or seconds), defaults to time-based * @param {Function} method (optional, defaults to YAHOO.util.Easing.easeNone) Computes the values that are applied to the attributes per frame (generally a YAHOO.util.Easing method) */(function() {   YAHOO.util.Motion = function(el, attributes, duration,  method) {      if (el) { // dont break existing subclasses not using YAHOO.extend         YAHOO.util.Motion.superclass.constructor.call(this, el, attributes, duration, method);      }   };   YAHOO.extend(YAHOO.util.Motion, YAHOO.util.ColorAnim);   // shorthand   var Y = YAHOO.util;   var superclass = Y.Motion.superclass;   var proto = Y.Motion.prototype;   proto.toString = function() {      var el = this.getEl();      var id = el.id || el.tagName;      return ("Motion " + id);   };   proto.patterns.points = /^points$/i;   proto.setAttribute = function(attr, val, unit) {      if (  this.patterns.points.test(attr) ) {         unit = unit || 'px';         superclass.setAttribute.call(this, 'left', val[0], unit);         superclass.setAttribute.call(this, 'top', val[1], unit);      } else {         superclass.setAttribute.call(this, attr, val, unit);      }   };   proto.getAttribute = function(attr) {      if (  this.patterns.points.test(attr) ) {         var val = [            superclass.getAttribute.call(this, 'left'),            superclass.getAttribute.call(this, 'top')         ];      } else {         val = superclass.getAttribute.call(this, attr);      }      return val;   };   proto.doMethod = function(attr, start, end) {      var val = null;      if ( this.patterns.points.test(attr) ) {         var t = this.method(this.currentFrame, 0, 100, this.totalFrames) / 100;         val = Y.Bezier.getPosition(this.runtimeAttributes[attr], t);      } else {         val = superclass.doMethod.call(this, attr, start, end);      }      return val;   };   proto.setRuntimeAttribute = function(attr) {      if ( this.patterns.points.test(attr) ) {         var el = this.getEl();         var attributes = this.attributes;         var start;         var control = attributes['points']['control'] || [];         var end;         var i, len;         if (control.length > 0 && !(control[0] instanceof Array) ) { // could be single point or array of points            control = [control];         } else { // break reference to attributes.points.control            var tmp = [];            for (i = 0, len = control.length; i< len; ++i) {               tmp[i] = control[i];            }            control = tmp;         }         if (Y.Dom.getStyle(el, 'position') == 'static') { // default to relative            Y.Dom.setStyle(el, 'position', 'relative');         }         if ( isset(attributes['points']['from']) ) {            Y.Dom.setXY(el, attributes['points']['from']); // set position to from point         }         else { Y.Dom.setXY( el, Y.Dom.getXY(el) ); } // set it to current position         start = this.getAttribute('points'); // get actual top & left         // TO beats BY, per SMIL 2.1 spec         if ( isset(attributes['points']['to']) ) {            end = translateValues.call(this, attributes['points']['to'], start);            var pageXY = Y.Dom.getXY(this.getEl());            for (i = 0, len = control.length; i < len; ++i) {               control[i] = translateValues.call(this, control[i], start);            }         } else if ( isset(attributes['points']['by']) ) {            end = [ start[0] + attributes['points']['by'][0], start[1] + attributes['points']['by'][1] ];            for (i = 0, len = control.length; i < len; ++i) {               control[i] = [ start[0] + control[i][0], start[1] + control[i][1] ];            }         }         this.runtimeAttributes[attr] = [start];         if (control.length > 0) {            this.runtimeAttributes[attr] = this.runtimeAttributes[attr].concat(control);         }         this.runtimeAttributes[attr][this.runtimeAttributes[attr].length] = end;      }      else {         superclass.setRuntimeAttribute.call(this, attr);      }   };   var translateValues = function(val, start) {      var pageXY = Y.Dom.getXY(this.getEl());      val = [ val[0] - pageXY[0] + start[0], val[1] - pageXY[1] + start[1] ];      return val;   };   var isset = function(prop) {      return (typeof prop !== 'undefined');   };})();/** * Anim subclass for scrolling elements to a position defined by the "scroll" * member of "attributes".  All "scroll" members are arrays with x, y scroll positions. * <p>Usage: <code>var myAnim = new YAHOO.util.Scroll(el, { scroll: { to: [0, 800] } }, 1, YAHOO.util.Easing.easeOut);</code></p> * @class Scroll * @namespace YAHOO.util * @requires YAHOO.util.Anim * @requires YAHOO.util.AnimMgr * @requires YAHOO.util.Easing * @requires YAHOO.util.Bezier * @requires YAHOO.util.Dom * @requires YAHOO.util.Event * @requires YAHOO.util.CustomEvent * @extends YAHOO.util.Anim * @constructor * @param {String or HTMLElement} el Reference to the element that will be animated * @param {Object} attributes The attribute(s) to be animated. * Each attribute is an object with at minimum a "to" or "by" member defined. * Additional optional members are "from" (defaults to current value), "units" (defaults to "px"). * All attribute names use camelCase. * @param {Number} duration (optional, defaults to 1 second) Length of animation (frames or seconds), defaults to time-based * @param {Function} method (optional, defaults to YAHOO.util.Easing.easeNone) Computes the values that are applied to the attributes per frame (generally a YAHOO.util.Easing method) */(function() {   YAHOO.util.Scroll = function(el, attributes, duration,  method) {      if (el) { // dont break existing subclasses not using YAHOO.extend         YAHOO.util.Scroll.superclass.constructor.call(this, el, attributes, duration, method);      }   };   YAHOO.extend(YAHOO.util.Scroll, YAHOO.util.ColorAnim);   // shorthand   var Y = YAHOO.util;   var superclass = Y.Scroll.superclass;   var proto = Y.Scroll.prototype;   proto.toString = function() {      var el = this.getEl();      var id = el.id || el.tagName;      return ("Scroll " + id);   };   proto.doMethod = function(attr, start, end) {      var val = null;      if (attr == 'scroll') {         val = [            this.method(this.currentFrame, start[0], end[0] - start[0], this.totalFrames),            this.method(this.currentFrame, start[1], end[1] - start[1], this.totalFrames)         ];      } else {         val = superclass.doMethod.call(this, attr, start, end);      }      return val;   };   proto.getAttribute = function(attr) {      var val = null;      var el = this.getEl();      if (attr == 'scroll') {         val = [ el.scrollLeft, el.scrollTop ];      } else {         val = superclass.getAttribute.call(this, attr);      }      return val;   };   proto.setAttribute = function(attr, val, unit) {      var el = this.getEl();      if (attr == 'scroll') {         el.scrollLeft = val[0];         el.scrollTop = val[1];      } else {         superclass.setAttribute.call(this, attr, val, unit);      }   };})();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?