javascript Chrome 会记住滚动位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16239520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome remembers scroll position
提问by amees_me
I'm running into a problem that's actually a "feature" on Chrome. As most of you might know, Chrome remembers a scroll position that it returns to, whenever you come back to a page. And I kind of have a problem with that.
我遇到了一个实际上是 Chrome 上的“功能”的问题。大多数人可能都知道,Chrome 会在您返回页面时记住它返回的滚动位置。我有点问题。
Is there any way to override this without the user noticing?
有没有办法在用户不注意的情况下覆盖它?
Mees
米斯
Failed try-outs:
失败的尝试:
- ScrollTop on document.ready
- document.ready 上的 ScrollTop
采纳答案by Ikrom
I've checked on chrome, it worked well. Sometimes setTimeout
does trick :)
我检查了 chrome,它运行良好。有时setTimeout
会欺骗:)
<script type="text/javascript">
window.onload=function(){
setTimeout(function(){
scrollTo(0,-1);
},0);
}
</script>
回答by Cory Duncan
In Chrome 46+, the auto scroll behavior can be turned off using history.scrollRestoration:
在 Chrome 46+ 中,可以使用 history.scrollRestoration 关闭自动滚动行为:
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
source: https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration
来源:https: //developers.google.com/web/updates/2015/09/history-api-scroll-restoration
回答by Luke Clifford
x = 0; //horizontal coord
y = document.height; //vertical coord
window.scroll(x,y);
Some Javascript like that may very well be able to be manipulated to stop the auto scrolling.
一些像这样的 Javascript 很可能能够被操纵来停止自动滚动。
It depends though, are you happy for the scroll to be simply set to automatically go to the top, or are you actually looking for the Chrome standard option to take the page to last scroll position, to be turned off completely?
不过,这取决于,您是否愿意将滚动条简单地设置为自动转到顶部,或者您是否真的在寻找 Chrome 标准选项将页面带到最后一个滚动位置,完全关闭?
What are you currently attempting to use for scrollTop()?
您目前尝试将什么用于 scrollTop()?
回答by Mikhail Kornienko
I solved this by attaching to scroll event, and then resetting scroll position the first time a user scrolls. Works for on-spot reloads for me.
我通过附加到滚动事件解决了这个问题,然后在用户第一次滚动时重置滚动位置。适用于我的现场重新加载。
Looks like this:
看起来像这样:
var scrollResetOnce = false;
$(window).on("scroll", function() {
if (scrollResetOnce) return;
scrollResetOnce = true;
scrollTo(0, -1);
});
回答by Walex
Here is a clean way of getting this done.
这是完成此操作的干净方法。
window.addEventListener('unload', function(e){
document.body.style.display = 'none';
});
By simply setting the body display to 'none' you don't have to worry about a flash of the browser scrolling to the top of the page before it is unloaded and the scroll position will automatically be reset to 0.
通过简单地将主体显示设置为“无”,您不必担心浏览器在卸载之前滚动到页面顶部的闪光,并且滚动位置将自动重置为 0。