본문 바로가기

아래로 스크롤 해주세요!

My Reference Book

-

제가 배웠던것을 한곳에 정리해보았어요!

HTML

HTML 태그 톺아보기

HTML

자세히보기

CSS

CSS 속성 톺아보기

CSS

자세히보기

JAVASCRIPT

JS 실행문 톺아보기

JAVASCRIPT

자세히보기

최신댓글

JS 응용하기

패럴렉스 이펙트_06 : 스크롤 값에 따라 텍스트를 순차적으로 나오게 하기

by C0Di 2022. 9. 29.
728x90

패럴렉스 이펙트_06 : 스크립트로 각 span에 CSS 애니메이션 적용하기

방법은 여러가지가 있겠지만
각 요소의 offsetTop값이 scrollTop(현재 스크롤 값) 이상이 되면 .split span의 요소를 찾아 애니메이션을 적용시키는 이펙트를 구현해보도록 하겠습니다!


HTML 코드 : 뼈대

간단한 html 구조를 만들어 뼈대를 구성해보도록 합시다!

<main id="parallax__cont">
  <div id="contents">

      <!-- section1 시작 -->
      <section id="section1" class="content__item">
          <span class="content__item__num">01</span>
          <h2 class="content__item__title">section1</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">우리는 사랑하는 친구들에 의해서만 알려진다. </p>
      </section>
      <!-- section1 끝 -->

      <!-- section2 시작 -->
      <section id="section2" class="content__item">
          <span class="content__item__num">02</span>
          <h2 class="content__item__title">section2</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">사랑은 눈으로 보지 않고 마음으로 보는 거지. </p>
      </section>
      <!-- section2 끝 -->

      <!-- section3 시작 -->
      <section id="section3" class="content__item">
          <span class="content__item__num">03</span>
          <h2 class="content__item__title">section3</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">두 분은 상처 하나 입지 않고, 대용사가 된 셈이오.</p>
      </section>
      <!-- section3 끝 -->

      <!-- section4 시작 -->
      <section id="section4" class="content__item">
          <span class="content__item__num">04</span>
          <h2 class="content__item__title">section4</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">그의 믿음은 그가 쓰는 모자의 유행처럼 변한다.</p>
      </section>
      <!-- section4 끝 -->

      <!-- section5 시작 -->
      <section id="section5" class="content__item">
          <span class="content__item__num">05</span>
          <h2 class="content__item__title">section5</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">잘 있거라! 우리가 언제 다시 만날지는 아무도 모른다.</p>
      </section>
      <!-- section5 끝 -->

      <!-- section6 시작 -->
      <section id="section6" class="content__item">
          <span class="content__item__num">06</span>
          <h2 class="content__item__title">section6</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">죄를 짓고 잘되는 사람도 있고, 덕을 베풀고 망하는 사람도 있다.</p>
      </section>
      <!-- section6 끝 -->

      <!-- section7 시작 -->
      <section id="section7" class="content__item">
          <span class="content__item__num">07</span>
          <h2 class="content__item__title">section7</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">모두를 사랑하되, 몇 사람만 믿으라. 누구에게도 잘못을 저지르지 말라.</p>
      </section>
      <!-- section7 끝 -->

      <!-- section8 시작 -->
      <section id="section8" class="content__item">
          <span class="content__item__num">08</span>
          <h2 class="content__item__title">section8</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">누구에게나 결점은 있기 마련이지. 그분의 경우는 솔직하다는 게 결점이야.</p>
      </section>
      <!-- section8 끝 -->

      <!-- section9 시작 -->
      <section id="section9" class="content__item">
          <span class="content__item__num">09</span>
          <h2 class="content__item__title">section9</h2>
          <figure class="content__item__imgWrap">
              <div class="content__item__img"></div>
          </figure>
          <p class="content__item__desc split">사람은 누구나 주어진 일과 원하는 것이 있다, 비록 보잘 것 없을 지라도. </p>
      </section>
      <!-- section9 끝 -->
  </div>
</main>
<!-- //main -->

CSS 코드 : 살

이제 뼈대를 구성하였으니 살을 붙여 searchEffect를 구현하기위한 준비를 마치겠습니다.

/* parallax__nav */
  #parallax__nav {
      position: fixed;
      right: 20px;
      top: 20px;
      z-index: 2000;
      background-color: rgba(0, 0, 0, 0.4);
      padding: 20px 30px;
      border-radius: 50px;
      transition: top 0.4s ease;
  }

  #parallax__nav li {
      display: inline;
      margin: 0 5px;
  }

  #parallax__nav li a {
      display: inline-block;
      height: 30px;
      padding: 5px 20px;
      text-align: center;
      line-height: 30px;
  }

  #parallax__nav li.active a {
      background: #fff;
      color: #000;
      border-radius: 20px;
      box-sizing: content-box;
  }

  #parallax__cont {
      max-width: 1600px;
      /* background-color: rgba(255, 255, 255, 0.1); */
      margin: 0 auto;
      width: 98%;
  }

  .content__item {
      width: 1000px;
      max-width: 70vw;
      margin: 30vw auto;
      /* background-color: rgba(255, 255, 255, 0.3); */
      text-align: left;
      margin-right: 0;
      position: relative;
      padding-top: 8vw;
  }

  .content__item:nth-child(even) {
      /* 2n도 가능하고 even 도 가능하고 */
      margin-left: 0;
      text-align: right;
  }

  .content__item__num {
      font-size: 35vw;
      font-family: 'Lato';
      font-weight: 100;
      position: absolute;
      left: -5vw;
      top: -16vw;
      opacity: 0.07;
      z-index: -2;
  }

  .content__item:nth-child(even) .content__item__num {
      right: -5vw;
      left: auto;
      /* left 값 초기화 */
  }

  .content__item__title {
      font-weight: 400;
      text-transform: capitalize;
  }

  .content__item__imgWrap {
      width: 100%;
      padding-bottom: 56.25%;
      background-color: #000;
      position: relative;
      overflow: hidden;
      z-index: -1;
  }

  .content__item__img {
      background: url(../../assets/slider/effect_bg_01.jpg);
      background-position: center center;
      background-repeat: no-repeat;
      background-size: cover;
      /* 내가 작아서 넣음 */
      position: absolute;
      width: 110%;
      height: 110%;
      left: -5%;
      top: -5%;
      filter: saturate(0%);
      /* transition: all 1s; */
  }

  .content__item:nth-child(1) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_01.jpg);
  }

  .content__item:nth-child(2) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_02.jpg);
  }

  .content__item:nth-child(3) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_03.jpg);
  }

  .content__item:nth-child(4) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_04.jpg);
  }

  .content__item:nth-child(5) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_05.jpg);
  }

  .content__item:nth-child(6) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_06.jpg);
  }

  .content__item:nth-child(7) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_07.jpg);
  }

  .content__item:nth-child(8) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_08.jpg);
  }

  .content__item:nth-child(9) .content__item__img {
      background-image: url(../../assets/slider/effect_bg_09.jpg);
  }

  .content__item__desc {
      font-size: 4vw;
      line-height: 1.4;
      margin-top: -5vw;
      margin-left: -4vw;
      word-break: keep-all;
  }

  .content__item:nth-child(even) .content__item__desc {
      margin-left: auto;
      margin-right: 4vw;
  }

  #parallax__info {
      position: fixed;
      left: 20px;
      bottom: 20px;
      z-index: 2000;
      background: rgba(0, 0, 0, 0.6);
      color: #fff;
      padding: 20px;
      border-radius: 10px;
  }

  #parallax__info li,
  .scrollTop {
      line-height: 1.4;
  }

  @media (max-width: 800px) {
      #parallax__cont {
          margin-top: 70vw;
      }

      #parallax__nav {
          padding: 10px;
          right: auto;
          left: 10px;
          top: 10px;
          transform: translateY(50%);
          border-radius: 5px;
          background-color: rgba(0, 0, 0, 0.8);
      }

      #parallax__nav li {
          display: block;
          margin: 5px;
      }

      #parallax__nav li a {
          font-size: 14px;
          padding: 5px;
          border-radius: 5px;
          height: auto;
          line-height: 1;
      }

      #parallax__nav li.active a {
          border-radius: 5px;
      }
  }

  .split span {
      opacity: 0;
      transform: translateX(0px);
      transition: all 0.3s ease-in-out;
      display: inline-block;
      min-width: 1vw;
  }

JS 코드 : dataset을 통해 값을 찾고 switch를 통해 중요도 표시하기

저는 최대한 간단하게 설명만 가능하므로 비교적 전문적인 용어는 배제한 형태로 설명해볼게요!

1. 글자를 공백을 기준으로 나눈 뒤 각 부분마다 </span> <span aria-hidden='true'>을 붙여 웹 표준을 준수합니다.
2. "scroll" 함수를 만들어준뒤 별도의 CSS 지정 없이도 각 span에 애니메이션이 나오도록 해주는 내용을 담아둡니다.
3. scroll함수의 원리를 작성하려면 content__item의 요소들을 forEach를 통해 선택하고
4. if를 통해 scrollTop(현재 스크롤 하고있는 값)offsetTop(브라우저에서 최상단의 값)의 이상이면
5. forEach를 통해 나왔던 요소에 forEach를 써 요소의 요소(.split span)를 선택하고
6. 이렇게 선택한 요소(.split span)의 인덱스 값을 통해 .split span의 nth-child 값을 1씩 증가시켜 각각 원하는 애니메이션을 설정합니다. * 애니메이션 딜레이는 평소 사용하던 0.(i)s로 설정하면 순서가 뒤죽박죽 되는 경우가 있어 (i)+00ms로 설정해 순차적으로 나오게 해줍니다.


//글씨 쪼개기
  let text = document.querySelectorAll(".content__item__desc");

  text.forEach((e, i) => {
      
  })

  document.querySelectorAll(".split").forEach(text => {
      let splitText = text.innerText;
      let splitWrap = splitText.split('').join("</span> <span aria-hidden='true'>");
      splitWrap = "<span aria-hidden='true'>" + splitWrap + "</span>";
      text.innerHTML = splitWrap;
      text.setAttribute("aria-label", splitText);
  })

  function scroll() {
      let scrollTop = window.scrollY;
      document.querySelector("#parallax__info .scroll").innerText = Math.round(scrollTop);
          
      document.querySelectorAll(".content__item").forEach(item=>{
          if(scrollTop >= item.offsetTop){
              item.querySelectorAll(".split span").forEach((e,i)=>{
                  e.classList.add("show")
                  item.querySelector(".split span:nth-child("+(i+1)+")").style.transform="translateX(10px)";
                  item.querySelector(".split span:nth-child("+(i+1)+")").style.transitionDelay=i+"00ms";//"0."+i+"s"
                  item.querySelector(".split span:nth-child("+(i+1)+")").style.opacity="1";
              })
          }
      })

      requestAnimationFrame(scroll);
  }
  scroll();

완성!

드디어~ 모든 과정이 끝났습니다
계속 두렵네요...


728x90
반응형

댓글

#HASH_TAGS

-

1

scroll-snap-type 필터선택자 숙제가 다양해서 너무 좋아요 오늘은 내가바로 오징어! 이건 또 뭐람 오늘도 웹표준은.. 오징어 한마리 수확 완료! 다크모드 scroll-snap-align 코드 오늘 조업 마감했습니다. 슬라이드 결과 : 월요일 선택해주세요 내일은 즐거운 월요일 ImageSlideEffect 화사한가요? JQuery 코딩 메서드 멈추지 않는 ' j ' 시리-즈 제이쿼리 오징어 1Kg 당 3000원 공부 HTML 울적하니 꽃을 달아봤습니다 오징어 두마리 포획 완료 Method