Smooth Scrolling with JS
Tags: html, css, javascript, webdev
Published: 2023-01-24 13:49:08

Hello Everyone today I will show how you can create smooth scrolling with Javascript as well as with CSS.

Let's get started...

# What we are going to create 
{% codepen https://codepen.io/shubhamtiwari909/pen/XWBEmyL %}

# With CSS only 

## HTML - 
```html
<header>
  <ul class="nav">
    <li><a class="nav_link" href="#section1">Section 1</a></li>
    <li><a class="nav_link" href="#section2">Section 2</a></li>
    <li><a class="nav_link" href="#section3">Section 3</a></li>
    <li><a class="nav_link" href="#section4">Section 4</a></li>
    <li><a class="nav_link" href="#section5">Section 5</a></li>
  </ul>
</header>

<main>
  <section id="section1">This is Section 1</section>
  <section id="section2">This is Section 2</section>
  <section id="section3">This is Section 3</section>
  <section id="section4">This is Section 4</section>
  <section id="section5">This is Section 5</section>
</main>
```

* I have created a simple Header with Navbar and main with 5 section.
* The href attribute of the anchor tag and id attribute of section tag are same, it means when we click on any anchor tag, it will directly jump to the section it is connected with. But it will jump to that section immediately like it just teleported there.

```css
*{
  margin:0;
  paddin:0;
  box-sizing:border-box;
  scroll-behavior: smooth;
}
ul{
  list-style:none;
}
a {
  text-decoration:none;
}

.nav{
  position:fixed;
  top:0;
  min-width:100%;
  padding:10px;
  display:flex;
  justify-content:center;
  gap:1rem;
  flex-wrap:wrap;
  background-color:rebeccapurple;
}
.nav_link {
  color:white;
}

section{
  height:100vh;
  display:grid;
  place-items:center;
  font-size:2rem;
}
#section1{
  background:crimson;
  color:white;
}
#section2{
  background:violet;
}
#section3{
  background:lime;
}
#section4{
  background:cyan;
}
#section5{
  background:rgb(0,0,0,0.8);
  color:white;
}
```
* I just styled the navbar and sections with some CSS
* At the top, I have used scroll-behavior: smooth, this will make that auto-scrolling to a particular section smooth when we click on any link that scrolls the view to that section.

## Browser Support 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgard231bclkz12ko3ob.png)

# With Javascript
* HTML and CSS part will be the same as above ( Just remove that scroll-behavior:smooth property from CSS code).

## JavaScript
* Older Way 
```js
const navbar = document.querySelector(".nav");

navbar.addEventListener("click", (e) => {
  e.preventDefault();
  const target = e.target;
  if (target.classList.contains("nav_link")) {
    const id = e.target.getAttribute("href");
    //Older browser Support
      const section = 
         document.querySelector(id).getBoundingClientRect();

      window.scrollTo({
      left:section.left + window.pageXOffset,
      top:section.top + window.pageYOffset,
      behavior:"smooth"
    })
  }
});
```
* Using event target, checking if the link which is clicked has a class name "nav_link", if it does, then get the href attribute value of that link.
* By passing this href attribute value as id in the querySelector(), we will get the section which is connected to this link ( Example - href="#section1" in anchor tag and id="section1" in section tag), querySelector will recieve the id directly like querySelector("#section1")
* What it is doing, is just calculating the top and left value of the section using getBoundingClientRect() method and bahavior:smooth will make the scrolling smooth. There is better method of doing this so, you can just not go in depth for this one.

* Modern Way
```js
const navbar = document.querySelector(".nav");

navbar.addEventListener("click", (e) => {
  e.preventDefault();
  const target = e.target;
  if (target.classList.contains("nav_link")) {
    const id = e.target.getAttribute("href");
    document.querySelector(id).scrollIntoView({ behavior: "smooth" });
  }
});

```
* We have another method called "scrollIntoView", we just need to apply this method to the section container with bahavior:"smooth" parameter and it will make the scrolling smooth.

## Browser support for this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbqje0crheny6b4k3cpz.png)

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me with some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/css-iswherehas-and-not-2afd

https://dev.to/shubhamtiwari909/theme-changer-with-html-and-css-only-4144

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/going-deep-in-array-sort-js-2n90