Web開發——JavaScript庫(jQuery效果——動畫/停止動畫)
jQuery animate() 方法允許您創建自定義的動畫。
1、jQuery 動畫 - animate() 方法
jQuery animate() 方法用於創建自定義動畫。
語法:
1 $(selector).animate({params},speed,callback);
必需的 params 參數定義形成動畫的 CSS 屬性。
可選的 speed 參數規定效果的時長。它可以取以下值:"slow"、"fast" 或毫秒。
可選的 callback 參數是動畫完成後所執行的函數名稱。
下面的例子演示 animate() 方法的簡單應用;它把 <div> 元素移動到左邊,直到 left 屬性等於 250 像素為止:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("div").animate({left:‘250px‘}); 16 }); 17 }); 18 19 </script> 20 21 </head> 22 23 <body> 24 25 <button>開始動畫</button> 26 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 27 <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> 28 </div> 29 30 </body> 31 </html>
輸出結果:點擊按鈕,div元素區域會出現從左向右的動畫。
提示:默認地,所有 HTML 元素都有一個靜態位置,且無法移動。
如需對位置進行操作,要記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute!
1.1 jQuery animate() - 操作多個屬性
請註意,生成動畫的過程中可同時使用多個屬性:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("div").animate({ 16 left:‘250px‘, 17 opacity:‘0.5‘, 18 height:‘150px‘, 19 width:‘150px‘ 20 }); 21 }); 22 }); 23 24 </script> 25 26 </head> 27 28 <body> 29 30 <button>開始動畫</button> 31 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 32 <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> 33 </div> 34 35 </body> 36 </html>
提示:可以用 animate() 方法來操作所有 CSS 屬性嗎?
是的,幾乎可以!不過,需要記住一件重要的事情:當使用 animate() 時,必須使用 Camel 標記法書寫所有的屬性名,比如,必須使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margin-right,等等。
同時,色彩動畫並不包含在核心 jQuery 庫中。
如果需要生成顏色動畫,您需要從 jQuery.com 下載 Color Animations 插件。
1.2 jQuery animate() - 使用相對值
也可以定義相對值(該值相對於元素的當前值)。需要在值的前面加上 += 或 -=:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("div").animate({ 16 left:‘250px‘, 17 height:‘+=150px‘, 18 width:‘+=150px‘ 19 }); 20 }); 21 }); 22 23 </script> 24 25 </head> 26 27 <body> 28 29 <button>開始動畫</button> 30 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 31 <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> 32 </div> 33 34 </body> 35 </html>
1.3 jQuery animate() - 使用預定義的值
您甚至可以把屬性的動畫值設置為 "show"、"hide" 或 "toggle":
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $("div").animate({ 16 height:‘toggle‘ 17 }); 18 }); 19 }); 20 21 </script> 22 23 </head> 24 25 <body> 26 27 <button>開始動畫</button> 28 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 29 <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> 30 </div> 31 32 </body> 33 </html>
1.4 jQuery animate() - 使用隊列功能
默認地,jQuery 提供針對動畫的隊列功能。
這意味著如果您在彼此之後編寫多個 animate() 調用,jQuery 會創建包含這些方法調用的“內部”隊列。然後逐一運行這些 animate 調用。
舉例1(隱藏,如果您希望在彼此之後執行不同的動畫,那麽我們要利用隊列功能):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 var div = $("div"); 16 div.animate({height:‘300px‘,opacity:‘0.4‘},"slow"); 17 div.animate({width:‘300px‘,opacity:‘0.8‘},"slow"); 18 div.animate({height:‘100px‘,opacity:‘0.4‘},"slow"); 19 div.animate({width:‘100px‘,opacity:‘0.8‘},"slow"); 20 }); 21 }); 22 23 </script> 24 25 </head> 26 27 <body> 28 29 <button>開始動畫</button> 30 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 31 <div style="background:#98bf21;height:100px;width:100px;position:absolute;"> 32 </div> 33 34 </body> 35 </html>
舉例2(下面的例子把 <div> 元素移動到右邊,然後增加文本的字號):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <script src="jquery-3.3.1.js"></script> 12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 var div=$("div"); 16 div.animate({left:‘100px‘},"slow"); 17 div.animate({fontSize:‘3em‘},"slow"); 18 }); 19 }); 20 21 </script> 22 23 </head> 24 25 <body> 26 27 <button>開始動畫</button> 28 <p>默認情況下,所有 HTML 元素的位置都是靜態的,並且無法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p> 29 <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div> 30 31 </body> 32 </html>
2、jQuery 停止動畫 - animate() 方法
(添加草稿中內容)
3、jQuery 效果參考手冊
如需全面查閱 jQuery 效果,請訪問 jQuery 效果參考手冊。
Web開發——JavaScript庫(jQuery效果——動畫/停止動畫)