最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep scroll positions on page by url - Stack Overflow

programmeradmin4浏览0评论

Can you fix the script, which keeps position on difrent pages with no problem, until there you have some pages with redirected to same php file, but different URL (thanks RewriteRule)? Would like to keep position even with atributes in URL. For example - page1.php is same as page1.php&abc=1 but diferent of page2.php. This script takes all these pages as same URL so it keeps same position, even if you did not visited second page yet.

.htaccess

RewriteRule page1\.php detail.php
RewriteRule page2\.php detail.php
RewriteRule page3\.php detail.php

script:

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

    window.onscroll = function (e) {
        localStorage.setItem("scrollpos", window.scrollY);
    };
</script>
发布评论

评论列表(0)

  1. 暂无评论