how to remember scroll position of page

前端 未结 7 1788
南笙
南笙 2020-12-15 00:31

I am submitting some data to my database then reloading the same page as the user was just on, I was wondering if there is a way to remember the scroll position the user was

7条回答
  •  感情败类
    2020-12-15 01:27

    (1) Solution 1:

    First, get the scroll position by JavaScript when clicking the submit button.

    Second, include this scroll position value in the data submitted to PHP page.

    Third, PHP code should write back this value into generated HTML as a JS variable:

    
    

    Fourth, use JS to scroll to position specified by the JS variable 'Scroll_Pos'

    (2) Solution 2:

    Save the position in cookie, then use JS to scroll to the saved position when page reloaded.

提交回复
热议问题