๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[EFFECT] ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ 03 : top๋ฒ„ํŠผ ๊ตฌํ˜„๊ณผ ๋ฉ”๋‰ด ์ˆจ๊น€ ํšจ๊ณผ

by _ํ† ๋งคํ†  2022. 9. 14.
728x90

๐Ÿ–ฑ ํŽ˜๋Ÿด๋ ‰์Šค ํšจ๊ณผ : top ๋ฒ„ํŠผ ๊ตฌํ˜„ํ•˜๊ธฐ๐Ÿ–ฑ

ํŽ˜๋Ÿด๋ž™์Šค(parallax) ์‹œ์ฐจ๋ž€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์Šคํฌ๋กค๋ง์„ ํ•  ๋•Œ ๋ฐฐ๊ฒฝ๊ณผ ์š”์†Œ๋งˆ๋‹ค ์†๋„ ๋ฐ ํฌ๊ธฐ์˜ ์ฐจ์ด๋ฅผ ์ฃผ์–ด ์ž…์ฒด์ ์œผ๋กœ ๋Š๊ปด์ง€๊ฒŒ ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” top๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ ์Šคํฌ๋กค์ด ์ตœ์ƒ๋‹จ์œผ๋กœ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ์‹ค์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ ๋ฒ„ํŠผ์ด ๊ฐ€์žฅ ํ•˜๋‹จ์—์„œ๋งŒ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  ๋ฉ”๋‰ด ๋ฒ„ํŠผ๋„ ์Šคํฌ๋กค์„ ๋‚ด๋ฆด ์‹œ ๋ณด์ด์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜†



#1. HTML / CSS ์„ค์ •ํ•˜๊ธฐ

โœ๏ธ HTML ๋ณด๊ธฐ
<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 -->

<nav id="parallax__top">Top</nav>
<!-- //parallax__top -->

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

<aside id="parallax__info">
    <div class="scroll">scrollTop : <span>0</span>px</div>
</aside>
โœ๏ธ 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__top {
    position: fixed;
    left: 50%;
    bottom: -220px;
    z-index: 10000;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    background-color: 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-color: #fff;
    color: #000;
}

#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(../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;
}

#parallax__info {
    position: fixed;
    left: 20px;
    bottom: 20px;
    z-index: 2000;
    background: rgba(0, 0, 0.4);
    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: 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;
    }
}

#2. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ๋กœ ํŽ˜๋Ÿด๋ž™์Šค ํšจ๊ณผ ์ฃผ๊ธฐ

๋จผ์ € ์ „์ฒด์ ์ธ ํ๋ฆ„์„ ๋ณด๊ณ  ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค! ๐Ÿฅน

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
// 01. top๋ฒ„ํŠผ ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค ๊ฐ’์—๋งŒ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
// ๋ฌธ์„œ ์ „์ฒด ๋†’์ด - ๋ธŒ๋ผ์šฐ์ € ๋†’์ด( = ๋งˆ์ง€๋ง‰ ์„น์…˜์— ์Šคํฌ๋กค์ด ์œ„์น˜ํ–ˆ์„ ๋•Œ  top๋ฒ„ํŠผ ๋„์šฐ๊ธฐ)
//๋ฌธ์„œ์˜ ์Šคํฌ๋กค ์ „์ฒด๊ฐ’ <= ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’ + ๋ธŒ๋ผ์šฐ์ € ๋‚ด์—์„œ ๋ณด์ด๋Š” ์Šคํฌ๋กค์˜ ๋†’์ด ๊ฐ’
window.addEventListener("scroll", ()=> {
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    if(document.documentElement.scrollHeight <= scrollTop + window.innerHeight){
        document.querySelector("#parallax__top").classList.add("show");
    }else {
        document.querySelector("#parallax__top").classList.remove("show");
    }
});



//02. top๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์˜ฌ๋ผ์˜ค๊ฒŒ ํ•˜๊ธฐ
document.querySelector("#parallax__top").addEventListener("click", ()=> {
    window.scrollTo({left: 0, top: 0, behavior:"smooth"}); //scrollTo : ์ ˆ๋Œ€ ๊ธฐ์ค€์˜ ์Šคํฌ๋กค ๊ฐ’ ์„ค์ •์œผ๋กœ 0px์˜ ์ตœ์ƒ๋‹จ์„ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (By๋Š” ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’ ์œ„์น˜์™€ ์ด๋™ํ•˜๋ ค๋Š” ์Šคํฌ๋กค์œ„์ฐจ๊ฐ’์˜ ๊ฑฐ๋ฆฌ๋งŒ ์„ค์ •.)
});

// 03.  ์Šคํฌ๋กค ๊ฐ’์„ ์›€์ง์ผ ๋•Œ ๋งˆ๋‹ค ์ž‘๋™๋˜๋Š” ์ด๋ฒคํŠธ ์„ค์ •ํ•˜๊ธฐ
// ํ˜„์žฌ ๊ฐ’์ด 3000์ธ ์Šคํฌ๋กค์„ ๋‚ด๋ ธ์„ ๋•Œ ์Šคํฌ๋กค ๊ฐ’์ด 3000๋ณด๋‹ค ๋†’์•„์ง€๋ฉด ๋ฉ”๋‰ด๊ฐ€ ๋ณด์ด์ง€ ์•Š์•„์•ผ ํ•˜๊ณ  ๋‹ค์‹œ ์Šคํฌ๋กค์„ ์˜ฌ๋ ค์„œ ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์งˆ ๋•Œ ๋‹ค์‹œ ๋ณด์—ฌ์•ผ ํ•œ๋‹ค.
// ํ˜„์žฌ ๊ฐ’์€ ๊ณ„์† ๋ณ€๋™ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์˜ ์ด ์Šคํฌ๋กค ๊ฐ’๋„ ์ฝ˜ํ…์ธ ๊ฐ€ ์ถ”๊ฐ€๋  ์ˆ˜๋ก ๊ณ„์† ๋ณ€ํ•œ๋‹ค. ์ฆ‰ ์ด๋Ÿฌํ•œ ๋ณ€ํ™”๋ฅผ ๊ณ ๋ คํ•˜์—ฌ 1์ดˆ ์ „์ด๋‚˜ 30์ดˆ ์ „ ์ฆ‰ ๊ณผ๊ฑฐ์˜ ์Šคํฌ๋กค๊ฐ’๊ณผ ํ˜„์žฌ์˜ ์Šคํฌ๋กค๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ์‹์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.
let nowScroll = true;   // ํŠธ๋ฆฌ๊ฑฐ ๋ณ€์ˆ˜ ์ด์šฉ //nowScroll๊ฐ€ true์ผ ๋•Œ ์‹คํ–‰
let lastScroll = 0;

function scrollProgress(){
    // info์— ํ˜„์žฌ ์Šคํฌ๋กค ์œ„์น˜ ๊ฐ’ ๊ตฌํ˜„ํ•˜๊ธฐ
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    document.querySelector('#parallax__info span').innerText = Math.ceil(scrollTop);

    nowScroll = true;

    // nowScroll = false์ผ ๋•Œ hasScroll()ํ•จ์ˆ˜๊ฐ€ 0.3์ดˆ ๋งˆ๋‹ค ์‹คํ–‰๋˜๋„๋ก ์„ค์ • : ๊ณ„์† ์‹คํ–‰ ์‹œ ๋ฒ„๋ฒ…๊ฑฐ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์Šคํฌ๋กค๊ฐ’์— ์•ฝ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ๋‘๊ธฐ ์œ„ํ•ด setInterval๋กœ ์‹œ๊ฐ„์ฐจ๋ฅผ ์ค€ ๊ฒƒ.
    setInterval(() => {
        if(nowScroll){
            nowScroll = false;
            hasScroll();
    }
    },300); 
}

function hasScroll(){
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    //console.log("ํ˜„์žฌ ๊ฐ’",scrollTop, "์ด์ „ ๊ฐ’", lastScroll)

    if(scrollTop < lastScroll ){ //ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’๊ณผ 1์ดˆ ์ „์˜ ์Šคํฌ๋กค ๊ฐ’
        document.querySelector("#parallax__nav").classList.add("show");
    }else {
        document.querySelector("#parallax__nav").classList.remove("show");
    }

    lastScroll = scrollTop; // 1์ดˆ ์ „์˜ ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’์ด ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค๊ฐ’์ด ๋˜์–ด 0.3์ดˆ๋งˆ๋‹ค ์Šคํฌ๋กค๊ฐ’์„ ์ธ์‹ํ•˜์—ฌ ๊ฐ’์ด ๋ณ€ํ™”ํ•จ.
}

window.addEventListener("scroll", scrollProgress); //ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ์ถ”์ถœํ•œ๋‹ค.

โ—๏ธ ๊ทธ๋Ÿผ ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉฐ ํ•จ๊ป˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์•Œ์•„๊ฐ€๋ด…์‹œ๋‹ค ๐Ÿฅน

// 01. top๋ฒ„ํŠผ ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค ๊ฐ’์—๋งŒ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
//๋ฌธ์„œ์˜ ์Šคํฌ๋กค ์ „์ฒด๊ฐ’ <= ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’ + ๋ธŒ๋ผ์šฐ์ € ๋‚ด์—์„œ ๋ณด์ด๋Š” ์Šคํฌ๋กค์˜ ๋†’์ด ๊ฐ’
window.addEventListener("scroll", ()=> {
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    if(document.documentElement.scrollHeight <= scrollTop + window.innerHeight){
        document.querySelector("#parallax__top").classList.add("show");
    }else {
        document.querySelector("#parallax__top").classList.remove("show");
    }
});

1. ๋‹ค์–‘ํ•œ ๋ธŒ๋ผ์šฐ์ € ํฌ๊ธฐ์—๋„ ํ•ญ์ƒ ๋ฒ„ํŠผ์ด ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค ๊ฐ’์— ๋ณด์ด๋„๋ก if๋ฌธ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

2. classList๋ฅผ ํ†ตํ•ด ๋งŒ์•ฝ true๋ผ๋ฉด show ์„ ํƒ์ž๋ฅผ ๋ถ™์ด๊ณ  ์•„๋‹ˆ๋ผ๋ฉด show๋ฅผ ์ง€์›๋‹ˆ๋‹ค.

///02. top๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์˜ฌ๋ผ์˜ค๊ฒŒ ํ•˜๊ธฐ
document.querySelector("#parallax__top").addEventListener("click", ()=> {
    window.scrollTo({left: 0, top: 0, behavior:"smooth"}); //scrollTo : ์ ˆ๋Œ“๊ฐ’ ๊ธฐ์ค€
});

1. addEventListener์—์„œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ๊ฒฝ์šฐ scrollTo๋ฅผ ํ†ตํ•ด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ตœ์ƒ๋‹จ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋„๋ก ์œ„์น˜์„ค์ •๊ณผ ํšจ๊ณผ๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

// 03.  ์Šคํฌ๋กค ๊ฐ’์„ ์›€์ง์ผ ๋•Œ ๋งˆ๋‹ค ์ž‘๋™๋˜๋Š” ์ด๋ฒคํŠธ ์„ค์ •ํ•˜๊ธฐ
// ํ˜„์žฌ ๊ฐ’์ด 3000์ธ ์Šคํฌ๋กค์„ ๋‚ด๋ ธ์„ ๋•Œ ์Šคํฌ๋กค ๊ฐ’์ด 3000๋ณด๋‹ค ๋†’์•„์ง€๋ฉด ๋ฉ”๋‰ด๊ฐ€ ๋ณด์ด์ง€ ์•Š์•„์•ผ ํ•˜๊ณ  ๋‹ค์‹œ ์Šคํฌ๋กค์„ ์˜ฌ๋ ค์„œ ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์งˆ ๋•Œ ๋‹ค์‹œ ๋ณด์—ฌ์•ผ ํ•œ๋‹ค.
// ํ˜„์žฌ ๊ฐ’์€ ๊ณ„์† ๋ณ€๋™ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์˜ ์ด ์Šคํฌ๋กค ๊ฐ’๋„ ์ฝ˜ํ…์ธ ๊ฐ€ ์ถ”๊ฐ€๋  ์ˆ˜๋ก ๊ณ„์† ๋ณ€ํ•œ๋‹ค. ์ฆ‰ ์ด๋Ÿฌํ•œ ๋ณ€ํ™”๋ฅผ ๊ณ ๋ คํ•˜์—ฌ 1์ดˆ ์ „์ด๋‚˜ 30์ดˆ ์ „ ์ฆ‰ ๊ณผ๊ฑฐ์˜ ์Šคํฌ๋กค๊ฐ’๊ณผ ํ˜„์žฌ์˜ ์Šคํฌ๋กค๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ์‹์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.
let nowScroll = true;   // ํŠธ๋ฆฌ๊ฑฐ ๋ณ€์ˆ˜ ์ด์šฉ //nowScroll๊ฐ€ true์ผ ๋•Œ ์‹คํ–‰
let lastScroll = 0;

function scrollProgress(){
    // info์— ํ˜„์žฌ ์Šคํฌ๋กค ์œ„์น˜ ๊ฐ’ ๊ตฌํ˜„ํ•˜๊ธฐ
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    document.querySelector('#parallax__info span').innerText = Math.ceil(scrollTop);

    nowScroll = true;

    // nowScroll = false์ผ ๋•Œ hasScroll()ํ•จ์ˆ˜๊ฐ€ 0.3์ดˆ ๋งˆ๋‹ค ์‹คํ–‰๋˜๋„๋ก ์„ค์ • : ๊ณ„์† ์‹คํ–‰ ์‹œ ๋ฒ„๋ฒ…๊ฑฐ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์Šคํฌ๋กค๊ฐ’์— ์•ฝ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ๋‘๊ธฐ ์œ„ํ•ด setInterval๋กœ ์‹œ๊ฐ„์ฐจ๋ฅผ ์ค€ ๊ฒƒ.
    setInterval(() => {
        if(nowScroll){
            nowScroll = false;
            hasScroll();
    }
    },300); 
}

function hasScroll(){
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
    //console.log("ํ˜„์žฌ ๊ฐ’",scrollTop, "์ด์ „ ๊ฐ’", lastScroll)

    if(scrollTop < lastScroll ){ //ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’๊ณผ 1์ดˆ ์ „์˜ ์Šคํฌ๋กค ๊ฐ’
        document.querySelector("#parallax__nav").classList.add("show");
    }else {
        document.querySelector("#parallax__nav").classList.remove("show");
    }

    lastScroll = scrollTop; // 1์ดˆ ์ „์˜ ํ˜„์žฌ ์Šคํฌ๋กค ๊ฐ’์ด ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค๊ฐ’์ด ๋˜์–ด 0.3์ดˆ๋งˆ๋‹ค ์Šคํฌ๋กค๊ฐ’์„ ์ธ์‹ํ•˜์—ฌ ๊ฐ’์ด ๋ณ€ํ™”ํ•จ.
}

window.addEventListener("scroll", scrollProgress); //ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ์ถ”์ถœํ•œ๋‹ค.

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css