How to get and set the current web page scroll position?

前端 未结 4 1107
无人共我
无人共我 2020-12-02 06:38

How can I get and set the current web page scroll position?

I have a long form which needs to be refreshed based on user actions/input. When this happens, the page

4条回答
  •  温柔的废话
    2020-12-02 07:03

    There are some inconsistencies in how browsers expose the current window scrolling coordinates. Google Chrome on Mac and iOS seems to always return 0 when using document.documentElement.scrollTop or jQuery's $(window).scrollTop().

    However, it works consistently with:

    // horizontal scrolling amount
    window.pageXOffset
    
    // vertical scrolling amount
    window.pageYOffset
    

提交回复
热议问题