๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript Effect

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒจ๋Ÿด๋ž™์Šค ํšจ๊ณผ : ๋ฉ”๋‰ด ์ด๋™

by ์ฝ”ํŒŒ์นด 2022. 9. 6.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ํŒจ๋Ÿด๋ž™์Šค ํšจ๊ณผ : ๋ฉ”๋‰ด ์ด๋™

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹ค์–‘ํ•œ ํšจ๊ณผ๋“ค ์ค‘, ํŒจ๋Ÿด๋ž™์Šค ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. ์Šคํฌ๋กค์˜ ์œ„์น˜์— ๋”ฐ๋ผ ๋ฉ”๋‰ด ํƒญ์˜ ํด๋ž˜์Šค์— ๋ณ€ํ™”๋ฅผ ์ฃผ๊ณ , ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฉ”๋‰ด๊ฐ€ ์žˆ๋Š” ๊ณณ์œผ๋กœ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™ํ•˜๊ฒŒ๋” ์ž‘์—…ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


HTML ์†Œ์Šค

HTML์€ ๋จผ์ € nav, main, aside ์„ธ ํŒŒํŠธ๋กœ ๋‚˜๋ˆ„์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. nav์—๋Š” 9๊ฐœ์˜ ๋ฉ”๋‰ด, main์—๋Š” 9๊ฐœ์˜ ์ฝ˜ํ…์ธ , aside์—๋Š” ์Šคํฌ๋กค์˜ ์œ„์น˜๊ฐ’ ๋ชฉ๋ก์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

<nav id="parallax__nav">
    <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>
            <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>
        <!-- //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">
                <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>
            <h2 class="content__item__title">section3</h2>
            <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>
            <h2 class="content__item__title">section4</h2>
            <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>
            <h2 class="content__item__title">section5</h2>
            <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>
            <h2 class="content__item__title">section6</h2>
            <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>
            <h2 class="content__item__title">section7</h2>
            <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>
            <h2 class="content__item__title">section8</h2>
            <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>
            <h2 class="content__item__title">section9</h2>
            <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>
    <div class="info">
        <ul>
            <li>#section1 offset() : <span class="offset1">0</span>px</li>
            <li>#section2 offset() : <span class="offset2">0</span>px</li>
            <li>#section3 offset() : <span class="offset3">0</span>px</li>
            <li>#section4 offset() : <span class="offset4">0</span>px</li>
            <li>#section5 offset() : <span class="offset5">0</span>px</li>
            <li>#section6 offset() : <span class="offset6">0</span>px</li>
            <li>#section7 offset() : <span class="offset7">0</span>px</li>
            <li>#section8 offset() : <span class="offset8">0</span>px</li>
            <li>#section9 offset() : <span class="offset9">0</span>px</li>
        </ul>
    </div>
</aside>
<!-- //parallax__info -->

CSS ์†Œ์Šค

CSS๋กœ ์Šคํƒ€์ผ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”๋‰ด์™€ info ๋ฐ•์Šค๋Š” position: fixed๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ๊ณ ์ •๋˜๊ฒŒ ์„ค์ •ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

/* parallaxType */
/* 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;
}
#parallax__nav li {
    display: inline;
    margin: 0 5px;
}
#parallax__nav li a {
    display: inline-block;
    height: 30px;
    padding: 5px 10px;
    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;
    /* 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: 7vw;
}
/* nth-child์˜ ๊ด„ํ˜ธ ์•ˆ์—๋Š” ์ˆ˜์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค, even = 2n (์ง์ˆ˜๋งŒ ์„ ํƒ) */
.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 ๊ฐ’ ์ดˆ๊ธฐํ™” */
    left: auto;
    right: -5vw;
}
.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;
    background-image: url(../assets/img/effect_bg20-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;
}
.content__item:nth-child(1) .content__item__img {
    background-image: url(../assets/img/effect_bg18-min.jpg);
}
.content__item:nth-child(2) .content__item__img {
    background-image: url(../assets/img/effect_bg17-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_bg15-min.jpg);
}
.content__item:nth-child(5) .content__item__img {
    background-image: url(../assets/img/effect_bg14-min.jpg);
}
.content__item:nth-child(6) .content__item__img {
    background-image: url(../assets/img/effect_bg13-min.jpg);
}
.content__item:nth-child(7) .content__item__img {
    background-image: url(../assets/img/effect_bg19-min.jpg);
}
.content__item:nth-child(8) .content__item__img {
    background-image: url(../assets/img/effect_bg20-min.jpg);
}
.content__item:nth-child(9) .content__item__img {
    background-image: url(../assets/img/effect_bg04-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;
}
#parallax__info {
    position: fixed;
    left: 10px;
    bottom: 10px;
    z-index: 2000;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    padding: 20px;
    border-radius: 10px;
}
#parallax__info li,
.scrollTop {
    line-height: 1.4;
}

@media (max-width: 800px) {
    #parallax__cont {
        margin-top: 70vw;
    }
    #parallax__nav {
        padding: 10px;
        right: auto;
        left: 10px;
        top: 10px;
        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;
    }
}

JAVASCRIPT ์†Œ์Šค

window.addEventListener("scroll", () => {
    // 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋‚˜ ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ์ ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹ฌ๋ผ์„œ,
    // 3๊ฐ€์ง€ ์ „๋ถ€ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ.
    // 1. let scrollTop = window.pageYOffset;
    // 2. let scrollTop = window.scrollY;
    // 3. let scrollTop = document.documentElement.scrollTop;
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

    // if (scrollTop > document.getElementById("section1").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(1)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section2").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(2)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section3").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(3)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section4").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(4)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section5").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(5)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section6").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(6)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section7").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(7)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section8").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(8)").classList.add("active");
    // }
    // if (scrollTop > document.getElementById("section9").offsetTop) {
    //     document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //         li.classList.remove("active");
    //     });
    //     document.querySelector("#parallax__nav li:nth-child(9)").classList.add("active");
    // }

    // for๋ฌธ
    // for (let i = 1; i <= 9; i++) {
    //     if (scrollTop > document.getElementById("section" + i).offsetTop) {
    //         document.querySelectorAll("#parallax__nav li").forEach((li) => {
    //             li.classList.remove("active");
    //         });
    //         document.querySelector("#parallax__nav li:nth-child(" + i + ")").classList.add("active");
    //     }
    // }

    // forEach๋ฌธ
    document.querySelectorAll(".content__item").forEach((el, i) => {
        // ์˜ค์ฐจ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด -2
        if (scrollTop > el.offsetTop - 2) {
            document.querySelectorAll("#parallax__nav li").forEach((li) => {
                li.classList.remove("active");
            });
            document.querySelector("#parallax__nav li:nth-child(" + (i + 1) + ")").classList.add("active");
        }
    });

    // info
    document.querySelector(".scroll span").innerText = Math.round(scrollTop);

    // document.querySelector(".offset1").innerText = document.getElementById("section1").offsetTop;
    // document.querySelector(".offset2").innerText = document.getElementById("section2").offsetTop;
    // document.querySelector(".offset3").innerText = document.getElementById("section3").offsetTop;
    // document.querySelector(".offset4").innerText = document.getElementById("section4").offsetTop;
    // document.querySelector(".offset5").innerText = document.getElementById("section5").offsetTop;
    // document.querySelector(".offset6").innerText = document.getElementById("section6").offsetTop;
    // document.querySelector(".offset7").innerText = document.getElementById("section7").offsetTop;
    // document.querySelector(".offset8").innerText = document.getElementById("section8").offsetTop;
    // document.querySelector(".offset9").innerText = document.getElementById("section9").offsetTop;

    for (let i = 1; i <= 9; i++) {
        document.querySelector(".offset" + i).innerText = document.getElementById("section" + i).offsetTop;
    }
});

// ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜๋ฉด ์Šคํฌ๋กค ์ด๋™์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ
// ๋”ฑ๋”ฑํ•œ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋จผ์ € ๋ง‰์•„์ฃผ๊ณ , ์ƒˆ๋กญ๊ฒŒ ๋ถ€๋“œ๋Ÿฌ์šด(smooth) ์ด๋ฒคํŠธ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
document.querySelectorAll("#parallax__nav li a").forEach((li) => {
    li.addEventListener("click", (e) => {
        e.preventDefault();
        document.querySelector(li.getAttribute("href")).scrollIntoView({
            behavior: "smooth",
        });
    });
});

์ ์šฉํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘