简体   繁体   English

HTML锚点-位置错误

[英]HTML Anchor Points - Wrong Position

This problem has been driving me insane for the last couple of hours. 在过去的几个小时中,这个问题使我发疯。 I have a one-page website design. 我有一页的网站设计。 The anchor links work perfectly fine on the page itself. 锚链接在页面本身上可以正常工作。

But I have a second page that will act as the Blog section. 但是我还有第二页将充当“博客”部分。

When I try to use the anchors from here to link back to the sections on the index page, they do not position correctly. 当我尝试从此处使用锚点链接回索引页面上的部分时,它们的位置不正确。

Please see main page: www.redcedarstudios.ca/themes/Haze/index.html 请参阅主页: www.redcedarstudios.ca/themes/Haze/index.html

and then try to click the nav links back from the blog page: http://www.redcedarstudios.ca/themes/Haze/post.html 然后尝试单击从博客页面返回的导航链接: http : //www.redcedarstudios.ca/themes/Haze/post.html

The positioning is completely out of whack. 定位完全不合时宜。

Any help or ideas is greatly appreciated. 任何帮助或想法,我们将不胜感激。

Thanks John 谢谢约翰

So I found a fix. 所以我找到了解决方法。 I added a $(window).load function that will read the hash tag from the url and scroll to onloading: 我添加了一个$(window).load函数,该函数将从url读取hash标签并滚动到onloading:

$(window).load(function() {
var hash = window.location.hash;
$(document).scrollTop( $(hash).offset().top ); 
});

If you can't get the above to work try this: 如果您无法使以上方法起作用,请尝试以下操作:

<script>
    $(window).load(function() {
        var hash = window.location.hash;
        console.log(hash);
        $('html, body').animate({
        scrollTop: $(hash).offset().top
    }, 2000);
        console.log("page loaded");
    });
</script>

It will actually animate to the correct div if it didn't land on it how it should because of assets that needed to load. 如果由于需要加载的资产而没有按其应有的样式,它将实际上为正确的div设置动画。 If you want to remove the animation, I couldn't get the above code to work by itself, but I used my example here combined with the above example: 如果要删除动画,则无法单独使用上面的代码,但是在这里我将示例与上面的示例结合使用:

<script>
        $(window).load(function() {
            var hash = window.location.hash;
            console.log(hash);

            $(document).scrollTop( $(hash).offset().top );

            $('html, body').animate({
            scrollTop: $(hash).offset().top
        }, 2000);
            console.log("page loaded");
        });
</script>

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

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