๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[EFFECT] ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ07 : ํ…์ŠคํŠธ ํšจ๊ณผ

by _ํ† ๋งคํ†  2022. 10. 21.
728x90

๐Ÿ–ฑ ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ : ํ…์ŠคํŠธ ํšจ๊ณผ ๐Ÿ–ฑ

ํŽ˜๋Ÿด๋ž™์Šค(parallax)๋ž€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์Šคํฌ๋กค๋ง์„ ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ(๋ฐฐ๊ฒฝ๊ณผ ์š”์†Œ๋งˆ๋‹ค ์†๋„ ๋ฐ ํฌ๊ธฐ์˜ ์ฐจ์ด๋ฅผ ์ฃผ์–ด ์ž…์ฒด์ ์œผ๋กœ ๋Š๊ปด์ง€๊ฒŒ ํ•˜๋Š” ํšจ๊ณผ ๋“ฑ)๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์Šคํฌ๋กค์„ ๋‚ด๋ฆด ๋•Œ ํ…์ŠคํŠธ๋ฅผ ์ชผ๊ฐœ์–ด ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ๋งŒ๋“ค์–ด ์ค€ ๋’ค css์˜ ๋”œ๋ ˆ์ด ์†์„ฑ์„ ํ†ตํ•ด ๊ธ€์”จ๊ฐ€ ํ•˜๋‚˜์”ฉ ์›€์ง์ด๋„๋ก ์‹ค์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿซ ๐Ÿซ ๐Ÿซ 



#1. HTML / CSS ์„ค์ •ํ•˜๊ธฐ

โœ๏ธ HTML ๋ณด๊ธฐ
<main id="parallax__cont">
  <div id="contents">
    <section id="section1" class="content__item">
      <span class="content__item__num">01</span>
      <h1 class="content__item__title">section1</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ๋ฏธ๋ž˜๋Š” ํ˜„์žฌ ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”๊ฐ€์— ๋‹ฌ๋ ค ์žˆ๋‹ค.
      </p>
    </section>
    <!-- //section1 -->

    <section id="section2" class="content__item">
      <span class="content__item__num">02</span>
      <h1 class="content__item__title">section2</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ์–ด์ œ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์—†๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋‚˜๋Š” ์–ด์ œ์™€๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋˜์—ˆ๊ธฐ
        ๋•Œ๋ฌธ์ด๋‹ค.
      </p>
    </section>
    <!-- //section2 -->

    <section id="section3" class="content__item">
      <span class="content__item__num">03</span>
      <h1 class="content__item__title">section3</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ์–ธ์ œ๋‚˜ ํ˜„์žฌ์— ์ง‘์ค‘ํ• ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ–‰๋ณตํ• ๊ฒƒ์ด๋‹ค.
      </p>
    </section>
    <!-- //section3 -->

    <section id="section4" class="content__item">
      <span class="content__item__num">04</span>
      <h1 class="content__item__title">section4</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ๋จผ์ € ํ•€ ๊ฝƒ์€ ๋จผ์ €์ง„๋‹ค ๋‚จ๋ณด๋‹ค ๋จผ์ € ๊ณต์„ ์„ธ์šฐ๋ ค๊ณ  ์กฐ๊ธ‰ํžˆ ์„œ๋‘˜๊ฒƒ์ด
        ์•„๋‹ˆ๋‹ค.
      </p>
    </section>
    <!-- //section4 -->

    <section id="section5" class="content__item">
      <span class="content__item__num">05</span>
      <h1 class="content__item__title">section5</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ์ข‹์€ ์„ฑ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ํ•œ ๊ฑธ์Œ ํ•œ ๊ฑธ์Œ์ด ํž˜์ฐจ๊ณ  ์ถฉ์‹คํ•˜์ง€ ์•Š์œผ๋ฉด ์•ˆ
        ๋œ๋‹ค.
      </p>
    </section>
    <!-- //section5 -->

    <section id="section6" class="content__item">
      <span class="content__item__num">06</span>
      <h1 class="content__item__title">section6</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ์„ฑ๊ณต์˜ ๋น„๊ฒฐ์€ ๋‹จ ํ•œ ๊ฐ€์ง€, ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์— ๊ด‘์ ์œผ๋กœ ์ง‘์ค‘ํ•˜๋Š”
        ๊ฒƒ์ด๋‹ค.
      </p>
    </section>
    <!-- //section6 -->

    <section id="section7" class="content__item">
      <span class="content__item__num">07</span>
      <h1 class="content__item__title">section7</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ๊ฟˆ์„ ๊ณ„์† ๊ฐ„์งํ•˜๊ณ  ์žˆ์œผ๋ฉด ๋ฐ˜๋“œ์‹œ ์‹คํ˜„ํ•  ๋•Œ๊ฐ€ ์˜จ๋‹ค.
      </p>
    </section>
    <!-- //section7 -->

    <section id="section8" class="content__item">
      <span class="content__item__num">08</span>
      <h1 class="content__item__title">section8</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ํ”ํžˆ ์‚ฌ๋žŒ๋“ค์€ ๊ธฐํšŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ์ง€๋งŒ ๊ธฐํšŒ๋Š” ๊ธฐ๋‹ค๋ฆฌ๋Š” ์‚ฌ๋žŒ์—๊ฒŒ ์žกํžˆ์ง€
        ์•Š๋Š” ๋ฒ•์ด๋‹ค.
      </p>
    </section>
    <!-- //section8 -->

    <section id="section9" class="content__item">
      <span class="content__item__num">09</span>
      <h1 class="content__item__title">section9</h1>
      <figure class="content__item__imgWrap">
        <div class="content__item__img"></div>
      </figure>
      <p class="content__item__desc">
        ์ด๋ฏธ๋๋‚˜๋ฒ„๋ฆฐ ์ผ์„ ํ›„ํšŒํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ํ•˜๊ณ  ์‹ถ์—ˆ๋˜ ์ผ๋“ค์„ ํ•˜์ง€๋ชปํ•œ ๊ฒƒ์„
        ํ›„ํšŒํ•˜๋ผ.
      </p>
    </section>
    <!-- //section9 -->
  </div>
</main>
<!-- //main -->

<aside id="parallax__info">
  <div class="scroll">scrollTop : <span>0</span>px</div>
</aside>
<!-- //parallax__info -->

html์€ ๋ฉ”๋‰ด, ์„น์…˜, ์„น์…˜์— ๋“ค์–ด๊ฐˆ ์ฝ˜ํ…์ธ (์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ)๋“ฑ์„ ๋งˆํฌ์—…ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์ด ๋•Œ ์„น์…˜์— ๋“ค์–ด๊ฐ€๋Š” ์ฝ˜ํ…์ธ ๋“ค์—๊ฒŒ ์„ ํƒ์ž๋“ค์„ ๋ถ€์—ฌํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ์™€ ์Šคํƒ€์ผ์„ ์ฃผ๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์—†๋„๋ก ํ•ฉ๋‹ˆ๋‹ค:)

โœ๏ธ CSS ๋ณด๊ธฐ
/* option : ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋™์ž‘ ์„ค์ •๋งŒ ํ•ด์ฃผ๊ณ  ๋”œ๋ ˆ์ด ์„ค์ •์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ฃผ๊ธฐ */
.split span {
  opacity: 0;
  transform: translateY(50px);
  transition: all 0.3s cubic-bezier(0, 0.71, 0.11, 1.66);
  display: inline-block;
  min-width: 1vw;
}

.split span.show  { 
  opacity: 1;
  transform: translateY(0);
}

/* option : ๋…ธ๊ฐ€๋‹ค ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ž‘์—… : ํ…์ŠคํŠธ๊ฐ€ ์ชผ๊ฐœ์–ด์ง„ ๋งŒํผ :nth-child๋กœ ๊ฐ๊ฐ 50ms์”ฉ ๋”œ๋ ˆ์ด ์„ค์ • ํ•ด์ฃผ๊ธฐ. */
/* .split span {
  opacity: 0;
  transform: translateY(50px);
  transition: all 0.3s cubic-bezier(0, 0.71, 0.11, 1.66);
  display: inline-block;
  min-width: 1vw;
}

.split.show span {
  opacity: 1;
  transform: translateY(0);
} */

/* .split span:nth-child(1) {
  transition-delay: 100ms;
}
.split span:nth-child(2) {
  transition-delay: 150ms;
}
.split span:nth-child(3) {
  transition-delay: 200ms;
}
.split span:nth-child(4) {
  transition-delay: 250ms;
}
.split span:nth-child(5) {
  transition-delay: 300ms;
}
.split span:nth-child(6) {
  transition-delay: 350ms;
}
.split span:nth-child(7) {
  transition-delay: 400ms;
}
.split span:nth-child(8) {
  transition-delay: 450ms;
}
.split span:nth-child(9) {
  transition-delay: 500ms;
}
.split span:nth-child(10) {
  transition-delay: 550ms;
}
.split span:nth-child(11) {
  transition-delay: 600ms;
}
.split span:nth-child(12) {
  transition-delay: 650ms;
}
.split span:nth-child(13) {
  transition-delay: 700ms;
}
.split span:nth-child(14) {
  transition-delay: 750ms;
}
.split span:nth-child(15) {
  transition-delay:800ms;
}
.split span:nth-child(16) {
  transition-delay: 850ms;
}
.split span:nth-child(17) {
  transition-delay: 900ms;
}
.split span:nth-child(18) {
  transition-delay: 950ms;
}
.split span:nth-child(19) {
  transition-delay: 1000ms;
}
.split span:nth-child(20) {
  transition-delay: 1050ms;
}
.split span:nth-child(21) {
  transition-delay: 1100ms;
}
.split span:nth-child(22) {
  transition-delay: 1150ms;
}
.split span:nth-child(23) {
  transition-delay: 1200ms;
}
.split span:nth-child(24) {
  transition-delay: 1250ms;
}
.split span:nth-child(25) {
  transition-delay: 1300ms;
}
.split span:nth-child(26) {
  transition-delay: 1350ms;
}
.split span:nth-child(27) {
  transition-delay: 1400ms;
}
.split span:nth-child(28) {
  transition-delay: 1450ms;
}
.split span:nth-child(29) {
  transition-delay: 1500ms;
}
.split span:nth-child(30) {
  transition-delay: 1550ms;
}
.split span:nth-child(31) {
  transition-delay: 1600ms;
}
.split span:nth-child(32) {
  transition-delay: 1650ms;
}
.split span:nth-child(33) {
  transition-delay: 1700ms;
}
.split span:nth-child(34) {
  transition-delay: 1750ms;
}
.split span:nth-child(35) {
  transition-delay: 1800ms;
}
.split span:nth-child(36) {
  transition-delay: 1850ms;
}
.split span:nth-child(37) {
  transition-delay: 1900ms;
}
.split span:nth-child(38) {
  transition-delay: 1950ms;
}
.split span:nth-child(39) {
  transition-delay: 2000ms;
}
.split span:nth-child(40) {
  transition-delay: 2050ms;
}
.split span:nth-child(41) {
  transition-delay: 2100ms;
}
.split span:nth-child(42) {
  transition-delay: 2150ms;
}
.split span:nth-child(43) {
  transition-delay: 2200ms;
} */

CSS ์†์„ฑ์€ ํŽ˜๋Ÿด๋ ‰์Šค05๋ฒˆ๊ณผ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค! ์œ„์˜ CSS๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์Šคํฌ๋ฆฝํŠธ๋กœ ๋”œ๋ ˆ์ด๋ฅผ ์„ค์ •ํ•  ์‹œ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก CSS๋กœ ๋™์  ํšจ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ ์Šคํฌ๋ฆฝํŠธ๋กœ span์„ ์„ ํƒํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— showํด๋ž˜์Šค์˜ ์œ„์น˜๋ฅผ span์˜†์œผ๋กœ ์ด๋™์‹œ์ผœ์ค๋‹ˆ๋‹ค.


#2. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ๋กœ ํŽ˜๋Ÿด๋ž™์Šค ํšจ๊ณผ ์ฃผ๊ธฐ

์˜ค๋Š˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” forEach๋ฐ˜๋ณต๋ฌธ์—์„œ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ…์ŠคํŠธ๋ฅผ ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ์ชผ๊ฐœ์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ชผ๊ฐœ์–ด์ง„ ํ…์ŠคํŠธ์—๊ฒŒ ๊ฐ๊ฐ ๋”œ๋ ˆ์ด๋ฅผ ์„ค์ •ํ•˜์—ฌ ์Šคํฌ๋กค์ด ๋‚ด๋ ค๊ฐˆ ๋•Œ ๊ธ€์”จ๊ฐ€ ํ•˜๋‚˜์”ฉ ๋‚˜ํƒ€๋‚˜๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿผ ์ฃผ์„๊ณผ ํ•จ๊ป˜ ์ „์ฒด์ ์ธ ํ๋ฆ„์„ ์‚ดํŽด ๋ด…์‹œ๋‹ค!

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
//๊ธ€์”จ ์ชผ๊ฐœ๊ธฐ - ํ•œ ์„น์…˜์˜ ํ…์ŠคํŠธ๋งŒ ์ชผ๊ฐœ๊ธฐ
// let text = document.querySelector("#section1 .content__item__desc"); //์ฒซ๋ฒˆ์งธ ํ…์ŠคํŠธ
// let splitText = text.innerText;
// //๋ฌธ์ž์—ด ๊ฐ์ฒด ์ฐธ๊ณ 
// let splitWrap = splitText.split('').join('</span><span>');
// text.innerHTML = splitWrap = "<span>" + splitWrap + "</span>";


//๊ธ€์”จ ์ชผ๊ฐœ๊ธฐ (์—ฌ๋Ÿฌ๊ฐœ : forEach๋ฌธ ์ด์šฉ ์ฟผ๋ฆฌ All)
// let text = document.querySelectorAll("#contents .content__item__desc");
// text.forEach(el => {
//   let splitText = el.innerText;
//   let splitWrap = splitText.split('').join('</span><span>');
//   el.innerHTML = splitWrap = "<span>" + splitWrap + "</span>";
// });


//01
//๊ธ€์”จ ์ชผ๊ฐœ๊ธฐ ๋‹ค์ค‘ : ์ชผ๊ฐœ๊ณ  ์‹ถ์€ ์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ ์‹œ์ผœ ๊ทธ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜จ๋‹ค.
//๊ทธ ๋’ค ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ split์„ ํ†ตํ•ด ํ…์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ชผ๊ฐœ ์ฃผ๊ณ  join()์„ ์ด์šฉํ•˜์—ฌ ๊ฐ๊ฐ spanํƒœ๊ทธ๋ฅผ ๊ฐ์‹ธ์ค€๋‹ค.(์ด๋•Œ ๋‹ซ๋Š” ํƒœ๊ทธ์™€ ์—ฌ๋Š” ํƒœ๊ทธ๊ฐ€ ๋ฐ˜๋Œ€๋กœ ๋˜๋ฏ€๋กœ join์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์จ์ค€๋‹ค.)
//์ชผ๊ฐฐ์„ ๋•Œ ์ ‘๊ทผ์„ฑ์ด ์ข‹์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ์ธ join()์œผ๋กœ ๋„ฃ์–ด์ค€ spanํƒœ๊ทธ ์•ˆ์— aria-hidden='true' ์†์„ฑ์„ ๋„ฃ์–ด ์ˆจ๊ฒจ์ค€๋‹ค.(์Šคํฌ๋ฆฐ ๋ฆฌ๋”๊ธฐ์—๋งŒ ์•ˆ์ฝํž˜.)
document.querySelectorAll(".split").forEach(text => {
    let splitText = text.innerText;
    let splitWrap = splitText.split('').join("</span><span aria-hidden='true'>");
    //๊ฐ€์žฅ ์•ž ๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์—๋Š” join์ด ๋จนํžˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๋”ฐ๋กœ ๋ณ€์ˆ˜ ์•ˆ์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค.
    splitWrap = "<span aria-hidden='true'>" + splitWrap + "</span>";
    //์œ„์˜ ๋ฐ”๋€ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋‹ค์‹œ ์ €์žฅ์‹œํ‚จ๋‹ค.
    text.innerHTML = splitWrap;
    //์œ„์— ์ˆจ๊ธด ํ…์ŠคํŠธ๋ฅผ ๋ฆฌ๋”๊ธฐ์— ์ฝํžˆ๊ธฐ ํ•˜๊ธฐ ์œ„ํ•ด aria-label ์†์„ฑ์„ ๋ถ™์—ฌ์ค€๋‹ค.
    text.setAttribute("aria-label", splitText);
    // console.log(splitText);
});


//02
//์Šคํฌ๋กค ๊ฐ’ ๊ตฌํ•˜๊ณ  ์Šคํฌ๋กค ์›€์ง์ผ ๋•Œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„ํ•˜๊ธฐ.
function scroll() {
    let scrollTop = window.scrollY;
    document.querySelector("#parallax__info .scroll").innerText = Math.round(scrollTop);

    //CSS ์Šคํƒ€์ผ ์„ค์ •์„ ์œ„ํ•œ ํด๋ž˜์Šค ์ถ”๊ฐ€ ์Šคํฌ๋ฆฝํŠธ. (๋…ธ๊ฐ€๋‹ค ver. : CSS๋กœ ์ผ์ผํžˆ ๊ฐ ํ•œ ํ…์ŠคํŠธ ๋‹น ๋”œ๋ ˆ์ด ์„ค์ •์„ ์ค€ ๋’ค ํด๋ž˜์Šค๋งŒ ์ถ”๊ฐ€.)
    // document.querySelectorAll(".content__item").forEach(item => {
    //   if(scrollTop >= item.offsetTop){
    //     item.querySelector(".split").classList.add("show");
    //   }
    // })
    
    //03 : CSS์Šคํƒ€์ผ ์ฃผ๊ธฐ
    document.querySelectorAll(".content__item").forEach(item => {
    //๋งŒ์•ฝ ์Šคํฌ๋กค๊ฐ’์ด ๊ฐ ์„น์…˜์˜ ์˜คํ”„์…‹ ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด.
    if( scrollTop >= item.offsetTop){
        //spanํƒœ๊ทธ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•˜์—ฌ ์ธ๋ฑ์Šค ๊ฐ’๊นŒ์ง€ ๋ถˆ๋Ÿฌ์˜จ ๋’ค (span = element)
        item.querySelectorAll(".split span").forEach((element, index) => {
        //ํด๋ž˜์Šค ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ†ตํ•ด span ์š”์†Œ์—๊ฒŒ ํด๋ž˜์Šค show๋ฅผ ๋„ฃ์–ด ํ…์ŠคํŠธ๊ฐ€ ๋ณด์ด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
        element.classList.add("show");

        //๋˜ํ•œ CSS์†์„ฑ ์ค‘ ๋”œ๋ ˆ์ด๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•œ ๊ธ€์ž ๋‹น 50ms์”ฉ ์›€์ง์ด๋„๋ก ํ•œ๋‹ค.
        element.style.transitionDelay = `${index * 50}ms`;
        });
    }
    });

    requestAnimationFrame(scroll); //(1์ดˆ์— 60๋ฒˆ ์‹คํ–‰. : ์žฌ๊ท€ํ•จ์ˆ˜)

}
scroll(); //์‹คํ–‰

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ •๋ฆฌ ๐Ÿ“Œ

//01 : ๊ธ€์”จ ์ชผ๊ฐœ๊ธฐ ๋‹ค์ค‘ : ์ชผ๊ฐœ๊ณ  ์‹ถ์€ ์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ ์‹œํ‚จ๋‹ค.
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);
});

//02 : ์Šคํฌ๋กค ๊ฐ’ ๊ตฌํ•˜๊ณ  ์Šคํฌ๋กค ์›€์ง์ผ ๋•Œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„ํ•˜๊ธฐ.
function scroll() {
  let scrollTop = window.scrollY;
  document.querySelector("#parallax__info .scroll").innerText = Math.round(scrollTop);
  
  //03 : CSS์Šคํƒ€์ผ ์ฃผ๊ธฐ
  document.querySelectorAll(".content__item").forEach(item => {
  if( scrollTop >= item.offsetTop){
      item.querySelectorAll(".split span").forEach((element, index) => {
      element.classList.add("show");
      element.style.transitionDelay = `${index * 50}ms`;
      });
  }
  });

  requestAnimationFrame(scroll);

}
scroll(); //์‹คํ–‰


728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css