刷新页面并保持滚动位置

IT技术 javascript html
2021-02-08 02:39:32

有人可以告诉我我做错了什么吗?我需要我的页面在一段时间后刷新,但它刷新到页面顶部,我需要它不更改页面位置!所以这就是我现在无法使用的元标记?这里是我有没有还是没有刷新一定是做错了什么?

这是我最初拥有的...

<!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;
}
window.onload = function () {
    setTimeout(refreshPage, 35000);
    if ( window.location.href.indexOf('page_y') != -1 ) {
        var match = window.location.href.split('?')[1].split("&")[0].split("=");
        $('html, body').scrollTop( match[1] );
    }
}
</script>

<STYLE type="text/css">
#Nav a{ position:relative; display:block; text-decoration: none; color:black; }
Body td{font-Family: Arial; font-size: 12px; }
</style>
6个回答

document.location.reload()存储位置,参见文档

添加附加true参数以强制重新加载,但不恢复位置。

document.location.reload(true)

MDN 文档:

forceReload 标志改变了一些浏览器处理用户滚动位置的方式。通常reload()之后恢复滚动位置,但是强制模式可以滚动回到页面顶部,就好像window.scrollY === 0。

Internet Explorer 不保存位置。我使用了@zingle-dingle 的回答
2021-03-13 02:39:32
它位于您的答案中链接到的页面上。他们称之为forcedReload
2021-03-27 02:39:32
现在在 MDN 上提到了这一点。
2021-04-01 02:39:32
仅适用于我的 Chrome,最新的 FF 或 Edge 都不会保持滚动位置,而是滚动到顶部
2021-04-10 02:39:32
文档说“在强制模式下(当参数设置为 true)时,新的 DOM 会以 scrollTop == 0 加载。” - 这听起来不像它保持滚动位置。我没有时间测试它,所以我可能会遗漏一些东西......
2021-04-11 02:39:32

这会变魔术

    <script>
        document.addEventListener("DOMContentLoaded", function(event) { 
            var scrollpos = localStorage.getItem('scrollpos');
            if (scrollpos) window.scrollTo(0, scrollpos);
        });

        window.onbeforeunload = function(e) {
            localStorage.setItem('scrollpos', window.scrollY);
        };
    </script>
非常好的和简单的解决方案。我做了一些改变。即使在浏览器关闭后,浏览器仍会存储这些数据。我用 sessionStorage 替换 localStorage 并在设置滚动位置后使用 sessionStorage.removeItem('scrollpos') 清除它。这确保一旦浏览器关闭,滚动位置就会被清除。我发布了修改后的代码作为单独的答案。
2021-03-16 02:39:32
杰出的!我也为我的案例做了一些改变,但它对我来说非常有效!!谢谢!!!
2021-04-07 02:39:32

如果您不想使用本地存储,那么您可以将页面的 y 位置附加到 url 并在加载时使用 js 抓取它并将页面偏移量设置为您传入的 get 参数,即:

//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 );
    }
});
所以保留元标记并替换功能在scrupt标记之间保存滚动位置?
2021-03-19 02:39:32
好添加://code to refresh the page var page_y = $( document ).scrollTop(); window.location.href = window.location.href + '?page_y=' + page_y;用于保存功能。
2021-03-19 02:39:32
现在我编辑它看起来像我认为你在说什么吗?
2021-03-19 02:39:32
我编辑了代码,以便它可以实际工作。JavaScript 是面向对象的。@kkemple 使用了一个不存在的 indexOf 函数。match变量中也有错误编辑只需要被接受。
2021-04-09 02:39:32
@kkemple 但它也匹配: ?sadjsaid332 或 ?2332 ... 另外,我不是指 RegExp ..我在谈论.split['?']<<那是不正确的。
2021-04-12 02:39:32

更新

您可以使用document.location.reload(true)下面提到的代替下面的强制技巧。

用这个替换你的 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>
我无法在 IE 11 中使用它。我在正文中添加了一堆这些: <a href="#">sadsad</a><br>但是当我单击链接时,我到达了页面的顶部。
2021-03-14 02:39:32
没有在 Firefox 上工作(scrollTop 总是零)所以我用了这个
2021-03-31 02:39:32

我修改了 Sanoj Dushmantha 的答案以使用 sessionStorage 而不是 localStorage。然而,尽管有文档,即使浏览器关闭,浏览器仍会存储这些数据。为了解决这个问题,我在重置后删除了滚动位置。

<script>
    document.addEventListener("DOMContentLoaded", function (event) {
        var scrollpos = sessionStorage.getItem('scrollpos');
        if (scrollpos) {
            window.scrollTo(0, scrollpos);
            sessionStorage.removeItem('scrollpos');
        }
    });

    window.addEventListener("beforeunload", function (e) {
        sessionStorage.setItem('scrollpos', window.scrollY);
    });
</script>
很有用,谢谢!我有两个问题,在尝试维护 scrollPosition 时,是否可以避免在每次加载/刷新时看到很少的过渡动画?另外,它会在标签之间共享吗?如何处理多个选项卡中的会话?为每个页面创建唯一的键名是否会对 Web 性能产生重大影响?
2021-03-22 02:39:32