简体   繁体   English

如何在url路径中指定垂直偏移量

[英]How to specify vertical offset in the url path

Can I specify an y offset in the url path when I use bookmarks ? 我使用书签时可以在网址路径中指定y偏移量吗?

Let's say I have this path: mywebsite.com/mypage#bookmark 假设我有这条路: mywebsite.com/mypage#bookmark

I would like to center the bookmark section in the middle of the page and not in the top. 我想将书签部分置于页面中间而不是顶部。

I was wondering if I can specify something like ..mypage#bookmark&y-offset=100 我想知道我是否可以指定像..mypage#bookmark&y-offset=100

maybe i can use javascript to get that value.. thanks 也许我可以用javascript来获得这个价值..谢谢

There is no native way to do this, but what you can do is define a separator between the ID and the offset, eg / , then parse it out. 没有本机方法可以做到这一点,但你可以做的是在ID和偏移量之间定义一个分隔符,例如/ ,然后解析出来。

For example: 例如:

/path/to/page#id/200 /路径/到/页#ID / 200

Then you can parse the value out and manually move it forward. 然后,您可以解析值并手动向前移动它。 Note that I don't know how to trigger this as an event if you're already on the page and moving to an anchor on the page (please fill me in anyone?) 请注意,如果您已经在页面上并移动到页面上的锚点,我不知道如何将其作为事件触发(请填写我的任何人?)

function moveToHash() {
    function offset(node) {
        var x = 0, y = 0; do {
            x += node.offsetLeft;
            y += node.offsetTop;
        } while (node = node.offsetParent);
        return {x: x, y: y};
    }
    var id = location.search.match(/([^\/]+)/)[1];
    var offset = location.search.match(/\/(.+)/)[1] * 1;
    var nodeOffset = offset(document.getElementById(id));
    window.scrollTo(nodeOffset.x, nodeOffset.y);
    window.scrollBy(0, offset);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM