Javascript Scrollspy를 만드는 방법(Vanilla Js 자습서)

안녕하세요 여러분,
이 튜토리얼에서는 바닐라 js로 javascript scrollspy를 만들 것입니다.

HTML 구조



<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>

우리는 html 구조를 만들었고 이제 css로 디자인할 것입니다.

CSS 디자인(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;
    }
    }

  }


}



그리고

자바스크립트



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')
    }
  })
})

이것은 우리의 간단한 scrollspy 튜토리얼입니다.
코펜을 위해

좋은 웹페이지 즐겨찾기