-4

I want to change URL of the site when i scrolling the page. When I scrolling the page,URL of the page will automatically change after each content.

1 Answers1

0

There are two parts to this:

  1. Detecting scrolling and entering sections

    You do this by using the scroll event on window, and then comparing the scrollTop to the position of the section(s) you want to compare to.

  2. Changing the URL

    You have a few choices here:

    1. You can set the hash fragment (the #xyz part of the URL) by assigning to location.hash. Doing that will create a history entry, though, so I don't recommend it. (If just scrolling a page added a bunch of entries to my browsing history, I wouldn't be a happy user.)

    2. You can use location.replace(newUrl) using the current URL updated with a new hash, which won't create a history entry.

    3. You can use history.pushState with any (reasonable) URL you want in modern browsers. Again that will create a history entry.

    4. You can use history.replaceState with any (reasonable) URL you want in modern browsers, which won't create a history entry.

    More about the history API on MDN.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875