| 類別 | 方法 | 說明 |
|---|---|---|
| 顯示/隱藏 | show() / hide() / toggle() | 尺寸+透明度漸變,支援速度參數與回調 |
| 滑動 | slideDown() / slideUp() / slideToggle() | 高度漸變,適合手風琴選單 |
| 淡入/淡出 | fadeIn() / fadeOut() / fadeToggle() / fadeTo() | 透明度漸變,fadeTo 可指定目標透明度 |
| 自定義 | animate(props, speed, easing, callback) | 任意 CSS 數值屬性動畫,支援相對值 +=100px |
| 佇列控制 | queue() / dequeue() / clearQueue() / delay() | 動畫序列管理,queue(callback) 插入非動畫步驟 |
| 停止/延遲 | stop(clearQueue, jumpToEnd) / delay(ms) | 中止動畫、清空佇列、跳至終態、延遲觸發 |
1000 (1秒)"slow" (600ms)、"normal" (400ms)、"fast" (200ms)400ms// 方式一:具名函數遞迴
$('#box').show('slow', function showNext() {
$(this).next().show('slow', showNext);
});
// 方式二:arguments.callee 匿名遞迴
$('#box').show('slow', function() {
$(this).next().show('slow', arguments.callee);
});
$('.animate').click(function() {
$('#box').animate({
width: '300px',
fontSize: '50px',
opacity: 0.5
}, 2000, function() {
alert('動畫結束');
});
});
$('#box').animate({
left: '+=100px', // 相對當前位置右移 100px
width: '+=50px'
});
$('#box')
.slideUp('slow')
.slideDown('slow')
.queue(function(next) {
$(this).css('background', 'orange'); // 非動畫操作入佇列
next(); // 必須呼叫 next() 釋放佇列繼續
})
.hide('slow');
// 立即停止當前動畫,清空佇列,不跳至終態
$('#box').stop(true, false);
// 立即停止,清空佇列,跳至終態
$('#box').stop(true, true);
$('#box')
.delay(1000) // 開始前延遲 1 秒
.animate({left: 300}, 1000)
.delay(500) // 中間延遲 0.5 秒
.animate({top: 300}, 1000);
"swing" (預設)、"linear" 等緩動函數transition/animation,效能更佳、不阻塞主執行緒jQuery 動畫 API 設計直觀,show/hide、slide、fade 滿足 80% 場景;animate 處理複雜數值變化;queue/stop/delay 掌控執行流程。理解「佇列機制」與「next() 釋放」是玩轉複雜序列動畫的關鍵。