一個(gè)簡單的js動(dòng)畫效果代碼
未實(shí)現(xiàn)的功能:無法實(shí)現(xiàn)單位換算,不能實(shí)現(xiàn)顏色漸變(還有很多,有待完善...)
代碼中的使用方法如下:
var $m = $M("divAnimate");
_("btnAnimate").onclick = function(){
this.disabled = true;
var that = this;
$m.animate({left:"300px",top:"100px",width:"300px", height:"200px",opacity:0.3},{duration:5000,easing:Tween.Back.easeInOut}).delay(200)
.animate({left:"50px",top:"300px",width:"150px",height:"100px",opacity:0.8},{duration:2000,easing:Tween.Elastic.easeInOut,
callback:function(){
that.disabled = false;
}
});
}
_("btnPause").onclick = function(){
$m.pause();
this.disabled = true;
_("btnResume").disabled = false;
}
_("btnResume").disabled = true;
_("btnResume").onclick = function(){
$m.resume();
this.disabled = true;
_("btnPause").disabled = false;
}
_("btnStop").onclick = function(){
$m.stop();
_("btnAnimate").disabled = false;
}
功能實(shí)現(xiàn):
/* 簡單動(dòng)畫方法
* 未實(shí)現(xiàn)單位換算
*/
var $M = function(obj){
var elem = ("string" === typeof obj )?document.getElementById(obj):obj;
var _this = {},props = {},timeId,isBusy = false,isPause = false;
var queue = [],_current;
//直線運(yùn)動(dòng)算法
function Linear(t,b,c,d){ return c*t/d + b;}
function setCss(className,value){
if(className == "opacity"){
if(document.defaultView){
elem.style["opacity"] = value;
} else {
elem.style["filter"] = 'alpha(opacity=' + 100 * value + ')';
}
} else {
elem.style[className] = value;
}
}
function getCss(className){
if(className == "opacity"){
var ret = "";
if(document.defaultView){
ret = document.defaultView.getComputedStyle(elem,null)['opacity'] || 1;
} else {
ret = elem.currentStyle['filter']?(elem.currentStyle['filter'].match(/^alpha\(opacity=([\d\.]+)\)$/))[1]/100 : 1;
}
return ret.toString();
} else {
return elem.style[className].toString();
}
}
function _move(params,easing,st,ht,callback){
var t = ((new Date()).getTime() - st);
_current.t = t;
if(isPause){return;}
easing = easing||Linear;
ht = ht || 500;
for(var p in params){
if(!props[p]){
var iv = parseFloat(getCss(p)) || 0;
var ev = parseFloat(params[p]);
props[p] = {
iv:iv,
iu:iv?getCss(p).substring(iv.toString().length):null,
ev:ev,
eu:params[p].toString().substring(ev.toString().length)
}
//TODO(初始值的單位和目標(biāo)值的單位不相同需要處理)
}
if(t >= ht){t = ht;}
var nv = easing(t,props[p].iv,(props[p].ev - props[p].iv),ht);
nv = parseFloat(nv);
setCss(p,nv + props[p].eu);
}
if(t < ht){
timeId = setTimeout(function(){
_move(params,easing,st,ht,callback);
},13);
} else {
props = {};
isBusy = false;
if(callback){
callback();
}
run();
}
}
function run(){
if(!isBusy && queue.length != 0){
var o = queue.shift();
var _delay = 0;
while(o && o.delay){
_delay += o.delay;
o = queue.shift();
}
if(o){
_current = o;
isBusy = true;
setTimeout(function(){
var st = (new Date()).getTime();
_move(o.params,o.options.easing,st,o.options.duration,o.options.callback);
},_delay);
}
}
}
var _this = {
animate:function(params,options){
queue.push({params:params,options:options});
isPause = false;
run();
return _this;
},
delay:function(ms){
queue.push({delay:ms});
return _this;
},
pause:function(){
isPause = true;
return _this;
},
resume:function(){
if(_current){
var o = _current;
isPause = false;
var st = (new Date()).getTime() - _current.t;
_move(o.params,o.options.easing,st,o.options.duration,o.options.callback);
return _this;
}
},
stop:function(){
isPause = true;
isBusy = false;
queue = [];
props = {};
return _this;
}
};
return _this;
}
參考地址:
http://chabaoo.cn/article/24309.htm
- JS動(dòng)畫效果代碼2
- JS動(dòng)畫效果代碼3
- js動(dòng)畫效果打開層 關(guān)閉層
- 用js實(shí)現(xiàn)的模擬jquery的animate自定義動(dòng)畫(2.5K)
- 用js模擬JQuery的show與hide動(dòng)畫函數(shù)代碼
- 圖片的左右移動(dòng),js動(dòng)畫效果實(shí)現(xiàn)代碼
- 在iframe里的頁面編寫js,實(shí)現(xiàn)在父窗口上創(chuàng)建動(dòng)畫效果展開和收縮的div(不變動(dòng)iframe父窗口代碼)
- js 排序動(dòng)畫模擬 冒泡排序
- JS判斷頁面加載狀態(tài)以及添加遮罩和緩沖動(dòng)畫的代碼
- js動(dòng)畫(animate)簡單引擎代碼示例
- js運(yùn)動(dòng)動(dòng)畫的八個(gè)知識(shí)點(diǎn)
- 九種原生js動(dòng)畫效果
相關(guān)文章
JavaScript 自動(dòng)完成腳本整理(33個(gè))
所謂的提升用戶體驗(yàn),其實(shí)就是把所有用戶視為極度弱智和超級(jí)懶鬼。比如JavaScript自動(dòng)完成(Autocomplete)腳本, 常用于表單,用戶只需輸入一兩個(gè)字母,就為你擴(kuò)展、聯(lián)想、匹配和供君選擇。2009-10-10在javascript中執(zhí)行任意html代碼的方法示例解讀
關(guān)于javascript的eval()函數(shù)無法執(zhí)行html代碼的問題,下面為大家介紹下一種在javascript中執(zhí)行任意html代碼的方法,感興趣的朋友不要錯(cuò)過2013-12-12基于aotu.js實(shí)現(xiàn)微信自動(dòng)添加通訊錄中的聯(lián)系人功能
這篇文章主要介紹了利用aotu.js實(shí)現(xiàn)微信自動(dòng)添加通訊錄中的聯(lián)系人,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05JavaScript實(shí)現(xiàn)網(wǎng)頁播放器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09cocos creator Touch事件應(yīng)用(觸控選擇多個(gè)子節(jié)點(diǎn)的實(shí)例)
下面小編就為大家?guī)硪黄猚ocos creator Touch事件應(yīng)用(觸控選擇多個(gè)子節(jié)點(diǎn)的實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09淺談 Webpack 如何處理圖片(開發(fā)、打包、優(yōu)化)
這篇文章主要介紹了淺談 Webpack 如何處理圖片(開發(fā)、打包、優(yōu)化),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Cordova(ionic)項(xiàng)目實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Cordova項(xiàng)目實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Javascript 的addEventListener()及attachEvent()區(qū)別分析
大家都知道事件的用法就是當(dāng)某個(gè)事件(狀況)被觸發(fā)了之后就會(huì)去執(zhí)行某個(gè)Function, 尤其是Javascript, 在當(dāng)紅AJAX的催化下, 了解Javascript的Event用法更加重要, 在這里就大概介紹一下avascript的Event用法.2009-05-05淺談JavaScript函數(shù)參數(shù)的可修改性問題
這篇文章主要是對(duì)JavaScript函數(shù)參數(shù)的可修改性進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12canvas實(shí)現(xiàn)弧形可拖動(dòng)進(jìn)度條效果
本篇文章主要介紹了canvas實(shí)現(xiàn)弧形可拖動(dòng)進(jìn)度條的實(shí)例方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05