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

ํŒจ๋Ÿด๋ž™์Šค ์ดํŽ™ํŠธ 3. ๋ฉ”๋‰ด ๋‚˜ํƒ€๋‚˜๊ธฐ / ํƒ‘๋ฒ„ํŠผ

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: -200px;
    z-index: 2000;
    background-color: rgba(0, 0, 0, 0.4);
    padding: 20px 30px;
    border-radius: 50px;
    transition: top .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(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;
}

#parallax__info {
    position: fixed;
    left: 20px;
    bottom: 20px;
    z-index: 2000;
    background-color: rgba(0, 0, 0, 0.6);
    color: #fff;
    padding: 20px;
    border-radius: 10px;
}

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

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

    #parallax__info {
        left: 10;
        bottom: 10px;
    }

}

#parallax__top {
    font-size: 14px;
    position: fixed;
    left: 50%;
    bottom: -220px;
    z-index: 10000;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.4);
    text-align: center;
    line-height: 40px;
    color: #fff;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
}

#parallax__top.show {
    bottom: 20px;
}

#parallax__top:hover {
    background: #fff;
    color: #000;
}

Javascript

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

window.addEventListener("scroll", ()=>{
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

    //01
    // if(scrollTop > 0){
    //     document.querySelector("#parallax__nav").classList.add("show");
    // } else {
    //     document.querySelector("#parallax__nav").classList.remove("show");
    // }                                ์Šคํฌ๋กค๋ฐ” 0๋ณด๋‹ค ์ปค์ง€๋ฉด ๋ฉ”๋‰ด๊ฐ€ ๋‚˜์˜จ๋‹ค.

    

    02
    if(scrollTop > (document.body.scrollHeight - window.outerHeight) ){
        document.querySelector("#parallax__top").classList.add("show");
    } else {
        document.querySelector("#parallax__top").classList.remove("show");
    }
    console.log();

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

// top ๋ฒ„ํŠผ
document.querySelector("#parallax__top").addEventListener("click", ()=>{
    window.scrollTo({left: 0, top: 0, behavior: "smooth"});
});

//03
let nowScroll = true;
let lastScroll = 0;

function scrollProgress(){
    nowScroll = true;

    setInterval(() => {
        if(nowScroll){
            nowScroll = false;
            hasScroll();
        }
    }, 300);
}

function hasScroll(){
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

    if(scrollTop < lastScroll){
        document.querySelector("#parallax__nav").classList.add("show");
    } else {
        document.querySelector("#parallax__nav").classList.remove("show");
    }
    lastScroll = scrollTop;
}

window.addEventListener("scroll", scrollProgress);
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€