如何使用 jQuery 動畫、隱藏和顯示元素?
與 animate() 方法一起使用 hide() 和 show() 方法來隱藏和顯示元素。
示例
你可以嘗試執行以下程式碼,瞭解如何使用 animate() 方法隱藏和顯示元素
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#btn1").click(function(){ $("#box").animate({height: "300px"}, 500, function() { $(this).hide(); }); }); $("#btn2").click(function(){ $("#box").animate({height: "300px"}, 500, function() { $(this).show(); }); }); }); </script> </head> <body> <button id="btn1">Hide</button> <button id="btn2">Show</button> <div id="box" style="background:#000000;height:300px;width:100px;margin:10px;"></div> </body> </html>
廣告