-1

can someone help me how can I make with pure javascript make a scroll go down to a particular div on initial state

lilo
  • 185
  • 1
  • 4
  • 17
  • Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please [**search thoroughly**](/search?q=%5Bjs%5D+scroll+to+element) before posting. More about searching [here](/help/searching). – T.J. Crowder Jan 27 '22 at 10:02

1 Answers1

0

You might use element.scrollIntoView method:

const mydiv = document.getElementById("mydiv");
mydiv.scrollIntoView({
  behavior: "smooth",
  block: "start",
  inline: "nearest"
});
<div id="mydiv">
  My div
</div>
stacj
  • 1,103
  • 1
  • 7
  • 20
  • This is an example of a question that's clearly been asked and answered before. Here on Stack Overflow, instead of posting answers on duplicated questions, leading to dozens of search results with slightly-different answers and answers that are difficult to curate over time as things change, we close questions as duplicates, pointing to a single (ideally) set of useful answers. Please take the [tour] and read through the [help] to understand what makes Stack Overflow's model different from, and more useful than, basic Q&A forums. – T.J. Crowder Jan 27 '22 at 10:05