jQuery - 動畫



讓我們學習如何使用 jQuery 的 animate() 方法在網頁或其他 jQuery(Javascript)應用程式上建立自定義動畫。

jQuery animate() 方法

jQuery 的 animate() 方法用於透過更改 DOM 元素的 CSS 數值屬性(例如,寬度、高度、邊距、填充、不透明度、頂部、左側等)來建立自定義動畫。

以下是 animate() 方法的簡單語法

$(selector).animate({ properties }, [speed, callback] );
jQuery animate() 方法不能用於動畫非數值屬性,例如顏色或背景顏色等。儘管您可以使用 jQuery 外掛 jQuery.Color 來動畫此類屬性。

您可以應用任何 jQuery 選擇器來選擇任何 DOM 元素,然後應用 jQuery animate() 方法對其進行動畫處理。以下是所有引數的描述,這些引數使您可以完全控制動畫:

  • 屬性 (properties) − 一個必需引數,定義要進行動畫處理的 CSS 屬性,這是呼叫中唯一必需的引數。

  • 速度 (speed) − 一個可選字串,表示三種預定義速度之一(“slow”、“normal”或“fast”)或執行動畫的毫秒數(例如 1000)。

  • 回撥函式 (callback) − 一個可選引數,表示在動畫完成時要執行的函式。

動畫先決條件

(a) - animate() 方法不會將隱藏元素作為效果的一部分顯示出來。例如,給定 $(selector).hide().animate({height: "20px"}, 500),動畫將執行,但元素將保持隱藏狀態。

(b) - 要將 DOM 元素的位置作為動畫的一部分進行操作,首先需要將其位置設定為 relativefixedabsolute,因為預設情況下,所有 HTML 元素都具有 static 位置,並且無法使用 animate() 方法移動它們。

示例

以下示例演示如何使用 animate() 方法將一個 <div> 元素向右移動,直到其 left 屬性達到 250px。接下來,當我們單擊按鈕時,相同的 <div> 元素將返回到其初始位置。

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
   $("#right").click(function(){
      $("div").animate({left: '250px'});
   });
   $("#left").click(function(){
      $("div").animate({left: '0px'});
   });
});
</script>
<style>
   #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
   #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}
</style>
</head>
<body>
   <p>Click on Left or Right button to see the result:</p>

   <div id="box"  style="background-color:#9c9cff;">This is Box</div>
   <button id="right" style="background-color:#fb7c7c;">Right Move</button>
   <button id="left" style="background-color:#93ff93;">Left Move</button>
</body>
</html>

具有自定義速度的動畫

我們可以使用不同的速度來動畫 DOM 元素的不同 CSS 數值屬性(例如,寬度、高度或左側)。

示例

讓我們重寫上面的示例,我們將使用 1000 毫秒的速度引數來動畫 <div> 的向右移動,並使用 5000 毫秒的速度引數來動畫向左移動。

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
   $("#right").click(function(){
      $("div").animate({left: '250px'}, 1000);
   });
   $("#left").click(function(){
      $("div").animate({left: '0px'}, 5000);
   });
});
</script>
<style>
   #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
   #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}
</style>
</head>
<body>
   <p>Click on Left or Right button to see the result:</p>

   <div id="box"  style="background-color:#9c9cff;">This is Box</div>
   <button id="right" style="background-color:#fb7c7c;">Right Move</button>
   <button id="left" style="background-color:#93ff93;">Left Move</button>
</body>
</html>

使用預定義值的動畫

我們可以使用字串 'show''hide''toggle' 作為 CSS 數值屬性的值。

示例

以下是一個示例,我們使用兩個按鈕將元素的 left 屬性設定為 hideshow

請注意,使用這些值設定任何數值 CSS 屬性都會產生相同的結果。例如,如果您將元素的寬度或高度設定為 hide,那麼它將隱藏元素,無論您設定的是其寬度屬性還是高度屬性。
<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
   $("#right").click(function(){
      $("div").animate({left: 'hide'});
   });
   $("#left").click(function(){
      $("div").animate({left: 'show'});
   });
});
</script>
<style>
   #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
   #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}
</style>
</head>
<body>
   <p>Click on Left or Right button to see the result:</p>

   <div id="box"  style="background-color:#9c9cff;">This is Box</div>
   <button id="right" style="background-color:#fb7c7c;">Right Move</button>
   <button id="left" style="background-color:#93ff93;">Left Move</button>
</body>
</html>

具有多個屬性的動畫

jQuery animate() 允許我們同時動畫元素的多個 CSS 屬性。

示例

以下是一個動畫 <div> 元素多個 CSS 屬性的示例。當我們單擊向右移動按鈕時,此 <div> 開始向右移動,直到其 left 屬性值為 250px,同時元素的不透明度降低到 0.2,並且框的寬度和高度減小到 100px。接下來,當我們單擊向左移動按鈕時,此框將返回到其初始位置和大小。

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
   $("#right").click(function(){
      $("div").animate({left: '250px', width:'100px', height:'100px', opacity:0.2});
   });
   $("#left").click(function(){
      $("div").animate({left: '0px',width:'180px', height:'100px', opacity:1.0});
   });
});
</script>
<style>
   #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
   #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}
</style>
</head>
<body>
   <p>Click on Left or Right button to see the result:</p>

   <div id="box"  style="background-color:#9c9cff;">This is Box</div>
   <button id="right" style="background-color:#fb7c7c;">Right Move</button>
   <button id="left" style="background-color:#93ff93;">Left Move</button>
</body>
</html>

具有佇列功能的動畫

考慮這樣一種情況:您需要應用多個動畫,這意味著您需要一次又一次地呼叫 animate() 方法。在這種情況下,jQuery 透過先進先出 (FIFO) 佇列處理這些動畫請求,並允許根據您的創意建立有趣的動畫。

示例

以下是一個示例,我們呼叫 animate() 方法 4 次,將 <div> 依次帶到多個方向。

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src="https://tutorialspoint.tw/jquery/jquery-3.6.0.js"></script>
<script>
$(document).ready(function() {
   $("button").click(function(){
      $("div").animate({left: '250px'});
      $("div").animate({top: '100px'});
      $("div").animate({left: '0px'});
      $("div").animate({top: '0px'});
   });
});
</script>
<style>
   button {margin:3px;border:2px solid #666; height:30px; width:180px;cursor:pointer;}
   #box{position:relative;margin:3px;padding:2px;border:2px solid #666;  height:30px; width:170px;}
</style>
</head>
<body>
   <p>Click on Start Animation button to see the result:</p>

   <div id="box"  style="background-color:#9c9cff;">This is Box</div>
   <button style="background-color:#93ff93;">Start Animation</button>
</body>
</html>

jQuery 效果參考

您可以在以下頁面獲取所有 jQuery 效果方法的完整參考:jQuery 效果參考

廣告