javascript简易动画类(div渐变)

2022-10-18,,,,

程序源码
复制代码 代码如下:
function animate(el, prop, opts) {
this.el = 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.prop] = val;
this.el.style.filter = 'alpha(opacity=' + val * 100 + ')';
break;
default:
this.el.style[this.prop] = val + 'px';
break;
}
}

animate.prototype._animate = function() {
var that = this;
this.now = new date();
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);
return;
}

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: all .5s linear;';
return !!el.style.webkittransitionproperty;
}();

使用方法
复制代码 代码如下:
// 透明度渐变
function changeopacity() {
// 透明度渐变 从1 - 0 渐变时间1000ms
var fx = 'opacity', from = 1, to = 0, time = 1000;
// 渐变完毕执行的回调函数
var callback = function() {
from = 0; to = 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: callback
}).start();
}

演示代码:

[ctrl+a 全选 注:如需引入外部js需刷新才能执行]

《javascript简易动画类(div渐变).doc》

下载本文的Word格式文档,以方便收藏与打印。