- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- JavaScript如何實(shí)現自定義動(dòng)畫(huà)
這篇文章將為大家詳細講解有關(guān)JavaScript如何實(shí)現自定義動(dòng)畫(huà),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
自定義動(dòng)畫(huà)用到的幾個(gè)框架函數:
$("Element").animate(params[,duration[,easing[,callback]]])
[quote]用于創(chuàng )建自定義動(dòng)畫(huà)的函數。
這個(gè)函數的關(guān)鍵在于指定動(dòng)畫(huà)形式及結果樣式屬性對象。這個(gè)對象中每個(gè)屬性都表示一個(gè)可以變化的樣式屬性(如“height”、“top”或“opacity”)。
注意:所有指定的屬性必須用駱駝形式,比如用marginLeft代替margin-left,如果有不懂得駱駝命名法的朋友請看三種通用CSS規范化命名的規則。
而每個(gè)屬性的值表示這個(gè)樣式屬性到多少時(shí)動(dòng)畫(huà)結束。如果是一個(gè)數值,樣式屬性就會(huì )從當前的值漸變到指定的值。如果使用的是“hide”、“show”或“toggle”這樣的字符串值,則會(huì )為該屬性調用默認的動(dòng)畫(huà)形式。
params
(Options) : 一組包含作為動(dòng)畫(huà)屬性和終值的樣式屬性和及其值的集合
duration (String,Number) : (可選) 三種預定速度之一的字符串("slow", "normal", or "fast")或表示動(dòng)畫(huà)時(shí)長(cháng)的毫秒數值(如:1000)
easing (String) : (可選) 要使用的擦除效果的名稱(chēng)(需要插件支持).默認jQuery提供"linear" 和 "swing".
callback (Function) : (可選) 在動(dòng)畫(huà)完成時(shí)執行的函數
$("Element").animate(params,options)
同上
params (Options)
: 一組包含作為動(dòng)畫(huà)屬性和終值的樣式屬性和及其值的集合
options (Options)
: 一組包含動(dòng)畫(huà)選項的值的集合
$("Element").stop()
停止指定元素上正在運行的動(dòng)畫(huà)
$("Element").queue()
返回指向第一個(gè)匹配元素的隊列,常與length配合使用;可以將其理解為數組,一個(gè)動(dòng)畫(huà)數組中包含了好幾個(gè)效果,queue().length表示獲得當前所執行的第一個(gè)效果。
通過(guò)以上函數實(shí)現自定義動(dòng)畫(huà)效果:
(1)實(shí)現一個(gè)動(dòng)畫(huà)queue,在循環(huán)展現每個(gè)動(dòng)畫(huà):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>第二十九節jQuery作業(yè)</title> <script language="javascript" src="js/jquery-1.3.2.js"></script> <script language="javascript" charset="GB2312"> $(function() { $("#show").click(function() { var n = $("div").queue(); $("span").text("Queue length is: " + $("div").queue().length); }); runIt(); }); function runIt() { $("div").show("slow"); $("div").animate({ left : '+=200' }, 2000); $("div").slideToggle(1000); $("div").slideToggle("fast"); $("div").animate({ left : '-=200' }, 1500); $("div").hide("slow"); $("div").show(1200); $("div").slideUp("normal", runIt); } </script> <style> div { margin: 3px; width: 40px; height: 40px; position: absolute; left: 0px; top: 30px; background: green; display: none; } div.newcolor { background: blue; } span { color: red; } </style> </head> <body> <button id="show">Show Length of Queue</button> <span></span> <div></div> </body> </html>
(2)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>第二十九節jQuery作業(yè)</title> <script language="javascript" src="js/jquery-1.3.2.js"></script> <script language="javascript" charset="GB2312"> $(function() { $(document.body).click(function() { $("div").show("slow"); $("div").animate({ left : '+=200' }, 2000); $("div").queue(function() { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({ left : '-=200' }, 500); $("div").queue(function() { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }) }); </script> <style> div { margin: 3px; width: 40px; height: 40px; position: absolute; left: 0px; top: 30px; background: green; display: none; } div.newcolor { background: blue; } </style> </head> <body> Click here... <div></div> </body> </html>
(3)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>第二十九節jQuery作業(yè)</title> <script language="javascript" src="js/jquery-1.3.2.js"></script> <script language="javascript" charset="GB2312"> $(function() { $("#start").click(function() { $("div").show("slow"); $("div").animate({ left : '+=200' }, 5000); $("div").queue(function() { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({ left : '-=200' }, 1500); $("div").queue(function() { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }) $("#stop").click(function() { $("div").queue("fx", []); $("div").stop(); }) }); </script> <style> div { margin: 3px; width: 40px; height: 40px; position: absolute; left: 0px; top: 30px; background: green; display: none; } div.newcolor { background: blue; } </style> </head> <body> <button id="start">Start</button> <button id="stop">Stop</button> <div></div> </body> </html>
(4)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>第二十九節jQuery作業(yè)</title> <script language="javascript" src="js/jquery-1.3.2.js"></script> <script language="javascript" charset="GB2312"> $(function() { $("button").click(function() { $("div").animate({ left : '+=200px' }, 2000); $("div").animate({ top : '0px' }, 600); $("div").queue(function() { $(this).toggleClass("red"); $(this).dequeue(); }); $("div").animate({ left : '10px', top : '30px' }, 700); }); }); </script> <style> div { margin: 3px; width: 50px; position: absolute; height: 50px; left: 10px; top: 30px; background-color: yellow; } div.red { background-color: red; } </style> </head> <body> <button>Start</button> <div></div> </body> </html>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站