Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore scroll position on infinite scroll page

I have a website with an infinite scroll page, listing the newest ads. When user click on a preview of the ads it will redirect him to the product page.

But when user click on back button, it can not restore scroll position.

Note 1 : i will load 10 ads when scroll reach the end. So let say we have loaded about 50 ads and user change the page. When click on back button, it will reload the main page which is empty, then it just load the first 10 ads.

Note 2 : Im using react js and react-router and have tried to find a solution in the community already. As recently chrome support scroll restoration they didn't do much for that. But chrome doesn't support infinite scroll.

like image 378
Fakhruddin Abdi Avatar asked Jan 12 '18 14:01

Fakhruddin Abdi


People also ask

How can I retain the scroll position of a scrollable area when pressing back button?

You can try $("div#element"). load(function() {}) and place the element outside the document ready handler. Also, if a user scroll down the page, then leave the page, and go back to the page again, it should be a new start for him so it should start at the top. But now it also remembers the previous position.

Why you shouldn't use infinite scroll?

One of the most often cons of infinite scroll is that it's impossible to bookmark items from the list of items. A simple bookmark results action (“save for later” of “add to favorite”) can be a very powerful tool for your users. Pinterest, for instance, uses a bookmarking tool that helps users save creative ideas.

How do I restore scroll position in react?

If the app is running in a normal environment, the scroll position is restored when navigating between pages using the browser's back and forward buttons. The scroll position of a page should be restored (kept) when refreshing the page.


2 Answers

I generally consider infinite scroll to be an antipattern. If you use pagination instead, the browser's behavior is much more predictable.

But if you want infinite scroll w/ back button support, you have only got a few choices:

  • Change the URL as the user scrolls such that the number of records is stored in the URL
  • Store the number of records somewhere else (local storage / cookie)

When the user visits the infinite scroll page, restore the state based on the state you stored in the URL / local storage / wherever.

If I had to do this, I'd try to keep the data in the URL. But if done naively, there are serious flaws with these approaches. Someone malicious could make that number very large, in which case the browser(s) would download way too many records at once.

So, what I'd probably do is only fetch the window of records that matches what they would have seen at their previous scroll position. You can infinitely scroll below it and you can add a "load previous records" or something above it. That would prevent a back-navigation from fetching too many records at once.

Honestly, I think pagination is the answer.

Note, as commented below:

If you aren't concerned about keeping scroll position if the user leaves the site and navigates back, then you can keep the original page in memory (e.g. just hide it and then re-show it when the user navigates back).

like image 126
Christopher Davies Avatar answered Oct 09 '22 23:10

Christopher Davies


You can store your number with history.replaceState when it changes, and load it with history.state when your page loads.

Example with hooks:

const PAGING = 10
// initialize with memorized paging or default if none:
const [numAds, setNum] = useState((history.state && history.state.numAds) || PAGING)
// when scrolling down:
setNum(numAds + PAGING)
history.replaceState({numAds: numAds + PAGING}, '')
like image 43
antoine129 Avatar answered Oct 09 '22 22:10

antoine129