DEV Community

Nikhil Chandra Roy
Nikhil Chandra Roy

Posted on

10 2

How to create Javascript Scrollspy (Vanilla Js Tutorial)

Hi guys,
In this tutorial we are going to create javascript scrollspy with vanilla js.

HTML Structure

<menu>
  <ul>
    <li class="active"> <a href="#section1"> Section 1 </a> </li>
    <li> <a href="#section2"> Section 2 </a> </li>
    <li> <a href="#section3"> Section 3 </a> </li>
    <li> <a href="#section4"> Section 4 </a> </li>
    <li> <a href="#section5"> Section 5 </a> </li>

    </ul>

  </menu>

<main>
  <section id="section1"> 
  <h1> Section 1 </h1>
    <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>

  </section>

  <section id="section2"> 
  <h1> Section 2 </h1>
    <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>

  </section>

  <section id="section3"> 
  <h1> Section 3 </h1>
    <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>

  </section>

  <section id="section4"> 
  <h1> Section 4 </h1>
    <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>

  </section>

  <section id="section5"> 
  <h1> Section 5 </h1>
    <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>

  </section>

  </main>
Enter fullscreen mode Exit fullscreen mode

We have created html structure and now we are going to design it by css.

Css Design(SCSS)

:root{
  scroll-behavior: smooth;
}
body{
  display: flex;
  flex-wrap: wrap;
  @mixin mp-0{
    margin: 0;
    padding: 0;
  }
  @include mp-0;
  menu{

    $w: 200px;
    display: block;
    @include mp-0;
    $h: 100vh;
    width: $w;
    ul{
      @include mp-0;
      margin: 0;
      padding: 0;
      list-style: none;
      position: fixed;
      width: $w;
      height: $h;
      background: #ccc;
      top: 0;
      left: 0;
      li{
        transition: .3s;
        padding: .5rem;
       &.active{
          background: deeppink;
         a{
           color: white;
         }

        }
        a{
        color: #222;
        text-decoration: none;

        }

      }
    }
  }

  main{
    width: 100%;
  @include mp-0;
  margin-left: 200px;
    padding: .5rem;
    section{
      padding:1rem;
      background: #eee;
      line-height: 28px;
      box-shadow: 0 3px 7px #222;
      margin-bottom: .5rem;
      h1{
      text-align: center;
    }
    }

  }


}


Enter fullscreen mode Exit fullscreen mode

and

Javascript

let menuSection = document.querySelectorAll('menu li');

// for clickable event
menuSection.forEach(v=> {
  v.onclick = (()=> {
   setTimeout(()=> {
      menuSection.forEach(j=> j.classList.remove('active'))
    v.classList.add('active')
  },300)
   })
})

// for window scrolldown event

window.onscroll = (()=> {
  let mainSection = document.querySelectorAll('main section');

  mainSection.forEach((v,i)=> {
    let rect = v.getBoundingClientRect().y
    if(rect < window.innerHeight-200){
      menuSection.forEach(v=> v.classList.remove('active'))
      menuSection[i].classList.add('active')
    }
  })
})
Enter fullscreen mode Exit fullscreen mode

This is our simple scrollspy tutorial.
for codpen

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (3)

Collapse
 
yellow1912 profile image
yellow1912

You should use the new observer api instead for better performance. If you use scroll event perhaps consider denouncing it?

Collapse
 
nikhilroy2 profile image
Nikhil Chandra Roy

hi, can you explain a little more what do you mean new observer api here?

Collapse
 
yellow1912 profile image
yellow1912

I mean this one:

developer.mozilla.org/en-US/docs/W...

It's a better way to detect when an element comes into view.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more