How to disable anchor jump when loading a page

前端 未结 2 444
执笔经年
执笔经年 2020-12-16 03:49

When I visit my_site.com/page.php#something, the scroll position is the one of the element carrying this particular hashtag rather than the top of the page.

2条回答
  •  囚心锁ツ
    2020-12-16 04:02

    Having this HTML code:

    link
    

    You can avoid scrolling to the div element and instead scrolling to the top of the window by using this code:

    $("a").on("click", function(e) {
        e.preventDefault();
        window.scrollTo(0, 0);
    });
    

    EDIT:

    You can try to add this:

    var firstVisit = true;
    $(window).scroll(function() {
        if (firstVisit) {
            window.scrollTo(0, 0);
            firstVisit = false;
        }
    });
    

提交回复
热议问题