Home > Software design >  How can I auto scroll when clicking on my nav bar to a specific part of my page?
How can I auto scroll when clicking on my nav bar to a specific part of my page?

Time:12-08

I'd like to know if it's possible to auto-scroll when I'm clicking on my nav bar title right to a specific part of my page? Is it possible without using jQuery?

CodePudding user response:

Very much possible without the use of Javascript or Jquery. I've created a basic version of the behavior you're looking for. This is possible using ids in your HTML. Please see the example.

a {
  font-size: 10rem;
}

html {
  scroll-behavior: smooth;
}
<div class="container">
      <!-- potential navbar component -->
      <a href="#about">About</a><br>
      <img src="https://dummyimage.com/1500/000/fff&text=I'm a dumb image simply for scroll behavior.">
      <h1 id="about">About</h1> <!-- goes to this spot of page because of id -->
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<img src="https://dummyimage.com/1500/000/fff&text=I'm a dumb image simply for scroll behavior.">
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related