javascript簡(jiǎn)易動(dòng)畫類(div漸變)
更新時(shí)間:2010年07月04日 23:02:48 作者:
javascript簡(jiǎn)易動(dòng)畫類 寬度漸變 高度漸變 透明度漸變 邊框漸變 綜合漸變
程序源碼
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 漸變時(shí)間1000ms
var fx = 'opacity', from = 1, to = 0, time = 1000;
// 漸變完畢執(zhí)行的回調(diào)函數(shù)
var callback = function() {
from = 0; to = 1;
new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton}).start();
}
// 實(shí)例化漸變函數(shù)
new Animate(demo, fx, {
from: from,
to: to,
time: time,
callback: callback
}).start();
}
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
復(fù)制代碼 代碼如下:
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;
}();
使用方法
復(fù)制代碼 代碼如下:
// 透明度漸變
function changeOpacity() {
// 透明度漸變 從1 - 0 漸變時(shí)間1000ms
var fx = 'opacity', from = 1, to = 0, time = 1000;
// 漸變完畢執(zhí)行的回調(diào)函數(shù)
var callback = function() {
from = 0; to = 1;
new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton}).start();
}
// 實(shí)例化漸變函數(shù)
new Animate(demo, fx, {
from: from,
to: to,
time: time,
callback: callback
}).start();
}
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
您可能感興趣的文章:
- 神奇!js+CSS+DIV實(shí)現(xiàn)文字顏色漸變效果
- js實(shí)現(xiàn)有過(guò)渡漸變效果的圖片輪播相冊(cè)(兼容IE,ff)
- JavaScript 漸變效果頁(yè)面圖片控制
- 純js和css實(shí)現(xiàn)漸變色包括靜態(tài)漸變和動(dòng)態(tài)漸變
- JS Tween 顏色漸變
- javascript漸變顯示的雅虎中國(guó)選項(xiàng)卡效果代碼
- javascript 線性漸變二
- javascript支持IE和firefox(FF)的漸變透明效果
- 利用遞增的數(shù)字返回循環(huán)漸變的顏色的js代碼
- 漂亮! js實(shí)現(xiàn)顏色漸變效果
相關(guān)文章
點(diǎn)擊按鈕后 文本框變?yōu)镾elect下拉列表框
在招聘類的網(wǎng)站會(huì)見(jiàn)到不少類似效果,文本框中最開(kāi)始默認(rèn)的是文字 ,如果點(diǎn)擊更改這類的按鈕文本框就變成了一個(gè)可以選擇的Select下拉列表框,挺有創(chuàng)意,以下是代碼,復(fù)制即可用啦。2009-09-09實(shí)用的層滑開(kāi)js實(shí)現(xiàn)代碼
一款實(shí)用簡(jiǎn)潔的層滑開(kāi)特效代碼,鼠標(biāo)放到“111”上,所對(duì)應(yīng)的層會(huì)緩沖展開(kāi),過(guò)會(huì)會(huì)自動(dòng)閉合起來(lái),兼容性好,各位有用到的自己美化一下。2009-11-11