js實(shí)現(xiàn)類似jquery里animate動(dòng)畫效果的方法
本文實(shí)例講述了js實(shí)現(xiàn)類似jquery里animate動(dòng)畫效果的方法。分享給大家供大家參考。具體分析如下:
該實(shí)例可實(shí)現(xiàn)鼠標(biāo)移上,先寬度變化,再高度變化,最后透明度變化,鼠標(biāo)移出,再依次變回去的效果。
要點(diǎn)一:
startrun(obj,attr,target,fn) box.onmouseover = function(){ startrun(box,"width",200,function(){ startrun(box,"height",200,function(){ startrun(box,"opacity","100") }); }); }
如上面,函數(shù)也可以做為參數(shù)使用,就可以達(dá)到先執(zhí)行某個(gè)動(dòng)作,再執(zhí)行某個(gè)動(dòng)作的效果了。
要點(diǎn)二:
if(cur == target){ clearInterval(obj.timer); if(fn){ fn(); } }
當(dāng)運(yùn)動(dòng)到達(dá)目標(biāo)點(diǎn),關(guān)閉定時(shí)器,然后就可以執(zhí)行新的函數(shù)了。
最后,上代碼:
<!DOCTYPE html> <html> <head> <meta charset="gb2312" /> <title>無標(biāo)題文檔</title> <style> <!-- body{margin:0; padding:0; font:12px/1.5 arial;} #box{width:100px; height:100px; position:absolute; background:#06c; left:0;filter:alpha(opacity=30); opacity:0.3;} --> </style> <script> <!-- function getstyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; }else{ return getComputedStyle(obj,false)[name]; } } window.onload = function(){ var box = document.getElementById("box"); box.onmouseover = function(){ startrun(box,"width",200,function(){ startrun(box,"height",200,function(){ startrun(box,"opacity","100") }); }); } box.onmouseout = function(){ startrun(box,"height",100,function(){ startrun(box,"width",100,function(){ startrun(box,"opacity","30"); }); }); } } function startrun(obj,attr,target,fn){ clearInterval(obj.timer); obj.timer = setInterval(function(){ var cur = 0; if(attr == "opacity"){ cur = Math.round(parseFloat(getstyle(obj,attr))*100); }else{ cur = parseInt(getstyle(obj,attr)); } var speed = (target-cur)/8; speed = speed>0?Math.ceil(speed):Math.floor(speed); if(cur == target){ clearInterval(obj.timer); if(fn){ fn(); } }else{ if(attr == "opacity"){ obj.style.filter = "alpha(opacity="+(cur+speed)+")"; obj.style.opacity = (cur+speed)/100; }else{ obj.style[attr] = cur + speed + "px"; } } },30) } //--> </script> </head> <body> <div id="box"> </div> </body> </html>
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
- 原生JS封裝animate運(yùn)動(dòng)框架的實(shí)例
- jQuery animate()實(shí)現(xiàn)背景色漸變效果的處理方法【使用jQuery.color.js插件】
- 淺談原生JS實(shí)現(xiàn)jQuery的animate()動(dòng)畫示例
- 原生js實(shí)現(xiàn)jquery函數(shù)animate()動(dòng)畫效果的簡單實(shí)例
- 原生js仿jquery animate動(dòng)畫效果
- js動(dòng)畫(animate)簡單引擎代碼示例
- 用js實(shí)現(xiàn)的模擬jquery的animate自定義動(dòng)畫(2.5K)
- javascript?中動(dòng)畫制作方法?animate()屬性
相關(guān)文章
JavaScript中函數(shù)聲明優(yōu)先于變量聲明的實(shí)例分析
同一個(gè)標(biāo)示符,先后用var和function聲明它。最后它是什么呢2012-03-03Three.js+React使二維圖片呈現(xiàn)3D效果
這篇文章主要為大家介紹了如何利用Three.js+React技術(shù)棧,將二維漫畫圖片轉(zhuǎn)化為三維視覺效果。文中的實(shí)現(xiàn)方法講解詳細(xì),需要的可以參考一下2022-02-02深入JS函數(shù)中默認(rèn)參數(shù)的使用詳解
本文主要介紹了深入JS函數(shù)中默認(rèn)參數(shù)的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07基于JavaScript實(shí)現(xiàn)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01JavaScript設(shè)計(jì)模式之性能優(yōu)化模式享元模式
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之性能優(yōu)化模式享元模式,享元設(shè)計(jì)模式是用于性能優(yōu)化的模式,這種設(shè)計(jì)模式的核心在于可以共享技術(shù)并支持對大量細(xì)分過后的對象進(jìn)行調(diào)整,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06