刷新页面并保持滚动位置

有人可以告诉我我在做什么错吗?我需要在一段时间后刷新页面,但它会刷新到页面顶部,我需要不更改页面位置!所以这是我现在无法使用的,是meta标签吗?这是我所没有的仍然不刷新必须做错了什么吗?


这是我本来的...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <meta http-equiv="refresh" content="72">

        <meta http-equiv="Pragma" CONTENT="no-cache">

        <meta http-equiv="Expires" CONTENT="-1">


        <style type="text/css">

        body

        { 

            background-image: url('../Images/Black-BackGround.gif');

            background-repeat: repeat;

        }

        </style>

    </head>


    <script type="text/javascript">


    function saveScrollPositions(theForm) {

        if(theForm) {

            var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset

                                                   : document.documentElement.scrollTop;

            var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset

                                                  : document.documentElement.scrollLeft;

            theForm.scrollx.value = scrollx;

            theForm.scrolly.value = scrolly;

        }

    }

    </script>


 <form action="enroll.php" name="enrollment" method="post" onsubmit="return saveScrollPositions (this);">

  <input type="hidden" name="scrollx" id="scrollx" value="0" />

  <input type="hidden" name="scrolly" id="scrolly" value="0" />


  <STYLE type="text/css">

   #Nav a{ position:relative; display:block; text-decoration: none; color:Black; }

   Body td{font-Family: Arial; font-size: 12px; }

  </style>

阅读了一些初始答案后,我将其更改为...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>


<style type="text/css">

body

    background-image: url('../Images/Black-BackGround.gif');

    background-repeat: repeat;

}

</style>

</head>



<script>

function refreshPage () {

    var page_y = $( document ).scrollTop();

    window.location.href = window.location.href + '?page_y=' + page_y;

}



有只小跳蛙
浏览 736回答 3
3回答

慕雪6442864

更新您可以使用document.location.reload(true)下面提到的方法代替下面的强制技巧。将HTML替换为:<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-image: url('../Images/Black-BackGround.gif');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-repeat: repeat;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body td {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-Family: Arial;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 12px;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Nav a {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position:relative;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display:block;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-decoration: none;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color:black;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </style>&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function refreshPage () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var page_y = document.getElementsByTagName("body")[0].scrollTop;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.onload = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(refreshPage, 35000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( window.location.href.indexOf('page_y') != -1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var match = window.location.href.split('?')[1].split("&")[0].split("=");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementsByTagName("body")[0].scrollTop = match[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body><!-- BODY CONTENT HERE --></body></html>

猛跑小猪

document.location.reload()存储位置,请参阅docs。添加其他true参数以强制重新加载,但不恢复位置。document.location.reload(true)MDN文档:forceReload标志更改了某些浏览器处理用户滚动位置的方式。通常reload()之后会恢复滚动位置,但是强制模式可以滚动回到页面顶部,就像window.scrollY === 0一样。

侃侃尔雅

如果您不想使用本地存储,则可以将页面的y位置附加到url上,并在加载时使用js进行抓取,并将页面偏移量设置为传入的get参数,即://code to refresh the pagevar page_y = $( document ).scrollTop();window.location.href = window.location.href + '?page_y=' + page_y;//code to handle setting page offset on load$(function() {&nbsp; &nbsp; if ( window.location.href.indexOf( 'page_y' ) != -1 ) {&nbsp; &nbsp; &nbsp; &nbsp; //gets the number from end of url&nbsp; &nbsp; &nbsp; &nbsp; var match = window.location.href.split('?')[1].match( /\d+$/ );&nbsp; &nbsp; &nbsp; &nbsp; var page_y = match[0];&nbsp; &nbsp; &nbsp; &nbsp; //sets the page offset&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $( 'html, body' ).scrollTop( page_y );&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP