Is it possible to have the url change while you scroll down a single page

前端 未结 7 779
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 14:34

Is it possible to have the url change while you scroll down a single page with ajax? I have a website all on one page and want to have this effect.

example:

7条回答
  •  [愿得一人]
    2020-12-05 15:10

    No. Not sure why you would want to either.

    As you mentioned, the only way would be to add it on to the page's hash, ex.

    http://www.website.com/blog/#entry-name
    

    Then to scroll to that part of the page, something like:

    if (window.location.indexOf("#") > 0) {
        var entry_id = window.location.substring(window.location.indexOf("#"));
        $('html, body').animate({
            scrollTop: $(entry_id).offset().top
        }, 2000);
    }
    

    EDIT: This is long since possible with window.history.pushState.

提交回复
热议问题