scrollto element by id 1

scrollto element by id

// scroll to specified element. accepts params to choose scroll type, position on page etc
document.getElementById("divFirst").scrollIntoView();
// scrolls smoothly and centers div:
document.getElementById("divFirst").scrollIntoView({behavior: "smooth", block: "center"});
// for all param options visit developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

Here is what the above code is Doing:
1. We’re selecting the element with the id of “divFirst”
2. We’re calling the scrollIntoView() method on that element
3. We’re passing in an object with two properties:
a. behavior: “smooth”
b. block: “center”

The behavior property tells the browser to scroll smoothly to the element.
The block property tells the browser to center the element on the page.

Similar Posts