2015-11-25 19 views
1

不工作我有這個滾動列表scrollTo在Safari

<ol> 
    <li>...</li> 
    ... 
</ol> 

DEMO

現在,我可以使用

document.querySelector('ol').scrollTo(100); 

編程滾動但是,這並不在Safari工作。雖然這似乎是微不足道的,我找不到替代品(不使用jQuery)

如何使列表在Safari中可滾動?

+0

您是否嘗試過其他瀏覽器,例如Google Chrome或Firefox?它在那裏工作嗎? – krishnaxv

+0

在谷歌瀏覽器上,它會拋出錯誤:Uncaught TypeError:document.querySelector(...)。scrollTo不是函數。你可以參考下面的答案。謝謝! – krishnaxv

回答

2

scrollTowindow對象的屬性。而你正試圖將其應用於element

Use element.scrollTop

代碼段

document.querySelector('ol').scrollTop = 100; 

它會做的伎倆!

分別指 Mozilla/Window/scrollTo & Mozilla/Element/scrollTop

有關scrollTo & scrollTop的更多信息,。

NOTE

document.querySelector(selectors) returns the first element within the document . If your document contains multiple <ol> elements, it will always return the first element.

To select specific element, you can assign an ID & refer the element by document.querySelector('#ID') .

希望它有幫助!