λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
EFFECT

[EFFECT] νŽ˜λŸ΄λ ‰μŠ€ 효과 04 : λ‚˜νƒ€λ‚΄κΈ° 효과

by _토맀토 2022. 9. 19.
728x90

πŸ–± νŽ˜λŸ΄λ ‰μŠ€ 효과 : μŠ€ν¬λ‘€μ„ 내릴 λ•Œ μš”μ†Œ λ‚˜νƒ€λ‚΄κΈ°πŸ–±

νŽ˜λŸ΄λž™μŠ€(parallax) μ‹œμ°¨λž€ 의미둜 μ‚¬μš©μžκ°€ μŠ€ν¬λ‘€λ§μ„ ν•  λ•Œ λ°°κ²½κ³Ό μš”μ†Œλ§ˆλ‹€ 속도 및 크기의 차이λ₯Ό μ£Όμ–΄ μž…μ²΄μ μœΌλ‘œ λŠκ»΄μ§€κ²Œ ν•˜λŠ” 효과λ₯Ό λ§ν•©λ‹ˆλ‹€.
이번 μ‹œκ°„μ—λŠ” μŠ€ν¬λ‘€μ„ 내릴 λ•Œ 각 μ„Ήμ…˜μ˜ μš”μ†Œλ“€μ΄ κ°œλ³„μ μœΌλ‘œ μ—λ‹ˆλ©”μ΄μ…˜ λ™μž‘μ„ ν•˜λ„λ‘ μ‹€μŠ΅ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€~!🀩



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

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;
    width: 98%;
    margin: 0 auto;
}

.content__item {
    width: 1000px;
    max-width: 70vw;
    margin: 30vw auto;
    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;
}

/* 각 μ„Ήμ…˜ ν•œ λ²ˆμ— λ‚˜νƒ€λ‚΄κΈ° */
/* #contents > section {
    opacity: 0;
    transition: all 1s;
}

#contents > section.show {
    opacity: 1;
} */

/* 각 μ„Ήμ…˜μ˜ μš”μ†Œλ“€μ„ κ°œλ³„μ μœΌλ‘œ λ‚˜νƒ€λ‚΄κΈ° */
#contents > section .content__item__num {
    opacity: 0;
    transform: translateY(200px);
    transition: all 1s 0.1s cubic-bezier(.1,1.01,.78,1.02);
}
#contents > section .content__item__title {
    opacity: 0;
    transform: translateX(-100px);
    transition: all 1s 0.3s cubic-bezier(.1,1.01,.78,1.02);
}
#contents > section .content__item__imgWrap {
    opacity: 0;
    transform: translateY(200px) rotate(30deg) skew(20deg);
    transition: all 1s 0.6s cubic-bezier(.1,1.01,.78,1.02);
}
#contents > section .content__item__desc {
    opacity: 0;
    transform: translateX(-200px);
    transition: all 1s 0.9s cubic-bezier(.1,1.01,.78,1.02);
}

#contents > section.show .content__item__num {
    opacity: 0.07;
    transform: translateY(0);
}
#contents > section.show .content__item__title {
    opacity: 1;
    transform: translateX(0);
}
#contents > section.show .content__item__imgWrap {
    opacity: 1;
    transform: translateY(0) rotate(0) skew(0);
}
#contents > section.show .content__item__desc {
    opacity: 1;
    transform: translateX(0);
}

/* 짝수일 λ•Œ : μš”μ†Œκ°€ 홀짝 κΈ°μ€€μœΌλ‘œ μ™Όμͺ½ 였λ₯Έμͺ½μœΌλ‘œ λ°°μΉ˜λ˜μ—ˆκΈ° λ•Œλ¬Έμ— λ°©ν–₯ 섀정을 λ§žμΆ°μ€€ 것. */
#contents > section:nth-child(even) .content__item__title {
    transform: translateX(100px);
}
#contents > section:nth-child(even).show .content__item__title {
    transform: translateX(0);
}
#contents > section:nth-child(even) .content__item__desc {
    transform: translateX(200px);
}
#contents > section:nth-child(even).show .content__item__desc {
    transform: translateX(0);
}


/* λ°˜μ‘ν˜• */
@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;
    }
}

μ„Ήμ…˜μ„ μ½˜ν…μΈ  μš”μ†Œ 별이 μ•„λ‹Œ ν•œ λ²ˆμ— λ™μž‘ν•˜λ„λ‘ ν•˜λ €λ©΄ 각 μ½˜ν…μΈ  μš”μ†Œλ“€μ—κ²Œ 속성을 λΆ€μ—¬ν•˜μ§€ μ•Šκ³  곡톡 μ„ νƒμžμ—κ²Œλ§Œ 속성을 λΆ€μ—¬ν•˜λ©΄ λ©λ‹ˆλ‹€.
λ°˜λŒ€λ‘œ 각 μ½˜ν…μΈ  μš”μ†Œλ³„λ‘œ μ›€μ§μ΄κ²Œ ν•˜κ³  μ‹Άλ‹€λ©΄ 각 μ½˜ν…μΈ  μš”μ†Œλ§ˆλ‹€ μ—λ‹ˆλ©”μ΄μ…˜μ„ μ„€μ •ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€.


#2. JAVASCRIPT : 슀크립트둜 νŽ˜λŸ΄λž™μŠ€ 효과 μ£ΌκΈ°

λ¨Όμ € 전체적인 흐름을 보고 μŠ€ν¬λ¦½νŠΈκ°€ μ–΄λ–»κ²Œ μž‘λ™λ˜λŠ”μ§€ ν™•μΈν•΄λ΄…μ‹œλ‹€! πŸ₯Ή
였늘 μŠ€ν¬λ¦½νŠΈμ—μ„œ κ°€μž₯ μ€‘μš”ν•œ ν¬μΈνŠΈλŠ” λ°”λ‘œ μž¬κ·€ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 슀크립트λ₯Ό μ§°λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

πŸ“– μž¬κ·€ν•¨μˆ˜λž€?
μž¬κ·€ν•¨μˆ˜λŠ” ν•¨μˆ˜ μ•ˆμ—μ„œ 자기 μžμ‹ μ„ ν•œ 번 더 ν˜ΈμΆœν•˜λŠ” ν•¨μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€. μžμ‹ μ˜ λ‘œμ§μ„ ν•¨μˆ˜ μ•ˆμ—μ„œ λ°˜λ³΅ν•˜λ‹€κ°€ 일정 쑰건을 λ§Œμ‘±ν•˜λ©΄ ν•¨μˆ˜ λ°–μœΌλ‘œ λ‚˜μ™€ 싀행을 멈μΆ₯λ‹ˆλ‹€.
λ§ˆμ§€λ§‰ μ‹€ν–‰ 쑰건을 μ„€μ •ν•˜μ§€ μ•Šμ€ 경우 ν•¨μˆ˜λŠ” 자기 μžμ‹ μ„ λ¬΄ν•œλŒ€λ‘œ λ°˜λ³΅μ„ ν•˜κ²Œ 되며 μ„œλ²„κ°€ λ‹€μš΄λ˜κΈ°λ„ ν•˜λ‹ˆ κΌ­ 쑰건을 μ„€μ •ν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

πŸ’‘ 전체 슀크립트 보기
//μž¬κ·€ν•¨μˆ˜ : 자기 μžμ‹ μ„ ν•œ 번 더 μ‹€ν–‰. 즉 λ¬΄ν•œλŒ€λ‘œ μ‹€ν–‰μ‹œν‚¨λ‹€. 이 λ•Œ κ·Έλƒ₯ μ‹€ν–‰μ‹œν‚¬ 경우 웹이 λ‹€μš΄λ  수 μžˆλ‹€.
function scroll(){
    //λ°˜λ³΅λ¬Έμ— μ‚¬μš©ν•  ν˜„μž¬ 슀크둀 μœ„μΉ˜ 값을 κ΅¬ν•˜μ—¬ λ³€μˆ˜μ— μ €μž₯ν•˜κΈ°
    //세가지λ₯Ό ν•œ λ²ˆμ— μž‘μ„±ν•œ μ΄μœ λŠ” ν˜Έν™˜μ„± 문제둜 μΈν•œ 였λ₯˜λ₯Ό 막기 μœ„ν•΄μ„œ 이닀. (μ•žμ˜ 값이 μž‘λ™ν•˜μ§€ μ•ŠμœΌλ©΄ λ’€μ˜ κ°’μœΌλ‘œ 뢈러였기)
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop || window.scrollY;

    //01
    //λͺ¨λ“  μ„Ήμ…˜μ„ μ„ νƒν•œ ν›„ forEach문으둜 λ°˜λ³΅μ‹œν‚€κΈ°
    document.querySelectorAll(".content__item").forEach(item =< {
    
    //02    
    //ν˜„μž¬ 슀크둀 값이 각 μ„Ήμ…˜μ˜ offsetTopκ°’ - window.innerHeight(μ‚¬μš©μž ν™”λ©΄μ—μ„œ λ³΄μ΄λŠ” λΈŒλΌμš°μ € μ˜μ—­) κ°’μ˜ 반 = ν˜„μž¬ λ³΄μ΄λŠ” λΈŒλΌμš°μ €μ˜ 반 λ†’μ΄μ˜ 이전 슀크둀 값을 ꡬ할 수 μžˆλ‹€.
    //즉 ν•΄λ‹Ή μ„Ήμ…˜ 슀크둀 값에 λ„λ‹¬ν•˜κΈ° 전에 κ·Έ 이전 슀크둀 κ°’μ—μ„œ μ—λ‹ˆλ©”μ΄μ…˜μ„ μ‹€ν–‰μ‹œν‚€λ„λ‘ ν•˜μ—¬ μ„Ήμ…˜ μŠ€ν¬λ‘€κ°’μ— 도달 ν–ˆμ„ λ•Œ μ™„μ „ν•œ μ„Ήμ…˜ μ½˜ν…μΈ λ₯Ό 확인할 수 μžˆλ„λ‘ ν•œλ‹€.
    if(scrollTop > item.offsetTop - window.innerHeight/2){
        item.classList.add("show");
    }
    });
    //03
    //requestAnimationFrame() == 슀크둀 이벀트 λ°œμƒμ‹œν‚€λŠ” λ©”μ„œλ“œ 즉 μŠ€ν¬λ¦½νŠΈμ—μ„œ μ• λ‹ˆλ©”μ΄μ…˜μ„ κ΅¬ν˜„ν•  λ•Œ 자주 μ‚¬μš©
    //requestAnimationFrame()은 λΈŒλΌμš°μ €μ— μ΅œμ ν™”λœ λ©”μ„œλ“œμ΄λ©° μ μ ˆν•œ ν”„λ ˆμž„ 속도λ₯Ό μ‹€ν–‰μ‹œν‚¨λ‹€.(κΈ°λ³Έ 1초 60ν”„λ ˆμž„) λ•Œλ¬Έμ— 슀크둀 이동 μ‹œ λΆ€λ“œλŸ½κ³  κ°€λ²Όμš΄ μ›€μ§μž„μ„ μ€€λ‹€. 
    requestAnimationFrame(scroll);
}
scroll();
//μ΄λ ‡κ²Œ 쑰건(λ©”μ„œλ“œ)을 μ„€μ •ν•œ ν›„ μ‹€ν–‰μ‹œν‚¬ 경우 λ©”λͺ¨λ¦¬λ₯Ό 적게 μ°¨μ§€ν•˜μ—¬ 웹이 λ‹€μš΄λ˜μ§€ μ•ŠλŠ”λ‹€. 

1. λͺ¨λ“  μ„Ήμ…˜μ„ μ„ νƒν•œ ν›„ 반볡문 λ§Œλ“€κΈ°
2. 반볡문 μ•ˆμ— 쑰건문을 톡해 ν•΄λ‹Ή μ„Ήμ…˜ μ˜μ—­μ˜ 반 μ •λ„μ˜ 이전 μ˜μ—­μ— 슀크둀이 올 경우 μ—λ‹ˆλ©”μ΄μ…˜μ„ μ‹€ν–‰μ‹œμΌœ ν•΄λ‹Ή μ„Ήμ…˜ μ˜μ—­μ— 슀크둀이 올 경우 μ½˜ν…μΈ κ°€ μ˜¨μ „νžˆ λ‹€ 보이도둝 μ„€μ •ν•©λ‹ˆλ‹€.
3. requestAnimationFrame()λ©”μ„œλ“œλ₯Ό 톡해 scroll ν•¨μˆ˜κ°€ 반볡이 되고 λ§ˆμ§€λ§‰ μ• λ‹ˆλ©”μ΄μ…˜μ΄ λλ‚œ 경우 λ°˜λ³΅μ„ μ€‘λ‹¨ν•˜λ„λ‘ ν•©λ‹ˆλ‹€.


728x90

λŒ“κΈ€


Lucky Charms Rainbow
js
html
css