色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

javascript簡易動畫類(div漸變)

程序源碼
復制代碼 代碼如下:
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技術javascript簡易動畫類(div漸變),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 平远县| 景谷| 西藏| 民和| 海宁市| 金华市| 蒲城县| 满洲里市| 山东省| 柯坪县| 莱阳市| 顺昌县| 历史| 延津县| 时尚| 宿松县| 新平| 固阳县| 衡南县| 玉树县| 周至县| 徐汇区| 莆田市| 德安县| 怀来县| 庄河市| 洮南市| 河北区| 铜川市| 苍梧县| 北票市| 定陶县| 江阴市| 保山市| 铁岭县| 东光县| 铜鼓县| 横山县| 庆元县| 葫芦岛市| 建瓯市|