3 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
更新
您可以使用document.location.reload(true)下面提到的方法代替下面的強(qiáng)制技巧。
將HTML替換為:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
background-image: url('../Images/Black-BackGround.gif');
background-repeat: repeat;
}
body td {
font-Family: Arial;
font-size: 12px;
}
#Nav a {
position:relative;
display:block;
text-decoration: none;
color:black;
}
</style>
<script type="text/javascript">
function refreshPage () {
var page_y = document.getElementsByTagName("body")[0].scrollTop;
window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
}
window.onload = function () {
setTimeout(refreshPage, 35000);
if ( window.location.href.indexOf('page_y') != -1 ) {
var match = window.location.href.split('?')[1].split("&")[0].split("=");
document.getElementsByTagName("body")[0].scrollTop = match[1];
}
}
</script>
</head>
<body><!-- BODY CONTENT HERE --></body>
</html>

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
document.location.reload()存儲(chǔ)位置,請(qǐng)參閱docs。
添加其他true參數(shù)以強(qiáng)制重新加載,但不恢復(fù)位置。
document.location.reload(true)
MDN文檔:
forceReload標(biāo)志更改了某些瀏覽器處理用戶滾動(dòng)位置的方式。通常reload()之后會(huì)恢復(fù)滾動(dòng)位置,但是強(qiáng)制模式可以滾動(dòng)回到頁(yè)面頂部,就像window.scrollY === 0一樣。

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
如果您不想使用本地存儲(chǔ),則可以將頁(yè)面的y位置附加到url上,并在加載時(shí)使用js進(jìn)行抓取,并將頁(yè)面偏移量設(shè)置為傳入的get參數(shù),即:
//code to refresh the page
var page_y = $( document ).scrollTop();
window.location.href = window.location.href + '?page_y=' + page_y;
//code to handle setting page offset on load
$(function() {
if ( window.location.href.indexOf( 'page_y' ) != -1 ) {
//gets the number from end of url
var match = window.location.href.split('?')[1].match( /\d+$/ );
var page_y = match[0];
//sets the page offset
$( 'html, body' ).scrollTop( page_y );
}
});
添加回答
舉報(bào)