程序源码
function Animate(el, prop, opts) {
this.el = 엘;
this.prop = prop;
this.from = opts.from;
this.to = opts.to;
this.time = opts.time;
this.callback = opts.callback;
this.animDiff = this.to - this.from;
}
Animate.prototype._setStyle = function(val) {
switch(this.prop) {
case 'opacity':
this.el.style[this. 소품] = 발;
this.el.style.filter = 'alpha(opacity=' val * 100 ')';
휴식;
기본값:
this.el.style[this.prop] = val 'px';
휴식;
}
}
Animate.prototype._animate = function() {
var that = this;
this.now = 새로운 날짜();
this.diff = this.now - this.startTime;
if (this.diff > this.time) {
this._setStyle(this.to);
if (this.callback) {
this.callback.call(this);
}
clearInterval(this.timer);
반환;
}
this.percentage = (Math.floor((this.diff / this.time) * 100) / 100);
this.val = (this.animDiff * this.percentage) this.from;
this._setStyle(this.val);
}
Animate.prototype.start = function() {
var that = this;
this.startTime = new Date();
clearInterval(this.timer);
this.timer = setInterval(function() {
that._animate.call(that);
}, 4);
}
Animate.canTransition = function() {
var el = document.createElement('foo');
el.style.cssText = '-webkit-transition: 모든 .5s 선형;';
return !!el.style.webkitTransitionProperty;
}();
이용방법
/ / 透명도渐变
functionchangeOpacity() {
// 透명도渐变 从1 - 0 渐变时间1000ms
var fx = 'opacity', from = 1, to = 0, time = 1000;
// 渐变完毕执行的回调函数
var callback = function() {
from = 0; = 1;
new Animate(demo, fx, { from: from, to: to, time: time, callback: ResetButton}).start();
}
// 实例化渐变函数
new Animate(demo, fx, {
from: from,
to: to,
time: time,
callback: 콜백
}).start();
}
演示代码: