简体   繁体   English

在无限滚动页面上恢复滚动位置

[英]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.注意 1:当滚动到最后时我将加载 10 个广告。 So let say we have loaded about 50 ads and user change the page.假设我们已经加载了大约 50 个广告并且用户更改了页面。 When click on back button, it will reload the main page which is empty, then it just load the first 10 ads.当点击后退按钮时,它会重新加载空白的主页,然后它只加载前 10 个广告。

Note 2 : Im using react js and react-router and have tried to find a solution in the community already.注 2:我使用的是 react js 和 react-router,并且已经尝试在社区中找到解决方案。 As recently chrome support scroll restoration they didn't do much for that.由于最近 chrome 支持滚动恢复,他们并没有为此做太多。 But chrome doesn't support infinite scroll.但是 chrome 不支持无限滚动。

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在用户滚动时更改 URL,以便将记录数存储在 URL 中
  • Store the number of records somewhere else (local storage / cookie)将记录数存储在其他地方(本地存储/cookie)

When the user visits the infinite scroll page, restore the state based on the state you stored in the URL / local storage / wherever.当用户访问无限滚动页面时,根据你存储在 URL / 本地存储 / where 中的状态恢复状态。

If I had to do this, I'd try to keep the data in the URL.如果我必须这样做,我会尝试将数据保留在 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.有人恶意能有这样的数量非常大,在这种情况下,浏览器(S)将同时下载太多的记录。

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 (eg just hide it and then re-show it when the user navigates back).如果您不关心在用户离开站点并返回时保持滚动位置,那么您可以将原始页面保留在内存中(例如,只需隐藏它,然后在用户返回时重新显示它)。

You can store your number with history.replaceState when it changes, and load it with history.state when your page loads.您可以在更改时使用history.replaceState存储您的号码,并在您的页面加载时使用history.state加载它。

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}, '')

我认为最简单的方法是您将一些变量设置为默认值为 true,如果此变量为 true,则仅进行刷新并获取更多项目,当您转到其他页面设置为 false 时,当您返回时,项目将处于状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM