這個效果常用於側邊選單或導覽列:滑鼠移入時,項目高度增加;移出後恢復原狀。使用 jQuery 時,只要在 animate() 前加上 stop(),就能避免連續滑動造成動畫堆疊。
$('.all ul li').hover(function () { $(this).stop().animate({ height: '70px', lineHeight: '70px' }, 100); }, function () { $(this).stop().animate({ height: '40px', lineHeight: '40px' }, 100); });
若要相容舊版瀏覽器,可改用 mouseover() 與 mouseleave(),但核心概念相同:先停止目前動畫,再啟動新的高度變化。