chrome 正常,每滑一次滾輪,alert 1,2,3,4,5,6。。。 而Firefox會莫名奇妙執行多次似的,第一次alert會輸出不等值!
無標題文檔
參照這篇文章,關掉Firefox平滑滾動,但是如果滾輪滑動的比較快,還是會出現問題。http://mozilla.com.cn/forum.phpmod=viewthread&tid=35551&highlight=Scroll
回複內容:
chrome 正常,每滑一次滾輪,alert 1,2,3,4,5,6。。。 而Firefox會莫名奇妙執行多次似的,第一次alert會輸出不等值!
無標題文檔
參照這篇文章,關掉Firefox平滑滾動,但是如果滾輪滑動的比較快,還是會出現問題。http://mozilla.com.cn/forum.phpmod=viewthread&tid=35551&highlight=Scroll
throttle
debounce
requestAnimationFrame + customEvent
;(function() { var throttle = function(type, name, obj) { obj = obj || window; var running = false; var func = function() { if (running) { return; } running = true; requestAnimationFrame(function() { obj.dispatchEvent(new CustomEvent(name)); running = false; }); }; obj.addEventListener(type, func); }; /* init - you can init any event */ throttle ("scroll", "optimizedScroll");})();// handle eventwindow.addEventListener("optimizedScroll", function() { console.log("Resource conscious scroll callback!");});
原文