一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

javascript div 渐变

时间:2010-07-05 编辑:简简单单 来源:一聚教程网

// 透明度渐变
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();
}

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.css教程text = '-webkit-transition: all .5s linear;';
return !!el.style.webkittransitionproperty;
}();

热门栏目