๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

ํŒจ๋Ÿด๋ž™์Šค ์ดํŽ™ํŠธ 4. ๋‚ด์šฉ ๋‚˜ํƒ€๋‚˜๊ธฐ

by Youcodein 2022. 9. 19.
728x90
๋ฐ˜์‘ํ˜•

Javascript parallax Effect

parallax ์ดํŽ™ํŠธ ์ค‘ ๋‚ด์šฉ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ ํ˜•์ž…๋‹ˆ๋‹ค.

HTML

html ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

<main id="parallax__cont">
    <div id="contents">
        <section id="section01" 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">
                ๊ธ€์— ์œ„์ธ์˜ ์ดˆ์ƒํ™”๊ฐ€ ์žˆ๊ณ  ์œ„์ธ์˜ ์ด๋ฆ„์ด ์“ฐ์—ฌ์žˆ๋‹ค๊ณ  ํ•ด์„œ,
                ์ธํ„ฐ๋„ท์—์„œ ๋ณธ ๋ง์„ ์•„๋ฌด๊ฑฐ๋‚˜ ๋ฏฟ์ง€ ๋ง๋ผ.
            </p>
        </section>
        <!-- section01 -->
        <section id="section02" class="content__item">
            <span class="content__item__num">02</span>
            <h2 class="content__item__title">section02</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">
                ๋น„๋ก ๋‚ด์ผ ์ง€๊ตฌ์˜ ์ข…๋ง์ด ์˜จ๋‹ค ํ•˜๋”๋ผ๋„ ๋‚˜๋Š” ์˜ค๋Š˜ ํ•œ ๊ทธ๋ฃจ์˜ ์‚ฌ๊ณผ๋‚˜๋ฌด๋ฅผ ์‹ฌ๊ฒ ๋‹ค.
            </p>
        </section>
        <!-- section02 -->
        <section id="section03" class="content__item">
            <span class="content__item__num">03</span>
            <h2 class="content__item__title">section03</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">๊ทธ๋“ค์ด ์ฃฝ์Œ์„ ๋‘๋ ค์›Œํ•˜๋Š” ๊ฒƒ์ด ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. ์‚ถ์ด ์ฃฝ์Œ๋ณด๋‹ค ํ›จ์”ฌ ๋” ์•„ํ”„๋‹ค.</p>
        </section>
        <!-- section03 -->
        <section id="section04" class="content__item">
            <span class="content__item__num">04</span>
            <h2 class="content__item__title">section04</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">
                ๋ชจ๋“  ๊ฒƒ์ด ๋ณ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์œผ๋ฉด ๋ถ™์žก์œผ๋ ค ํ•  ๊ฒƒ์ด ์—†๋‹ค.
            </p>
        </section>
        <!-- section04 -->
        <section id="section05" class="content__item">
            <span class="content__item__num">05</span>
            <h2 class="content__item__title">section05</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">
                ์–ด๋–ค ์ƒ๊ฐ์„ ๋ฐ›์•„๋“ค์ด์ง€ ์•Š๊ณ  ์ฆ๊ฒ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๊ต์–‘ ์žˆ๋Š” ์ •์‹ ์˜ ํ‘œ์ง•์ด๋‹ค.
            </p>
        </section>
        <!-- section05 -->
        <section id="section06" class="content__item">
            <span class="content__item__num">06</span>
            <h2 class="content__item__title">section06</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">
                ์ฒ ํ•™์€ ์–ธ์ œ๋‚˜ ์šฐ๋ฆฌ ๋ˆˆ ์•ž์— ๋†“์—ฌ ์žˆ๋Š” ๊ทธ ์œ„๋Œ€ํ•œ ์ฑ…์— ๊ธฐ๋ก๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์šฐ์ฃผ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
            </p>
        </section>
        <!-- section06 -->
        <section id="section07" class="content__item">
            <span class="content__item__num">07</span>
            <h2 class="content__item__title">section07</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">
                ํ–‰๋ณต์€ ํž˜์ด ์ฆ๊ฐ€ํ•˜๊ณ  ์ €ํ•ญ์ด ๊ทน๋ณต๋˜๊ณ  ์žˆ๋‹ค๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค.
            </p>
        </section>
        <!-- section07 -->
        <section id="section08" class="content__item">
            <span class="content__item__num">08</span>
            <h2 class="content__item__title">section08</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">์‚ฌ๋ž‘์˜ ๊ด‘๊ธฐ๋Š” ํ•˜๋Š˜์˜ ์ถ•๋ณต ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค.</p>
        </section>
        <!-- section08 -->
        <section id="section09" class="content__item">
            <span class="content__item__num">09</span>
            <h2 class="content__item__title">section09</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc">๊ฐํžˆ ์•Œ๊ณ ! ๋‹น์‹ ์˜ ์ง€๋Šฅ์„ ์‚ฌ์šฉํ•  ์šฉ๊ธฐ๋ฅผ ๊ฐ€์ง€์‹ญ์‹œ์˜ค.</p>
        </section>
        <!-- section09 -->
    </div>
</main>

CSS

css ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

/* 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 .4s ease;
}


#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(img/effect_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(img/effect_bg09.jpg);
}

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

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

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

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

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

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

.content__item:nth-child(9) .content__item__img {
    background-image: url(img/effect_bg09.jpg);
}

.content__item__desc {
    font-size: 3vw;
    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;
}

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



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

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

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

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

/* ํ•œ๋ฒˆ์— ๋‚˜ํƒ€๋‚˜๊ธฐ
#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(0.12, 0.72, 0.48, 1.08);
}
#contents > section .content__item__title {
    opacity: 0;
    transform: translateX(-100px);
    transition: all 1s 0.3s cubic-bezier(0.12, 0.72, 0.48, 1.08);

}
#contents > section .content__item__imgWrap {
    opacity: 0;
    transform: translateY(200px) rotate(30deg) skew(20deg);
    transition: all 1s 0.6s cubic-bezier(0.12, 0.72, 0.48, 1.08);
}
#contents > section .content__item__desc {
    opacity: 0;
    transform: translateX(-200px);
    transition: all 1s 0.9s cubic-bezier(0.12, 0.72, 0.48, 1.08);
}

#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);
}

Javascript

Javascript ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

//์žฌ๊ท€ํ•จ์ˆ˜

function scroll(){
    let scrollTop = window.pageXOffset || document.documentElement.scrollTop || window.screenY

    document.querySelectorAll(".content__item").forEach(item => {
        if(scrollTop > item.offsetTop - window.innerHeight/2){
            item.classList.add("show");
        }
    });
    
    requestAnimationFrame(scroll);
}

scroll();
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€