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

[EFFECT] ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ05 : ์ด์งˆ๊ฐ ํšจ๊ณผ

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

๐Ÿ–ฑ ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ : ์ด์งˆ๊ฐ ํšจ๊ณผ ๐Ÿ–ฑ

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



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

โœ๏ธ HTML ๋ณด๊ธฐ
<nav id="parallax__nav" class="show">
    <ul>
      <li class="active"><a href="#section1">๋ฉ”๋‰ด1</a></li>
      <li><a href="#section2">๋ฉ”๋‰ด2</a></li>
      <li><a href="#section3">๋ฉ”๋‰ด3</a></li>
      <li><a href="#section4">๋ฉ”๋‰ด4</a></li>
      <li><a href="#section5">๋ฉ”๋‰ด5</a></li>
      <li><a href="#section6">๋ฉ”๋‰ด6</a></li>
      <li><a href="#section7">๋ฉ”๋‰ด7</a></li>
      <li><a href="#section8">๋ฉ”๋‰ด8</a></li>
      <li><a href="#section9">๋ฉ”๋‰ด9</a></li>
    </ul>
  </nav>
  <!-- //parallax__nav -->
  
  <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 ๋ณด๊ธฐ
/* parallax__nav */
#parallax__nav {
    position: fixed;
    right: 20px;
    top: -200px;
    z-index: 2000;
    background-color: rgba(95, 95, 95, 0.4);
    padding: 20px 30px;
    border-radius: 50px;
    transition: top 0.4s ease;
}

#parallax__nav.show {
    top: 20px;
}

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

#parallax__nav li a {
    display: inline-block;
    /* width: 30px; */
    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); */
    width: 98%;
    margin: 0 auto;
}

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

.content__item:nth-child(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;
}

.content__item__title {
    font-weight: 400;
    text-transform: capitalize; /* ์ฒซ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž */
}

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

.content__item__img {
    position: absolute;
    width: 110%;
    height: 110%;
    left: -5px;
    top: -5px;
    background-image: url(../assets/img/city_bg02.jpg);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    filter: saturate(0%);
    /* transition: all 1s; */
}

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

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

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

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

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

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

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

.content__item:nth-child(9) .content__item__img {
    background-image: url(../assets/img/Mountian_bg05.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;
}

/* ๋ฐ˜์‘ํ˜• */
@media (max-width: 800px) {
    #parallax__cont {
        margin-top: 70vw;
    }

    #parallax__nav {
        padding: 10px;
        right: auto;
        left: 10px;
        top: 50%;
        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;
    }
}

/* ํŽ˜๋Ÿด๋ ‰์Šค ์ธํฌ */
#parallax__info {
    position: fixed;
    left: 20px;
    bottom: 20px;
    z-index: 2000;
    background: rgba(0, 0, 0.4);
    color: #fff;
    padding: 20px;
    border-radius: 10px;
}

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

์„น์…˜์„ ์ฝ˜ํ…์ธ  ์š”์†Œ ๋ณ„์ด ์•„๋‹Œ ํ•œ ๋ฒˆ์— ๋™์ž‘ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด ๊ฐ ์ฝ˜ํ…์ธ  ์š”์†Œ๋“ค์—๊ฒŒ ์†์„ฑ์„ ๋ถ€์—ฌํ•˜์ง€ ์•Š๊ณ  ๊ณตํ†ต ์„ ํƒ์ž์—๊ฒŒ๋งŒ ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋ฐ˜๋Œ€๋กœ ๊ฐ ์ฝ˜ํ…์ธ  ์š”์†Œ๋ณ„๋กœ ์›€์ง์ด๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ฐ ์ฝ˜ํ…์ธ  ์š”์†Œ๋งˆ๋‹ค ์—๋‹ˆ๋ฉ”์ด์…˜์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


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

๋จผ์ € ์ „์ฒด์ ์ธ ํ๋ฆ„์„ ๋ณด๊ณ  ์ฃผ์„๊ณผ ํ•จ๊ป˜ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค! ๐Ÿฅน

์˜ค๋Š˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋„ ์ง€๋‚œ ์‹œ๊ฐ„๊ณผ ๊ฐ™์ด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ฆ‰ ์˜ค๋Š˜์˜ ์Šคํฌ๋ฆฝํŠธ ํฌ์ธํŠธ๋Š”! ์žฌ๊ท€ํ•จ์ˆ˜์™€ ์ด์งˆ๊ฐ์„ ์ฃผ๋„๋ก ์ด๋™ ๊ฑฐ๋ฆฌ์˜ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๋Š” ์‹, ๊ทธ๋ฆฌ๊ณ  gsap์„ ์ด์šฉํ•œ ์Šคํฌ๋กค ์—๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„ ์ž…๋‹ˆ๋‹ค~!
๊ทธ๋Ÿผ ์ฝ”๋“œ๋ณด๋Ÿฌ ์Š~~ ํ•ด๋ณด์•„์š”~๐Ÿš—๐Ÿ’จ (์žฌ๊ท€ํ•จ๊ท€์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํŽ˜๋Ÿด๋ ‰์Šค04๋ฒˆ์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”~)

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
//ํ•จ์ˆ˜ ์„ค์ •
function scroll(){

    //01 scrollTop๊ฐ’ ๊ตฌํ•˜๊ณ  ๊ฐ’์„ ์„ ํƒ ์š”์†Œ์— ์ถœ๋ ฅ
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop;

    document.querySelector("#parallax__info span").innerText = Math.ceil(scrollTop);

    //02 ์ด์งˆ๊ฐ ํšจ๊ณผ ์ฃผ๊ธฐ
    document.querySelectorAll(".content__item").forEach(item => {
        //์„ ํƒ์ž
        //์„ ํƒํ•œ ์ฝ˜ํ…์ธ (๋„˜๋ฒ„, ์ด๋ฏธ์ง€, ํ…์ŠคํŠธ)๋“ค์„ ํƒ€๊ฒŸ(๋ณ€์ˆ˜)์—๊ฒŒ ์„ค์ •ํ•œ๋‹ค.
        const taget1 = item.querySelector(".content__item__img");
        const taget2 = item.querySelector(".content__item__desc");
        const taget3 = item.querySelector(".content__item__num");

        //์ด๋™์‹œํ‚ฌ ๊ฑฐ๋ฆฌ (์˜ค์ฐจ์ฃผ๊ธฐ)
        //ํ˜„์žฌ ์Šคํฌ๋กค ํƒ‘ ๊ฐ’์ด 1000์ผ ๊ฒฝ์šฐ -item.offsetTop(์„น์…˜์˜ ์‹œ์ž‘ ์Šคํฌ๋กค๊ฐ’ ์œ„์น˜)๋ฅผ ํ•˜๋ฉด 0์ด ๋‚˜์˜จ๋‹ค. ์ด๋•Œ ์Šคํฌ๋กค์„ ๋‚ด๋ฆฌ๋ฉด scrollTop์€ ๋‹ค์‹œ ๋ณธ๋ž˜์˜ ๊ฐ’์œผ๋กœ ๋Œ์•„์˜ค๋ฉฐ ๋‚ด๋ ธ์„ ๋•Œ์˜ ๊ฐ’์—์„œ ๋‹ค์‹œ item.offsetTop์„ ๋บ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์— 0.1์„ ๊ณฑํ•  ๊ฒฝ์šฐ ๋‚˜์˜ค๋Š” ๊ฐ’์ด ๋ณ€์ˆ˜์—๊ฒŒ ๋Œ€์ž…๋˜๋Š” ๊ฒƒ์ด๋‹ค.
        //scrollTop๊ณผ item.offsetTop๊ฐ’์€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— 0์ด ๋‚˜์˜ค๋Š” ๊ฒƒ.
        //์ฆ‰ scrollTop์ด content__item(์„น์…˜)์˜ ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ๊ฐ’์„ 0์œผ๋กœ ๋งŒ๋“  ๋’ค ๋‹ค์‹œ scrollTop์ด ์ด๋™ํ•  ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์— ์›ํ•˜๋Š” ์˜ค์ฐจ๊ฐ’์„ ๊ณฑํ•˜์—ฌ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. ๊ทธ ๊ฐ’์ด y์ถ•์˜ ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ค.
        //๋‹ค์Œ content__item(์„น์…˜)์— ๋„๋‹ฌํ•  ๊ฒฝ์šฐ ๊ฐ’์€ ๋‹ค์‹œ 0์ด ๋œ๋‹ค.
        let offset1 = (scrollTop - item.offsetTop) * 0.1;
        let offset2 = (scrollTop - item.offsetTop) * 0.15;
        let offset3 = (scrollTop - item.offsetTop) * 0.2;
        
        //03 GSAP
        // ์Šคํฌ๋ฆฝํŠธ๋กœ ์Šคํƒ€์ผ์„ ์„ค์ •ํ•  ๊ฒฝ์šฐ : css์Šคํƒ€์ผ ์ค‘ transform์„ ํ†ตํ•ด ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ์˜ ๊ฐ ๊ณ ์ •์œ„์น˜์—์„œ ์›€์ง์ด๋ ค๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๊ฐ๊ฐ์ด ์ฝ˜ํ…์ธ ๋“ค์˜ ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ์Šคํฌ๋กค์ด ์›€์ง์ผ ๋•Œ ์ด์งˆ๊ฐ์„ ์ค€๋‹ค.
        // taget1.style.transform = `translateY(${offset}px)`;
        // taget2.style.transform = `translateX(${offset}px)`;

        //gsap์œผ๋กœ ์Šคํƒ€์ผ ์ฃผ๊ธฐ : ๋” ๋งŽ์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์†์„ฑ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ์›€์ง์ž„์ด ๋ถ€๋“œ๋Ÿฝ๋‹ค. (ํ•œ ๋ฒˆ์— ์Šคํƒ€์ผ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌ) : ์ฃผ์˜ํ•  ์ ์€ ๊ผญ gsap์˜ CDN์„ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ.
        //์Šคํƒ€์ผ์„ ์ค„ ์š”์†Œ, {duration : ์ง€์†์‹œ๊ฐ„, translateX or Y(gsap์—์„œ๋Š” x, y): ์„ ํƒํ•œ ์ถ• ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ๊ฐ’, ease: ์ „ํ™˜ ํšจ๊ณผ (์›€์ง์ž„ ์„ค์ • : ์‹œ์ž‘๊ณผ ๋์˜ ์†๋„ ์กฐ์ ˆ ๋“ฑ)}
        gsap.to(taget1, {duration: .3, y: offset1, ease: "power4.out"});
        gsap.to(taget2, {duration: .3, y: offset2});
        gsap.to(taget3, {duration: .3, y: offset2, ease: "expo.out"});
    });

    //04 : 1์ดˆ์— 60๋ฒˆ ์‹คํ–‰
    //ํ•จ์ˆ˜๊ฐ€ ๋ฌดํ•œํ•˜๊ฒŒ ๋ฐ˜๋ณต๋˜์–ด๋„ ํ•œ ๋ฒˆ ์›€์ง์ผ ๋•Œ 60๋ฒˆ๋งŒ ์‹คํ–‰์ด๋ผ๋Š” ์ œํ•œ์ด ๊ฑธ๋ ค์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๊ฐ€ ๋‹ค์šด์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
    requestAnimationFrame(scroll); 
}
scroll(); //์‹คํ–‰๋ฌธ

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

document.querySelectorAll(".content__item").forEach(item => {
    let offset1 = (scrollTop - item.offsetTop) * 0.1;   //100์ผ ๊ฒฝ์šฐ 10
    let offset2 = (scrollTop - item.offsetTop) * 0.15;  //100์ผ ๊ฒฝ์šฐ 15
    let offset3 = (scrollTop - item.offsetTop) * 0.2;   //100์ผ ๊ฒฝ์šฐ 20
});

1. scrollTop์ด item.offsetTop(= content__item.offsetTop)์˜ ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ๊ฐ’์„ 0์œผ๋กœ ๋งŒ๋“  ๋’ค ๋‹ค์‹œ scrollTop์ด ์ด๋™ํ•  ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์— ์›ํ•˜๋Š” ์˜ค์ฐจ๊ฐ’์„ ๊ณฑํ•˜์—ฌ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
2. ๋ณ€์ˆ˜์— ๋Œ€์ž… ๋œ ๊ฐ’์ด ๋ฐ”๋กœ ๋‹ค์Œ์— ์„ค์ •ํ•  translateY์˜ ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ 0์—์„œ 100๋งŒํผ ์ด๋™ํ•œ๋‹ค ๊ฐ€์ •ํ•˜์—ฌ ๊ณฑํ•œ ๊ฐ’์ด 0.1์ผ ๋•Œ Y์ถ•์œผ๋กœ 10px์„ ์›€์ง์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
3. ๊ทธ ๋’ค ๋‹ค์Œ item.offsetTop(= content__item)์— ๋„๋‹ฌํ•  ๊ฒฝ์šฐ ๊ฐ’์€ ๋‹ค์‹œ 0์ด ๋ฉ๋‹ˆ๋‹ค.

document.querySelectorAll(".content__item").forEach(item => {
    gsap.to(taget1, {duration: .3, y: offset1, ease: "power4.out"});
    gsap.to(taget2, {duration: .3, y: offset2});
    gsap.to(taget3, {duration: .3, y: offset2, ease: "expo.out"});
});

1. gsap์„ ์‚ฌ์šฉํ•˜์—ฌ CSS ์›€์ง์ž„ ์„ค์ •์„ ํ•ด์ค„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ์†์„ฑ๊ณผ ๋ถ€๋“œ๋Ÿฌ์šด ์›€์ง์ž„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
2. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์Šคํƒ€์ผ์„ ์ค„ ์š”์†Œ, {duration : ์ง€์†์‹œ๊ฐ„, translateX or Y(gsap์—์„œ๋Š” x, y): ์ถ• ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ๊ฐ’, ease: ์ „ํ™˜ ํšจ๊ณผ (์›€์ง์ž„ ์„ค์ • : ์‹œ์ž‘๊ณผ ๋์˜ ์†๋„ ์กฐ์ ˆ ๋“ฑ)}์œผ๋กœ ์ž‘์„ฑํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
3. ์ด ๋•Œ translateY์— offset1์„ ๋Œ€์ž…ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ์ „์ฒด 100px์„ ์›€์ง์ผ ๊ฒฝ์šฐ translateY๋Š” 10px์„ ์›€์ง์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

TIP! ์ข€ ๋” ๋‹ค์–‘ํ•œ ease๋ฅผ ์„ค์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ GSAP ํ™ˆํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€์„œ ํ™•์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค! ๋˜ํ•œ GSAP์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ CDN์„ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค!!

๐Ÿ“– ์ •๋ฆฌ ๐Ÿ“–

1. ์„น์…˜1 offsetTop๊ฐ’ : 500px / ์„น์…˜2 offsetTop๊ฐ’ : 1000px => scrollTop == 500px / scrollTop == 1000px ์ผ ๋•Œ 0px์ด ๋ฉ๋‹ˆ๋‹ค.

2. ๋‹ค์‹œ ์›€์ง์ธ scrollTop๊ฐ’์ด 700์ผ ๋•Œ ๋‹ค์‹œ ์„น์…˜1์˜ offsetTop๊ฐ’์„ ๋ปฐ ๊ฒฝ์šฐ 200์ด ๋˜๋ฉฐ * 0.1์„ ํ–ˆ์„ ๋•Œ 20์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ตœ์ข…์ ์œผ๋กœ translateY์—๊ฒŒ ์ด ๊ฐ’์„ ๋Œ€์ž…ํ•˜๊ฒŒ ๋˜๋ฉด ์Šคํฌ๋กค์„ ๋‚ด๋ฆด ๋•Œ ํ•ด๋‹น ์ปจํ…์ธ ๊ฐ€ 20px์”ฉ ๋‚ด๋ ค๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ด๋™ ํšจ๊ณผ๋ฅผ ๋” ํฌ๊ฒŒ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ๊ณฑํ•˜๋Š” ๊ฐ’์„ ๋” ํฌ๊ฒŒ ์„ค์ •ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

3. ๊ทธ ๋’ค ๋‹ค์Œ ์„น์…˜ ๊ฐ’์ธ 1000์— ๋˜์—ˆ์„ ๋•Œ scrollTop๊ฐ’์€ ๋‹ค์‹œ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๊ณ„์†ํ•ด์„œ ๋ชจ๋“  content__item๋“ค์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์ž‘๋™๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css