Browser back button does not work for Anchor links

后端 未结 4 2497
广开言路
广开言路 2021-02-20 08:36

In the footer of my page there a few links that point to different sections on the same page using anchor tags (# appended to the URL of the page).

This works fine, just

4条回答
  •  死守一世寂寞
    2021-02-20 09:18

    I also faced the same problem see my question anchor links referring to the page sections not working on browser refresh, back and forward

    But I had to do it the way normal links work so what I did was I manually go to that section by getting the element from the hash.

    $(window).on('hashchange', function () 
    {
        var top = $(window.location.hash).offset().top;
        $(window).scrollTop(top);
    });
    

    This works for forward and back buttons. And for refresh also you need to do the same thing. Get the element from the hash and scroll to that element manually.

提交回复
热议问题