sessionStorage.setItem();
sessionStorage.getItem()
获取滚动条当前的位置
function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
在即将离开当前页面(刷新或关闭)时执行,保存滚动条位置
window.onbeforeunload = function(){
var offsetTop = getScrollTop();
if (offsetTop != 0){
sessionStorage.setItem("offsetTop", offsetTop); //保存滚动位置
}
}
再次进到页面中, onload时 sessionStorage.getItem(key) 取出并滚动到上次保存位置
window.onload = function()
{
var offsetTop = sessionStorage.getItem("offsetTop");
if(offsetTop!=null){
document.body.scrollTop = offsetTop;
}
}