滚动到锚点

发布于 2024-08-29 00:01:42 字数 1335 浏览 3 评论 0原文

我有以下 userjs,旨在删除 URL 的锚点部分,但仍然跳转到它:

// ==UserScript==
// @name PurgeAnchor
// @include *
// ==/UserScript==
(function() {
 var reg=/^(.*)\#(.*)$/;
 var match=reg.exec(location);
 function ObjectPosition(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
 }
 if(match) {
    document.location.replace(match[1]);
    sessionStorage.setItem("anchor", match[2]);
 }
 window.addEventListener("load", (function() {
         var anchor=sessionStorage.getItem("anchor");
         if(anchor!==null) {
             var obj=document.anchors.item(anchor);
             // var obj=document.getElementById(anchor);
             // if(obj===null) {
                 // obj=document.getElementsByName(anchor)[0];
             // }
             var pos=0;
             if(obj!==null) {
                 pos=ObjectPosition(obj);
                 window.scrollTo(0, pos);
             }
             sessionStorage.removeItem("anchor");
         }
     }), false);
 })()

问题是,如果我有一个带有名称集的空 标记,它无法跳。 obj.scrollIntoView() 也失败。 Opera-10.52_pre6306,Gentoo。

I have the following userjs which is intended to remove anchor part of the URL but still jump to it:

// ==UserScript==
// @name PurgeAnchor
// @include *
// ==/UserScript==
(function() {
 var reg=/^(.*)\#(.*)$/;
 var match=reg.exec(location);
 function ObjectPosition(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
 }
 if(match) {
    document.location.replace(match[1]);
    sessionStorage.setItem("anchor", match[2]);
 }
 window.addEventListener("load", (function() {
         var anchor=sessionStorage.getItem("anchor");
         if(anchor!==null) {
             var obj=document.anchors.item(anchor);
             // var obj=document.getElementById(anchor);
             // if(obj===null) {
                 // obj=document.getElementsByName(anchor)[0];
             // }
             var pos=0;
             if(obj!==null) {
                 pos=ObjectPosition(obj);
                 window.scrollTo(0, pos);
             }
             sessionStorage.removeItem("anchor");
         }
     }), false);
 })()

The problem is that if I have an empty <a> tag with the name set, it fails to jump. obj.scrollIntoView() also fails.
Opera-10.52_pre6306, Gentoo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2024-09-05 00:01:42

好吧,花了一段时间,但我想我终于明白了:

(function detectSaveAndRedirect() {
            window.addEventListener('load', function() {
                // If the URL already has a hash, remove it.
                if (location.hash.length > 0) {
                    sessionStorage.setItem('anchor',location.hash.substr(1))
                    location.hash = ''
                }
                window.addEventListener('hashchange', function(event) {
                    if (sessionStorage.getItem('anchor')) {
                        event.preventDefault();
                        document.getElementById(sessionStorage.getItem('anchor')).scrollIntoView()
                        sessionStorage.removeItem('anchor')
                    }
                });
                // If links have hashes, remove them.
                document.getElementsByTagName('a').forEach(function(a) {
                    if (
                        (new URL(a.href).host == location.host) &&
                        (a.hash.length > 0)
                    ) {
                        a.addEventListener('click',function(event) {
                            a.removeAttribute('href')
                            a.scrollIntoView();
                        });
                    }
                });
            })
        })()

Ok, it took a while, but I think I finally got it:

(function detectSaveAndRedirect() {
            window.addEventListener('load', function() {
                // If the URL already has a hash, remove it.
                if (location.hash.length > 0) {
                    sessionStorage.setItem('anchor',location.hash.substr(1))
                    location.hash = ''
                }
                window.addEventListener('hashchange', function(event) {
                    if (sessionStorage.getItem('anchor')) {
                        event.preventDefault();
                        document.getElementById(sessionStorage.getItem('anchor')).scrollIntoView()
                        sessionStorage.removeItem('anchor')
                    }
                });
                // If links have hashes, remove them.
                document.getElementsByTagName('a').forEach(function(a) {
                    if (
                        (new URL(a.href).host == location.host) &&
                        (a.hash.length > 0)
                    ) {
                        a.addEventListener('click',function(event) {
                            a.removeAttribute('href')
                            a.scrollIntoView();
                        });
                    }
                });
            })
        })()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文