2014-09-22 56 views
1

我所知道的最跨瀏覽器的方式來獲取或設置滾動偏移量爲:$(window).scrollTop()作爲一個getter,$('html,body').scrollTop(offset)作爲一個二傳手。

但我很困惑什麼是處理滾動事件的最佳做法?

$(window).scroll(fn) or $("html,body").scroll(fn)

+1

在jQuery本身使用'$(窗口)的文檔中.scroll'如實施例('滾動事件被髮送到一個元素[...]。它適用於窗口對象,也適用於可滾動的框架和元素''),我認爲它是jQuery的_correct_方法。但是如果你改變'html'或'body'的'overflow',你可能會使用別的東西。 – 2014-09-22 08:25:00

回答

-1

我寧願使用.bind

$(window).bind('mousewheel', function(e){ }); 
+4

您應該解釋爲什麼要使用'.bind',以及爲什麼'mousewheel'而不是'scroll'。 – 2014-09-22 08:16:50

+1

[** MDN mousewheel **](https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel):'注意:Gecko沒有計劃實施這個事件由於遺產事件類型和非標準。「 – 2014-09-22 09:06:06