百度地圖 API 的首頁有個我很喜歡的細節:滑鼠移過下方的標籤,上方對應的內容會平順地滑換過去。這效果不難做,關鍵是 animate 加上計時器的搭配。順便把另一個常用效果——點擊錨點平滑捲動——也一起整理在這裡。
思路是:所有內容垂直堆疊在一個容器裡,外層設為 overflow hidden,然後改變內層的 top 值,讓指定區塊滑進視野。
var timer;
$("li").on("mouseover", function () {
// 每次移入先清掉上一個計時器,避免動畫互相打架
clearTimeout(timer);
timer = null;
$(this).addClass("active");
$(this).siblings().removeClass("active");
var $index = $(this).index();
// 延遲 300ms 才啟動動畫
timer = setTimeout(function () {
$(".wrapper").animate({ top: -$index * 300 });
}, 300);
});
這裡的 clearTimeout 是整個效果的靈魂。如果滑鼠快速掃過好幾個標籤,每個都啟動動畫,畫面就會瘋狂抖動。加上延遲與清除,等於做了一個簡易的 debounce——只有停留超過 300 毫秒的標籤才會真正觸發切換。
每個區塊高度固定 300px,所以第 n 個區塊的偏移量就是 -n * 300。
這個效果讓卡片在滑鼠移入時,把正面往左收起、背面展開填滿:
$('.dd').find($(".cc")).hover(
function () {
$(this).find(".c1").stop().animate({ width: 0 }, 100, function () {
$(this).hide().next().show().animate({ width: 300 }, 100);
});
},
function () {
$(this).find(".c2").animate({ width: 0 }, 100, function () {
$(this).hide().prev().show().animate({ width: 300 }, 100);
});
}
);
這裡的 .stop() 很重要:它會中止該元素上仍在進行中的動畫。少了它,快速連續滑入滑出會讓動畫堆疊,卡片最後停在半開半合的怪狀態。這是在任何連續動畫場景中都該養成的習慣。
* { margin: 0; padding: 0; box-sizing: border-box }
ul { list-style: none; border: 1px solid #ccc; border-radius: 5px; width: 900px; margin: 30px auto 10px; }
.clearfix { zoom: 1; }
.clearfix:after { content: "."; display: block; width: 0; height: 0; visibility: hidden; clear: both; }
li { float: left; border-right: 1px solid #ccc; width: 20%; text-align: center; height: 30px; line-height: 30px; cursor: pointer; }
li:last-child { border: none; }
li.active { background-color: #0099cc; color: #fff; }
.container, .wrapper, .con { height: 300px; }
.container { width: 900px; margin: 0 auto; position: relative; overflow: hidden; }
.wrapper { position: relative; top: 0; }
.con { line-height: 300px; text-align: center; border: 1px solid #ccc; }
.dd { margin: 30px auto; width: 1200px; }
.cc { float: left; width: 300px; height: 200px; overflow: hidden; margin: 20px; }
.c1, .c2 { width: 100%; height: 100%; margin: 0 auto; text-align: center; line-height: 200px; cursor: pointer; overflow: hidden; }
.c1 { background-color: #c01110; }
.c2 { background-color: #0099cc; }
.container 的 overflow: hidden 是滑換效果的前提——它負責把視窗外的内容裁掉。
<ul class="clearfix"> <li class="active">選項1</li> <li>選項2</li> <li>選項3</li> <li>選項4</li> <li>選項5</li> </ul> <div class="container"> <div class="wrapper"> <div class="con">111111</div> <div class="con">222222</div> <div class="con">33333</div> <div class="con">44444</div> <div class="con">55555</div> </div> </div> <div class="dd clearfix"> <div class="cc"><div class="c1">111</div><div class="c2">222</div></div> <div class="cc"><div class="c1">111</div><div class="c2">222</div></div> <div class="cc"><div class="c1">111</div><div class="c2">222</div></div> </div>
點擊頁內連結時,瀏覽器預設是瞬間跳過去。想改成捲動動畫,只要攔下預設行為再自己 animate:
1)引入 jQuery
2)給要攔截的 a 標籤加上 class="smooth",href 指向目標元素的 id 或 class
3)掛上事件
$(".smooth").click(function () {
var href = $(this).attr("href");
var pos = $(href).offset().top;
$("html,body").animate({ scrollTop: pos }, 1000);
return false; // 阻止預設的瞬間跳轉
});
選擇器同時寫 html,body 是為了跨瀏覽器相容——不同瀏覽器實際承擔捲動的元素不同。若目標區塊上方有固定高度的 header,記得把 pos 再減掉那個高度,否則標題會被遮住。