jQuery 動畫
jQuery 動畫 - animate() 方法
jQuery animate() 方法用于創(chuàng)建自定義動畫。
語法:
$(selector).animate({params},speed,callback);
必需的 params 參數(shù)定義形成動畫的 CSS 屬性。
可選的 speed 參數(shù)規(guī)定效果的時長。它可以取以下值:"slow"、"fast" 或毫秒。
可選的 callback 參數(shù)是動畫完成后所執(zhí)行的函數(shù)名稱。
下面的例子演示 animate() 方法的簡單應(yīng)用。它把 <div> 元素往右邊移動了 250 像素:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({left:'250px'}); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
運行程序嘗試一下
提示:默認(rèn)情況下,所有的 HTML 元素有一個靜態(tài)的位置,且是不可移動的。如果需要改變?yōu)椋覀冃枰獙⒃氐?position 屬性設(shè)置為 relative, fixed, 或 absolute!
jQuery animate() - 操作多個屬性
請注意,生成動畫的過程中可同時使用多個屬性:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:'250px', opacity:'0.5', height:'150px', width:'150px' }); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#4ae936;height:100px;width:100px;position:absolute;"> </div> </body> </html>
運行程序嘗試一下
jQuery animate() - 使用相對值
也可以定義相對值(該值相對于元素的當(dāng)前值)。需要在值的前面加上 += 或 -=:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:'250px', height:'+=150px', width:'+=150px' }); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
運行程序嘗試一下
jQuery animate() - 使用預(yù)定義的值
您甚至可以把屬性的動畫值設(shè)置為 "show"、"hide" 或 "toggle":
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ height:'toggle' }); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
運行程序嘗試一下
jQuery animate() - 使用隊列功能
默認(rèn)地,jQuery 提供針對動畫的隊列功能。
這意味著如果您在彼此之后編寫多個 animate() 調(diào)用,jQuery 會創(chuàng)建包含這些方法調(diào)用的"內(nèi)部"隊列。然后逐一運行這些 animate 調(diào)用。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var div=$("div"); div.animate({height:'300px',opacity:'0.4'},"slow"); div.animate({width:'300px',opacity:'0.8'},"slow"); div.animate({height:'100px',opacity:'0.4'},"slow"); div.animate({width:'100px',opacity:'0.8'},"slow"); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> </div> </body> </html>
運行程序嘗試一下
下面的例子把 <div> 元素往右邊移動了 100 像素,然后增加文本的字號:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var div=$("div"); div.animate({left:'100px'},"slow"); div.animate({fontSize:'3em'},"slow"); }); }); </script> </head> <body> <button>開始動畫</button> <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div> </body> </html>
運行程序嘗試一下