본문 바로가기
Javascript응용_Effect

Parallax Effect 07

by 코딩대원 2022. 10. 11.

패럴랙스 이펙트 07

페럴랙스 이펙트 일곱 번째 시간입니다.
이번 시간에는 스크롤을 내릴때 리빌(reveal) 효과를 줬습니다.

01. HTML 코드

data-delay와 class에 reveal을 추가했습니다.

{
    <main id="parallax__cont">
    <div id="contents">
        <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 reveal">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal" data-delay="1500">결과도 중요하지만, 과정을 더 중요하게 생각한다.</p>
        </section>
        <!-- //section1 -->
        <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 reveal reveal-RTL">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal" data-delay="500">피할수 없으면 즐겨라</p>
        </section>
        <!-- //section2 -->
        <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 reveal reveal-TTB">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal" data-delay="500">승리하면 조금 배울 수 있고 패배하면 모든 것을 배울 수 있다</p>
        </section>
        <!-- //section3 -->
        <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 reveal reveal-RTL" data-delay="500">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal">과거를 지배하는 자가 마래를 지배하며 현재를 지배하는 자가 과거를 지배한다.</p>
        </section>
        <!-- //section4 -->
        <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 reveal reveal-BTT">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal">세상은 고통으로 가득하지만 한편 그것을 이겨내는 일로도 가득차 있다</p>
        </section>
        <!-- //section5 -->
        <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 reveal reveal-RTL">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal">가장 하기 힘든 일은 아무 일도 안하는 것이다</p>
        </section>
        <!-- //section6 -->
        <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 reveal reveal-TWO reveal-BTT">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal reveal-TWO">말을 많이 하는 것과 말을 잘하는 것은 다르다.</p>
        </section>
        <!-- //section7 -->
        <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 reveal reveal-RTL reveal-TWO">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal reveal-TTB reveal-BTT">오늘 할 수 있는 일에 전력을 다하라, 그러면 내일에는 한 걸음 더 진보한다.</p>
        </section>
        <!-- //section8 -->
        <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 reveal reveal-TTB reveal-BTT">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc reveal reveal-BTT reveal-TWO" data-delay="500">끝을 맺기를 처음과 같이 하면 실패가 없다.</p>
        </section>
        <!-- //section9 -->
    </div>
</main>
}

02. CSS 코드

키프레임을 이용해서 다양한 방향의 애니메이션 효과를 만들었습니다.

{
    #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 .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-color: #fff;
        color: #000;
        border-radius: 20px;
        box-sizing: content-box;
    }
    #parallax__cont {
        max-width: 1600px;
        width: 98%;
        margin: 0 auto;
        /* background-color: rgba(255,255,255,0.1); */
    }
    .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){
        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 {
        left: auto;
        right: -5vw;
    }
    .content__item__title {
        font-weight: 400;
        text-transform: capitalize;
        
    }
    .content__item__imgWrap {
        width: 100%;
        padding-bottom: 56.25%;
        position: relative;
        overflow: hidden;
        z-index: -1;
    }
    .content__item__img {
        position: absolute;
        background: url(../assets/img/effect_bg18-min.jpg);
        background-repeat: no-repeat;
        background-position: center center;
        background-size: cover;
        width: 110%;
        height: 110%;
        left: -5%;
        top: -5%;
        filter: saturate(0%);
        /* transition: all 1s; */
        padding-top: 8vw;
    }
    .content__item:nth-child(1) .content__item__img {
        background-image: url(../assets/img/effect_bg14-min.jpg);
    }
    .content__item:nth-child(2) .content__item__img {
        background-image: url(../assets/img/effect_bg15-min.jpg);
    }
    .content__item:nth-child(3) .content__item__img {
        background-image: url(../assets/img/effect_bg16-min.jpg);
    }
    .content__item:nth-child(4) .content__item__img {
        background-image: url(../assets/img/effect_bg17-min.jpg);
    }
    .content__item:nth-child(5) .content__item__img {
        background-image: url(../assets/img/effect_bg18-min.jpg);
    }
    .content__item:nth-child(6) .content__item__img {
        background-image: url(../assets/img/effect_bg19-min.jpg);
    }
    .content__item:nth-child(7) .content__item__img {
        background-image: url(../assets/img/effect_bg01-min.jpg);
    }
    .content__item:nth-child(8) .content__item__img {
        background-image: url(../assets/img/effect_bg02-min.jpg);
    }
    .content__item:nth-child(9) .content__item__img {
        background-image: url(../assets/img/effect_bg03-min.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 {
            left: 10px;
            bottom: 10px;
        }
    }
    #parallax__info {
        position: fixed;
        left: 20px;
        bottom: 20px;
        z-index: 2000;
        background-color: rgba(0,0,0,0.4);
        color: #fff;
        padding: 20px;
        border-radius: 10px;
    }
    /* option */
    .reveal > div,
    .reveal > span {
        opacity: 0;
    }
    .reveal.show > div {
        animation: opacity 1s linear forwards;
    }
    .reveal.show > span {
        opacity: 1;
    }
    .reveal {
        position: relative;
    }
    .reveal::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 0;
        height: 100%;
        background-color: #fff;
        z-index: 1;
    }
    .reveal.reveal-TWO::after {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 0;
        height: 100%;
        z-index: 1;
        background: orange;
    }

    /* 1개 */
    .reveal.show::before {
        animation: reveal 1s;
    }
    .reveal.reveal-RTL.show:before {
        animation: reveal-RTL 1s;
    }
    .reveal.reveal-TTB.show:before {
        animation: reveal-TTB 1s;
    }
    .reveal.reveal-BTT.show:before {
        animation: reveal-BTT 1s;
    }
    
    /* 2개 */
    .reveal.show::after {
        animation: reveal 1s 0.3s;
    }
    .reveal.reveal-RTL.show::after {
        animation: reveal-RTL 1s 0.3s;
    }
    .reveal.reveal-TTB.show::after {
        animation: reveal-TTB 1s 0.3s;
    }
    .reveal.reveal-BTT.show::after {
        animation: reveal-BTT 1s 0.3s;
    }

    @keyframes opacity {
        0%   {opacity: 0;}
        60%  {opacity: 0;}
        70%  {opacity: 1;}
        100% {opacity: 1;}
    }
    @keyframes reveal {
        0%   {width: 0;    left: 0}
        50%  {width: 100%; left: 0;}
        80%  {width: 100%; left: 0;}
        100% {width: 0;    left: 100%;}
    }
    @keyframes reveal-RTL {
        0%   {width: 0;    right: 0; left: auto;}
        50%  {width: 100%; right: 0; left: auto;}
        80%  {width: 100%; right: 0; left: auto;}
        100% {width: 0;    right: 100%; left: auto;}
    }
    @keyframes reveal-TTB {
        0%   {width: 100%; height: 0; top: 0;}
        50%  {width: 100%; height: 100%; top: 0;}
        80%  {width: 100%; height: 100%; top: 0;}
        100% {width: 100%; height: 0; top: 100%;}
    }
    @keyframes reveal-BTT {
        0%   {width: 100%; height: 0;    bottom: 0;       top: auto;}
        50%  {width: 100%; height: 100%; bottom: 0;       top: auto;}
        80%  {width: 100%; height: 100%; bottom: 0;       top: auto;}
        100% {width: 100%; height: 0;    bottom: 100%;    top: auto;}
    }
}

03. script 코드

리빌 클래스를 span태그로 감싸줍니다.
scrollTop 값을 구한 뒤, revealOffset와 revealDelay를 정의해줍니다.
조건문을 걸어서 스크롤이 원하는 영역에 왔을때 효과를 나타나게 해줍니다.

{
    // reveal 클래스 자식에 글씨만 있다면 가상으로 span 태그 만들어서 넣어주세요
    const reveal = document.querySelectorAll('.reveal');
     reveal.forEach((el) => {
         let text = el.innerText;
         if (text) {
             el.innerHTML = "<span>" + text + "</span>";
         }
     });

     function scroll() {
         let scrollTop = window.scrollY;
         reveal.forEach((el, index) => {
             let revealOffset = el.offsetTop + el.parentElement.offsetTop;
             let revealDelay = el.dataset.delay;

             // if(scrollTop > revealOffset - window.innerHeight) {
             //     el.classList.add('show');
             // }

             if (scrollTop > revealOffset - window.innerHeight / 2) {
                 if (!revealDelay) {
                     el.classList.add('show');
                 }
                 else {
                     setTimeout(() => {
                         el.classList.add("show");
                     }, revealDelay);
                 }
             }
         });
         requestAnimationFrame(scroll);
     }
     scroll();
}

'Javascript응용_Effect' 카테고리의 다른 글

Search Effect06  (0) 2022.10.18
Slider Effect 05  (0) 2022.10.14
Search Effect 05  (4) 2022.09.30
Parallax Effect 06  (2) 2022.09.30
Mouse Effect 06  (2) 2022.09.30

댓글


HTML
CSS

JAVASCRIPT

자세히 보기