本文示範如何使用 jQuery 的 fadeIn() 方法,讓指定元素(如色塊)逐漸淡入顯示。
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn(); // 預設速度
$("#div2").fadeIn("slow"); // 慢速
$("#div3").fadeIn(3000); // 3000 毫秒
});
});
</script>
</head>
<body>
<p>Demonstrate fadeIn() with different parameters.</p>
<button>Click to fade in boxes</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
三個色塊初始都設為 display:none,點擊後 fadeIn() 會以淡入動畫把它們逐漸顯示出來。它與 fadeOut() 正好相反,速度參數的設定方式也相同。希望本文對大家的 jQuery 開發有所幫助。