實作首屏與第二屏之間滾動滑鼠滾輪整頁切換效果。Chrome 下 onmousewheel 事件會在一次滾動操作中連續觸發多次,導致動畫佇列堆疊、頁面滾動失控。
wheel 事件(高精度滑鼠/觸控板)animate() 未完成)新事件持續觸發$(document).ready(function(){
var height = $(window).height();
var scrollFunc = function(e){
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
e = e || window.event;
// 向下滾動:第 1 屏 → 第 2 屏
if((e.wheelDelta < 0 || e.detail > 0) && scrollTop >= 0 && scrollTop < height){
$(document.body).animate({scrollTop: height}, "fast");
$(document.documentElement).animate({scrollTop: height}, "fast");
}
// 向上滾動:第 2 屏 → 第 1 屏
else if((e.wheelDelta > 0 || e.detail < 0) && scrollTop >= height && scrollTop <= height + 20){
$(document.body).animate({scrollTop: 0}, "fast");
$(document.documentElement).animate({scrollTop: 0}, "fast");
}
};
// 事件註冊
if(document.addEventListener){
document.addEventListener('DOMMouseScroll', scrollFunc, false); // Firefox
}
window.onmousewheel = document.onmousewheel = scrollFunc; // IE/Chrome/Safari
});
var scrollFunc = function(e){
// 關鍵:動畫開始前移除事件監聽
document.onmousewheel = undefined;
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
e = e || window.event;
if((e.wheelDelta < 0 || e.detail > 0) && scrollTop >= 0 && scrollTop < height){
$(document.body).animate({scrollTop: height}, "fast", "linear", function(){
// 動畫完成後恢復監聽
document.onmousewheel = scrollFunc;
});
$(document.documentElement).animate({scrollTop: height}, "fast", "linear", function(){
document.onmousewheel = scrollFunc;
});
}
// ... 向上滾動邏輯相同
};
缺點:document.body 與 document.documentElement 兩個動畫各自恢復監聽,可能重複綁定。
$(document).ready(function(){
var height = $(window).height();
var width = $(window).width();
// 判斷捲軸容器:Firefox/IE 用 documentElement,其他用 body
var body;
if(navigator.userAgent.indexOf("Firefox") > 0 || navigator.userAgent.indexOf("MSIE") > 0){
body = document.documentElement;
} else {
body = document.body;
}
var isFinish = true; // 動畫完成旗標
var scrollFunc = function(e){
// 關鍵:動畫未完成時直接忽略事件
if(!isFinish) return;
var scrollTop = body.scrollTop;
e = e || window.event;
// 向下滾動(預留 20px 緩衝)
if((e.wheelDelta < 0 || e.detail > 0) && scrollTop >= 0 && scrollTop < height - 20){
scroll(height);
}
// 向上滾動
else if((e.wheelDelta > 0 || e.detail < 0) && scrollTop >= height && scrollTop <= height + 20){
scroll(0);
}
};
// 統一滾動動畫函式
var scroll = function(targetTop){
isFinish = false; // 鎖定
$(body).animate({
scrollTop: targetTop
}, "fast", "linear", function(){
isFinish = true; // 解鎖
});
};
// 事件註冊
if(document.addEventListener){
document.addEventListener('DOMMouseScroll', scrollFunc, false);
}
document.onmousewheel = scrollFunc;
});
const scrollController = (() => {
const height = window.innerHeight;
const body = document.scrollingElement; // 統一取得捲軸容器(現代瀏覽器)
let isAnimating = false;
function scrollTo(target) {
if (isAnimating) return;
isAnimating = true;
// 使用 Web Animations API 或 CSS scroll-behavior
body.animate({ scrollTop: target }, {
duration: 400,
easing: 'ease-out'
}).onfinish = () => { isAnimating = false; };
}
function onWheel(e) {
if (isAnimating) return;
const scrollTop = body.scrollTop;
const delta = e.deltaY || -e.wheelDelta || e.detail * 40; // 統一 delta
if (delta > 0 && scrollTop < height - 20) {
scrollTo(height); // 向下
} else if (delta < 0 && scrollTop >= height) {
scrollTo(0); // 向上
}
}
// passive: true 提升滾動效能,但無法 preventDefault
// 此處不需 preventDefault,可加入 passive
window.addEventListener('wheel', onWheel, { passive: true });
return { destroy: () => window.removeEventListener('wheel', onWheel) };
})();
| 技術點 | 舊版寫法 | 現代寫法 |
|---|---|---|
| 事件類型 | onmousewheel / DOMMouseScroll | wheel (標準) |
| Delta 正規化 | 手動判斷 wheelDelta/detail | e.deltaY (標準) |
| 捲軸容器 | UA 判斷 body/documentElement | document.scrollingElement |
| 防抖/節流 | 旗標鎖定 isFinish | 旗標 / requestAnimationFrame / 防抖函式 |
| 動畫 | jQuery animate() | Web Animations API / CSS scroll-behavior |
核心解法是動畫期間忽略後續輪播事件(旗標鎖定或移除監聽器)。現代專案建議使用標準 wheel 事件、document.scrollingElement、Web Animations API,並善用 passive: true 提升滾動效能。