| 方法 | 效果 | 參數 | 適用場景 |
|---|---|---|---|
show() | 直接顯示 | duration, easing, callback | 無動畫需求 |
hide() | 直接隱藏 | duration, easing, callback | 無動畫需求 |
toggle() | 切換顯示/隱藏 | duration, easing, callback | 開關切換 |
slideDown() | 向下滑動顯示 | duration, easing, callback | 下拉選單、面板展開 |
slideUp() | 向上滑動隱藏 | duration, easing, callback | 面板收合 |
slideToggle() | 滑動切換 | duration, easing, callback | 手風琴效果 |
fadeIn() | 淡入顯示 | duration, easing, callback | 模態框、提示框 |
fadeOut() | 淡出隱藏 | duration, easing, callback | 通知消失 |
fadeToggle() | 淡入淡出切換 | duration, easing, callback | 開關切換 |
'fast' (200ms)、'slow' (600ms)'swing' (預設,緩動)、'linear' (勻速)<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery 顯示隱藏動畫示範</title>
<style>
#demoBox {
width: 300px;
height: 150px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 8px;
display: none; /* 初始隱藏 */
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.btn-group { margin: 15px 0; }
button { margin-right: 8px; padding: 8px 16px; cursor: pointer; }
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h3>jQuery 顯示/隱藏動畫效果示範</h3>
<div class="btn-group">
<button onclick="showBox()">顯示隱藏切換滑下顯示滑上隱藏滑動切換淡入淡出淡入淡出切換
// 停止當前動畫,跳至終狀態
$('#box').stop(true, true).fadeIn();
// 只停止當前動畫,不執行佇列剩餘
$('#box').stop(false, false);
// 清除動畫佇列
$('#box').clearQueue().stop();
.fade-enter {
opacity: 0;
transition: opacity 0.3s ease;
}
.fade-enter-active {
opacity: 1;
}
.fade-leave {
opacity: 1;
transition: opacity 0.3s ease;
}
.fade-leave-active {
opacity: 0;
}
// JS 僅切換類別
$('#box').addClass('fade-enter-active').removeClass('fade-leave-active');
display:none 而非 JS .hide()transform: translateZ(0)max-height 技巧jQuery 提供豐富的顯示/隱藏動畫方法,slide 系適合垂直面板,fade 系適合模態層,toggle 系適合開關切換。現代專案建議優先使用 CSS Transition/Animation,JS 僅控制類別切換,效能更好且關注點分離。