How to specify vertical offset in URL path

Is it possible to specify the offset y in the path of the URL when using bookmarks?

Let's say I have this way: mywebsite.com/mypage#bookmark

I would like to center the bookmark in the middle of the page, and not at the top.

I was wondering if I can specify something like ..mypage#bookmark&y-offset=100

maybe i can use javascript to get this value. thank

+3
source share
1 answer

There is no own way to do this, but what you can do is define a separator between identifier and offset, for example. /then analyze it.

For instance:

/ path / to / page # Identifier / 200

. , , , (, -?)

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);
}
+1

Source: https://habr.com/ru/post/1760252/


All Articles