admin管理员组文章数量:1401764
I have one container div what overflow-y:scroll.
.contents_container {width: 100%; height: 100%; overflow-y: scroll; position: relative; float:left;}
I want to remember the container's scroll position when clicked back button or refresh.
So I got helped from stackoverflow, But I doesn't works.
Here is my script and could you fix the problem?
window.addEventListener('popstate', onLocationChange);
function onLocationChange(e){
//if(document.location.test() or startsWith() ....
// to fire only on desired pages
const container document.querySelector('.contents_containe');
const scrollTop = container.scrollTop;
localStorage.setItem("container-scrollTop", scrollTop)
}
window.addEventListner("load", onPageLoad);
function onPageLoad(){
const scrollTop = parseInt(localStorage.getItem("container-scrollTop"));
if(scrollTop && !isNaN(scrollTop){
const container document.querySelector('.contents_containe');
container.scrollTop = scrollTop;
}
}
I have one container div what overflow-y:scroll.
.contents_container {width: 100%; height: 100%; overflow-y: scroll; position: relative; float:left;}
I want to remember the container's scroll position when clicked back button or refresh.
So I got helped from stackoverflow, But I doesn't works.
Here is my script and could you fix the problem?
window.addEventListener('popstate', onLocationChange);
function onLocationChange(e){
//if(document.location.test() or startsWith() ....
// to fire only on desired pages
const container document.querySelector('.contents_containe');
const scrollTop = container.scrollTop;
localStorage.setItem("container-scrollTop", scrollTop)
}
window.addEventListner("load", onPageLoad);
function onPageLoad(){
const scrollTop = parseInt(localStorage.getItem("container-scrollTop"));
if(scrollTop && !isNaN(scrollTop){
const container document.querySelector('.contents_containe');
container.scrollTop = scrollTop;
}
}
Share
Improve this question
edited Jan 3, 2021 at 10:26
yjdev
asked Jan 3, 2021 at 10:20
yjdevyjdev
872 silver badges8 bronze badges
2
-
maybe try utilizing
window.sessionStorage
? And thenwindow. scrollTo
if the value is defined? – zergski Commented Jan 3, 2021 at 11:12 - Plus you'll need to update the value on scroll – wawa Commented Jan 3, 2021 at 11:46
1 Answer
Reset to default 7There are 2 things you want to do.
- Save current scroll position when you leave the page
- Jump to that position on page load
Saving current position:
window.addEventListener("beforeunload", () => {
localStorage.setItem("scrollPositon", document.querySelector(".contents_container").scrollTop);
});
Retriving position:
window.addEventListner("load", () => {
document.querySelector(".contents_container").scrollTop = localStorage.getItem("scrollPositon") || 0;
});
You might want to read up on the localStorage API
Let's take a closer look at the saving part: beforeunload
// event fires, if the user leaves the page
window.addEventListener("beforeunload", () => {
// save an item to the users local storage
localStorage.setItem(
"scrollPositon", // item name/key
document.querySelector(".contents_container").scrollTop // item value (scroll top position in this case)
);
});
We then basically do the reverse when the page loads:
// event fires when the page loads
window.addEventListner("load", () => {
// setting scrollTop of container to:
document.querySelector(".contents_container").scrollTop =
// whatever value the item with name/key scrollPosition in the local storage holds
localStorage.getItem("scrollPositon") || 0; // or if it's not set, simply use 0 as fallback
});
Edit: I haven't run your code, but I noticed an error. You're using document.querySelector('.contents_containe')
in your JS but .contents_container { ... }
in the CSS. The JS version lacks the r
at the end. Always make sure the selectors are the same across HTML/CSS/JS!
本文标签: javascriptHow to remember scroll position of overflowscroll divStack Overflow
版权声明:本文标题:javascript - How to remember scroll position of overflow:scroll div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744762554a2623838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论