程序源码
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();
}
演示代码:
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
-
安全整改思考之道
最近项目组安全整改,然后需要把所有的安全隐患问题一扫而空,其中也踩了不少的坑,记录下来,以便后续遇到类似问题能立...
[阅读全文]
-
网友评论