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

Parallax Scrolling ํŒจ๋Ÿด๋ž™์Šค ์Šคํฌ๋กค๋ง 02 : ์‚ฌ์ด๋“œ dot ๋ฉ”๋‰ด active & ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™๋˜๋Š” ํšจ๊ณผ

by oranssy 2022. 9. 14.
728x90
๋ฐ˜์‘ํ˜•

โญ๏ธ Parallax Scrolling ํŒจ๋Ÿด๋ž™์Šค ์Šคํฌ๋กค๋ง 02 : ์‚ฌ์ด๋“œ dot ๋ฉ”๋‰ด active & ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™๋˜๋Š” ํšจ๊ณผ โญ๏ธ


01. Parallax Scrolling

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

02. ํŒจ๋Ÿด๋ž™์Šค ์Šคํฌ๋กค๋ง ๋งŒ๋“ค์–ด๋ณด๊ธฐ : ๋ชฉํ‘œ

๐ŸงŠ ์Šคํฌ๋กคํ•˜์—ฌ ํ™”๋ฉด์„ ์›€์ง์ด๋ฉด, ํ™”๋ฉด ์ƒ์˜ ์œ„์น˜์— ๋”ฐ๋ผ ๋ฉ”๋‰ด๋ฐ”์˜ active ํšจ๊ณผ๊ฐ€ ์ „ํ™˜๋˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
๐ŸงŠ ๋ฉ”๋‰ด๋ฐ”๋ฅผ ๋ˆŒ๋Ÿฌ ๊ฐ ์„น์…˜์˜ ๋‚ด์šฉ์ด ์žˆ๋Š” ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ฒŒ ๋  ๋•Œ, ํ™”๋ฉด์ด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™๋˜๋Š” ํšจ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

[1] ๋ฉ”๋‰ด๋ฐ” ๋งŒ๋“ค๊ธฐ (html)

1) ์‹œ๋ฉ˜ํ‹ฑ ๊ตฌ์กฐ๋ฅผ ์œ„ํ•ด nav ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— ์ž์‹ ํƒœ๊ทธ๋กœ ul ๊ณผ li ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ๋ฉ”๋‰ด๋ฐ” ๋ชฉ๋ก์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
2) ๋ฉ”๋‰ด๋ฐ”๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ์ด๋™์ด ๋˜๋„๋ก a ํƒœ๊ทธ๋กœ ๋งํฌ๋ฅผ ๊ฑธ์–ด์ฃผ๊ณ , class ๋กœ active ๋ฅผ '๋ฉ”๋‰ด1'์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
3) ์ดํ›„์— css ๋กœ ๋ฉ”๋‰ด$ ์˜ ๊ธ€์ž๋ฅผ ์—†์• ๊ธฐ ์œ„ํ•ด ๋ฉ”๋‰ด$ ๊ธ€์ž๋ฅผ span ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   a href ์— class ="ํด๋ž˜์Šค๊ฐ’" ๋˜๋Š” id ="์•„์ด๋””๊ฐ’" ์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋งํฌ ์ด๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

<nav id="parallax__dot">
    <ul>
        <li class="active"><a href="#section1"><span>๋ฉ”๋‰ด1</span></a></li>
        <li><a href="#section2"><span>๋ฉ”๋‰ด2</span></a></li>
        <li><a href="#section3"><span>๋ฉ”๋‰ด3</span></a></li>
        <li><a href="#section4"><span>๋ฉ”๋‰ด4</span></a></li>
        <li><a href="#section5"><span>๋ฉ”๋‰ด5</span></a></li>
        <li><a href="#section6"><span>๋ฉ”๋‰ด6</span></a></li>
        <li><a href="#section7"><span>๋ฉ”๋‰ด7</span></a></li>
        <li><a href="#section8"><span>๋ฉ”๋‰ด8</span></a></li>
        <li><a href="#section9"><span>๋ฉ”๋‰ด9</span></a></li>
    </ul>
</nav>

[2] ๋ฉ”๋‰ด๋ฐ” ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

#parallax__dot {
    position: fixed;    /* ์˜ค๋ฅธ์ชฝ ์ค‘๊ฐ„์— ๊ณ ์ •ํ•ด์„œ ๋„์›Œ๋†“๊ธฐ */
    right: 20px;        /* ๊ณ ์ •ํ•ด ๋‘˜ ์œ„์น˜ : ์˜ค๋ฅธ์ชฝ ๊ฐ„๊ฒฉ ์„ค์ • */
    top: 50%;           /* top ์„ 30% ์ฃผ๊ณ  ์•„๋ž˜์˜ transform ์„ ์ง€์›Œ๋„ ๊ฒฐ๊ณผ๊ฐ’์€ ๊ฐ™์Œ */
    transform: translateY(-50%);    /* ๋ฉ”๋‰ด๋ฐ”๊ฐ€ Y์ถ•(์œ„์•„๋ž˜)์˜ ์ค‘๊ฐ„์— ์œ„์น˜ํ•˜๋„๋ก */
    z-index: 10000;     /* ๋ฉ”๋‰ด๋ฐ”๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ž‘๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๋ ˆ์ด์•„์›ƒ์˜ ๋งจ ์œ—์ชฝ(ํ‘œ๋ฉด)์— ์œ„์น˜์‹œํ‚ด */
    background: rgba(0,0,0,0.4);    /* rgba : ํˆฌ๋ช…์ƒ‰ ์ง€์ • */
    padding: 20px 10px;     /* ์•ˆ์ชฝ ์—ฌ๋ฐฑ : ์ƒํ•˜ ์ขŒ์šฐ */
    border-radius: 30px;    /* ํ…Œ๋‘๋ฆฌ ๊ณก๋ฅ  */
}

#parallax__dot li {
    position: relative;     /* parallax__dot li a ์˜ position ์— ๋Œ€ํ•œ ๊ธฐ์ค€์  ์„ค์ • (์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์„ธ๋กœ๋กœ ๊ธด ํƒ€์›๋งŒ ๋ณด์ž„) */
    width: 20px;            /* parallax__dot li ๊ฐ€ ์ฐจ์ง€ํ•  ์˜์—ญ์˜ ๋„ˆ๋น„ ์„ค์ • */
    height: 20px;           /* parallax__dot li ๊ฐ€ ์ฐจ์ง€ํ•  ์˜์—ญ์˜ ๋†’์ด ์„ค์ • */
    border-radius: 50%;     /* ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ๊ฐ™์„ ๋•Œ ํ…Œ๋‘๋ฆฌ ๊ณก๋ฅ ์ด 50% ์ด๋ฉด ์› ๋ชจ์–‘์ด ๋งŒ๋“ค์–ด์ง */
    margin: 12px 8px;       /* ๋ฐ”๊นฅ ์—ฌ๋ฐฑ : ์ƒํ•˜ ์ขŒ์šฐ */
    box-shadow: 0 0 0 2px rgba(225,225,225,0);      /* ํˆฌ๋ช…๋„๊ฐ€ 0์œผ๋กœ ๋ณด์—ฌ์ง€์ง€ ์•Š๊ฒŒ ์„ค์ •,  ๋ฐ•์Šค ๊ทธ๋ฆผ์ž ํšจ๊ณผ : ๊ฐ€๋กœpx, ์„ธ๋กœpx, ๋ฒˆ์ง ์ •๋„, ํ™•์žฅ์ถ•์†Œ, ์ƒ‰๊น” | inset, initial, inherit */
    transition: box-shadow .2s ease;    /* box-shadow์— ์˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ 0.2์ดˆ์˜ ์‹œ๊ฐ„ ๋™์•ˆ ์„œ์„œํžˆ ๋ณ€ํ™”์‹œํ‚ด */
}

#parallax__dot li a {
    background: #fff;
    width: 100%;
    height: 100%;
    position: absolute;     /* ์œ„์น˜ ์„ค์ •; ๋ถ€๋ชจ ์˜์—ญ์„ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •๋˜์–ด์•ผ ํ•จ -> relative ์„ค์ • */
    top: 0;
    left: 0;
    border-radius: 50%;
    transition: transform .2s ease;     /* parallax__dot li.active a ์˜ transform์— ์˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ 0.2์ดˆ์˜ ์‹œ๊ฐ„ ๋™์•ˆ ์„œ์„œํžˆ ๋ณ€ํ™”์‹œํ‚ด */
}

#parallax__dot li.active {
    box-shadow: 0 0 0 2px rgba(225,225,225,1);      /* active ํšจ๊ณผ๊ฐ€ ์ง€์ •๋œ ๋ฉ”๋‰ด๋Š” ๊ทธ๋ฆผ์ž์˜ ํˆฌ๋ช…๋„๋ฅผ 1๋กœ ์„ค์ •ํ•˜์—ฌ ๋ณด์—ฌ์คŒ */
}

#parallax__dot li.active a {
    transform: scale(0.4);      /* transform : ์š”์†Œ์— ํšŒ์ „, ํฌ๊ธฐ ์กฐ์ ˆ, ๊ธฐ์šธ์ด๊ธฐ, ์ด๋™ ํšจ๊ณผ ๋“ฑ์„ ๋ถ€์—ฌ */
}

#parallax__dot li span {
    display: none;          /* parallax__dot li span ์˜ ๊ธ€์ž(๋ฉ”๋‰ด ๊ธ€์ž)๋ฅผ ๋ณด์ด์ง€ ์•Š๊ฒŒ ์„ค์ • */
}

[3] ๋‚ด์šฉ ๊ตฌ์กฐ ์งœ๊ธฐ (html)

1) main ํƒœ๊ทธ > div (contents) > section > [ span (๋ฉ”๋‰ด ๋ฒˆํ˜ธ) | h2 (๋ฉ”๋‰ด ์ด๋ฆ„) | { figure > div (์ด๋ฏธ์ง€) } | p (๋‚ด์šฉ ๊ธ€) ] * 9 (๋ฉ”๋‰ด ๊ฐฏ์ˆ˜)
2) css ์™€ script ์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ์œ„ํ•ด, id ์™€ class ๋กœ ์ด๋ฆ„์„ ์ •ํ•ฉ๋‹ˆ๋‹ค.

<main id="parallax__cont">
    <div id="contents">
        <section id="section1" class="contents__item">
            <span class="contents__item__num">01</span>
            <h2 class="contents__item_title">section1</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 1</p>
        </section>
        <!-- //section1 -->

        <section id="section2" class="contents__item">
            <span class="contents__item__num">02</span>
            <h2 class="contents__item_title">section2</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 2</p>
        </section>
        <!-- //section2 -->

        <section id="section3" class="contents__item">
            <span class="contents__item__num">03</span>
            <h2 class="contents__item_title">section3</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 3</p>
        </section>
        <!-- //section3 -->

        <section id="section4" class="contents__item">
            <span class="contents__item__num">04</span>
            <h2 class="contents__item_title">section4</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 4</p>
        </section>
        <!-- //section4 -->

        <section id="section5" class="contents__item">
            <span class="contents__item__num">05</span>
            <h2 class="contents__item_title">section5</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 5</p>
        </section>
        <!-- //section5 -->

        <section id="section6" class="contents__item">
            <span class="contents__item__num">06</span>
            <h2 class="contents__item_title">section6</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 6</p>
        </section>
        <!-- //section6 -->

        <section id="section7" class="contents__item">
            <span class="contents__item__num">07</span>
            <h2 class="contents__item_title">section7</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 7</p>
        </section>
        <!-- //section7 -->

        <section id="section8" class="contents__item">
            <span class="contents__item__num">08</span>
            <h2 class="contents__item_title">section8</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 8</p>
        </section>
        <!-- //section8 -->

        <section id="section9" class="contents__item">
            <span class="contents__item__num">09</span>
            <h2 class="contents__item_title">section9</h2>
            <figure class="contents__item__imgWrap">
                <div class="contents__item__img"></div>
            </figure>
            <p class="contents__item__desc">๋‚ด์šฉ ๊ธ€ 9</p>
        </section>
        <!-- //section9 -->
    </div>
</main>
<!-- //main -->

[4] ๋‚ด์šฉ ๊ตฌ์กฐ ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

.contents__item {
    width: 1000px;          /* section ์ด ์ฐจ์ง€ํ•  ๋„ˆ๋น„ ์„ค์ • */
    max-width: 70vw;        /* vw : ๋ทฐํฌํŠธ์˜ ๋น„์œจ๋กœ */
    margin: 30vw auto;
    /* background-color: rgba(255, 255, 255, 0.1);   ์ž„์‹œ๋กœ ์ค€ ๊ฐ’, css ์„ค์ •ํ•˜๋ฉฐ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด ํ™•์ธ ํ›„ ์ง€์›๋‹ˆ๋‹ค. */
    text-align: left;       /* ํ…์ŠคํŠธ ์™ผ์ชฝ ์ •๋ ฌ */
    margin-right: 0;
    position: relative;     /* contents__item__num ์˜ position ์— ๋Œ€ํ•œ ๊ธฐ์ค€์  ์„ค์ • (์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๊ธฐ์ค€์ ์ธ ๋ธŒ๋ผ์šฐ์ € ์™ผ์ชฝ ๋งจ ์œ„์— ๋ชจ๋‘ ๊ฒน์ณ์ง) */
    padding-top: 9vw;
}

.contents__item:nth-child(even) {       /* : ์˜ ์ž์‹ child( ~ ๋ฒˆ์งธ ) -> 2n : 2์˜ ๋ฐฐ์ˆ˜, 2n+1 : 3์˜ ๋ฐฐ์ˆ˜, even : ์ง์ˆ˜, odd : ํ™€์ˆ˜ */
    margin-left: 0;
    text-align: right;     /* ํ…์ŠคํŠธ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ */
}

.contents__item__num {
    font-size: 35vw;        /* ๊ธ€์ž ํฌ๊ธฐ */
    font-family: 'Lato';    /* ๊ธ€์ž์ฒด */
    font-weight: 100;       /* ๊ธ€์ž ๋‘๊ป˜ */
    position: absolute;     /* ์ˆซ์ž๊ฐ€ ์ด๋ฏธ์ง€์™€ ๊ฒน์น˜๋„๋ก ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด์คŒ; ๋ถ€๋ชจ ์˜์—ญ์„ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •๋˜์–ด์•ผ ํ•จ -> relative ์„ค์ • */
    left: -6vw;             /* ์œ„์น˜ ์„ค์ • : ์™ผ์ชฝ ๊ฐ„๊ฒฉ ์„ค์ • */
    top: -13vw;             /* ์œ„์น˜ ์„ค์ • : ์œ—์ชฝ ๊ฐ„๊ฒฉ ์„ค์ • */
    opacity: 0.07;          /* ํˆฌ๋ช…๋„ */
    z-index: -2;            /* ๋ ˆ์ด์•„์›ƒ์—์„œ ๊ฒน์ณ์ง„ ์ˆซ์ž๊ฐ€ ์ด๋ฏธ์ง€ ์•„๋ž˜(๋’ค)์— ์œ„์น˜ํ•˜๋„๋ก */
}

.contents__item:nth-child(even) .contents__item__num {
    left: auto;     /* left ๊ฐ’ ์ดˆ๊ธฐํ™”; ๊ธฐ์ค€์ ์ด right ๋กœ ๋ฐ”๋€œ */
    right: -8vw;    /* ์œ„์น˜ ์„ค์ • : ์˜ค๋ฅธ์ชฝ ๊ฐ„๊ฒฉ ์„ค์ • */
}

.contents__item_title {
    font-weight: 400;
    text-transform: capitalize;     /* ๋‹จ์–ด์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์คŒ; section -> Section */
}

.contents__item__imgWrap {
    width: 100%;
    padding-bottom: 56.25%;     /* ๊ฐ€์ƒ์œผ๋กœ ์ด๋ฏธ์ง€ ์˜์—ญ์„ ๋งŒ๋“ค์–ด์คŒ -> ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Œ */
    background: #000;
    position: relative;     /* contents__item__img ์˜ position ์— ๋Œ€ํ•œ ๊ธฐ์ค€์  ์„ค์ • */
    overflow: hidden;       /* ์ด๋ฏธ์ง€๊ฐ€ ์˜์—ญ ๋ฐ–์œผ๋กœ ๋„˜์ณ ๋‚˜๊ฐ€๋Š” ๊ฒƒ์„ ๋ง‰์•„์คŒ */
    z-index: -1;
}

.contents__item__img {
    background-repeat: no-repeat;           /* ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋œ ์ด๋ฏธ์ง€ ๋ฐ˜๋ณต ์ฑ„์›€์„ ํ•ด์ œ */
    background-position: center center;     /* ์ด๋ฏธ์ง€๊ฐ€ ์ • ๊ฐ€์šด๋ฐ์— ์œ„์น˜ํ•˜๋„๋ก ์ง€์ • ex) left top, right center, center bottom ๋“ฑ */
    background-size: cover;                 /* ์š”์†Œ์˜ ํฌ๊ธฐ์— ๋งž์ถฐ์„œ ์ด๋ฏธ์ง€๊ฐ€ ๋ฎ์ด๋„๋ก ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์„ค์ • */
    width: 110%;
    height: 110%;
    position: absolute;     /* ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด์คŒ; ๋ถ€๋ชจ ์˜์—ญ์„ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •๋˜์–ด์•ผ ํ•จ -> relative ์„ค์ • */
    left: -5%;
    top: -5%;
    filter: saturate(0%);   /* ์ด๋ฏธ์ง€ ํ•„ํ„ฐ : ํ‘๋ฐฑ (0% ํ‘๋ฐฑ, 50% ํ‘๋ฐฑ ๋ฐ˜ ์ƒ‰๊น” ๋ฐ˜) */
    transition: all 1s;
}

/* ๊ฐ ์„น์…˜์— ์ด๋ฏธ์ง€๋กœ ๋ฐฐ๊ฒฝ ๋„ฃ๊ธฐ */
.contents__item:nth-child(1) .contents__item__img {     /* contents__item ์˜ ์ฒซ๋ฒˆ์งธ ์ž์‹ contents__item__img ์—๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด๋ผ */
    background-image: url(../assets/img/parallax01.jpg);
}

.contents__item:nth-child(2) .contents__item__img {
    background-image: url(../assets/img/parallax02.jpg);
}

.contents__item:nth-child(3) .contents__item__img {
    background-image: url(../assets/img/parallax03.jpg);
}

.contents__item:nth-child(4) .contents__item__img {
    background-image: url(../assets/img/parallax04.jpg);
}

.contents__item:nth-child(5) .contents__item__img {
    background-image: url(../assets/img/parallax05.jpg);
}

.contents__item:nth-child(6) .contents__item__img {
    background-image: url(../assets/img/parallax06.jpg);
}

.contents__item:nth-child(7) .contents__item__img {
    background-image: url(../assets/img/parallax07.jpg);
}

.contents__item:nth-child(8) .contents__item__img {
    background-image: url(../assets/img/parallax08.jpg);
}

.contents__item:nth-child(9) .contents__item__img {
    background-image: url(../assets/img/parallax09.jpg);
}

.contents__item__desc {
    font-size: 2vw;
    font-weight: 600;
    color: 	#FFFFF0;
    text-shadow: 1px 1px 1px #222;      /* ํ…์ŠคํŠธ ๊ทธ๋ฆผ์ž ํšจ๊ณผ : ๊ฐ€๋กœpx, ์„ธ๋กœpx, ๋ฒˆ์ง ์ •๋„, ์ƒ‰๊น” -> ์ด์™€ ๊ฐ™์€ ์†์„ฑ ๊ฐ’์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ ์ž…๋ ฅํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ทธ๋ฆผ์ž๊ฐ€ ์ƒ๊น€*/
    line-height: 1.4;
    margin-top: -2vw;
    margin-left: -5vw;
    word-break: keep-all;       /* ์–ด์ ˆ ๋‹จ์œ„๋กœ ๋Š์–ด์ง€๋ฉฐ ์ค„๋ฐ”๊ฟˆ์ด ๋˜๋„๋ก ์„ค์ • */
}

.contents__item:nth-child(even) .contents__item__desc {
    margin-left: auto;      /* ์™ผ์ชฝ ๋ฐ”๊นฅ ์—ฌ๋ฐฑ ์ดˆ๊ธฐํ™”; ๊ธฐ์ค€์ ์ด ์˜ค๋ฅธ์ชฝ ๋ฐ”๊นฅ ์—ฌ๋ฐฑ์œผ๋กœ ๋ฐ”๋€œ */
    margin-right: -4vw;
}

[5] scrollTop ํ™•์ธ์นธ ๋งŒ๋“ค๊ธฐ (html)

1) ์‹œ๋ฉ˜ํ‹ฑ ๊ตฌ์กฐ๋ฅผ ์œ„ํ•ด aside ํƒœ๊ทธ > div ํƒœ๊ทธ > span ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
2) css ์™€ script ์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ์œ„ํ•ด, id ์™€ class ๋กœ ์ด๋ฆ„์„ ์ •ํ•ฉ๋‹ˆ๋‹ค.

<aside id="parallax__info">
    <div>scrollTop: <span>0</span>px</div>
</aside>

[6] scrollTop ํ™•์ธ์นธ ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

#parallax__info {
    position: fixed;        /* ์™ผ์ชฝ ์•„๋ž˜์— ๊ณ ์ •์‹œํ‚ด */
    left: 20px;
    bottom: 20px;
    z-index: 2000;          /* ๊ฐ€๋ ค์ง€์ง€ ์•Š๋„๋ก ๋งจ ์œ—์ชฝ(ํ‘œ๋ฉด)์— ์œ„์น˜์‹œํ‚ด */
    background: rgba(0, 0, 0, 0.4);
    color: #fff;
    padding: 20px;
    border-radius: 10px;
}

#parallax__info .scroll {
    line-height: 1.4;           /* parallax__info .scroll ์˜ ๋‚ด์šฉ ๊ธ€์ž์— ๋Œ€ํ•œ ์ค„๊ฐ„๊ฒฉ ์„ค์ • */
}

[7] ๋ณ€ํ•˜๋Š” ๊ฐ’ ๊ณ„์‚ฐํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ (script)

1) ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๊ฐ€์ ธ์™€์•ผ ํ• ์ง€ ์ƒ๊ฐํ•ด๋ณด๊ณ , ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ๊ตฌ์ƒํ•ด๋ด…๋‹ˆ๋‹ค.
2) ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•, ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ณด๊ณ  ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋กœ ์ถœ๋ ฅํ•ด๋ด…์‹œ๋‹ค.

// ๋ฉ”๋‰ด ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ, ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™ํ•˜๋Š” ํšจ๊ณผ ์ฃผ๊ธฐ

// ๊ณ„์‚ฐ ์กฐ๊ฑด๊ณผ ์‹ ์ž…๋ ฅ
document.querySelectorAll("#parallax__dot a").forEach( el => {      // ๋ชจ๋“  parallax__dot a ์— ๋Œ€ํ•ด์„œ,
    el.addEventListener("click", e => {     // ๋ฉ”๋‰ด์˜ ๋งํฌ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ,
        e.preventDefault();                 // ๋งํฌ ์ด๋™ ๋˜๋Š” ์ƒˆ๋กœ๊ณ ์นจ ๋ง‰์•„์ฃผ๊ธฐ
        
        // ๋ฌธ์„œ์—์„œ ์š”์†Œ์˜ ์†์„ฑ href ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ. ์Šคํฌ๋กค ์ด๋™์„ ์„ค์ •ํ•จ (๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์Šคํฌ๋กค๋˜์–ด ๋„˜์–ด๊ฐ€๋„๋ก)
        document.querySelector(el.getAttribute("href")).scrollIntoView({behavior: "smooth"});
        
        // ์ฐธ๊ณ ํ•˜๊ธฐ : ๋ธŒ๋ผ์šฐ์ € ์Šคํฌ๋กค ๋ฉ”์„œ๋“œ
        // window.scroll(), window.scrollTo(), window.scrollBy() ๋ชจ๋‘ ์•ž์˜ window๋ฅผ ์ƒ๋žต ๊ฐ€๋Šฅ
        
        // window.scroll(0, 1000);                  // ๋ธŒ๋ผ์šฐ์ €์˜ (x ์ขŒํ‘œ 0, y ์ขŒํ‘œ 1000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scroll({left: 0, top: 1000});     // ๋ธŒ๋ผ์šฐ์ €์˜ (left = x ์ขŒํ‘œ 0, top = y ์ขŒํ‘œ 1000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scroll({left: 0, top: 1000, behavior: "smooth"});
        // ๋ธŒ๋ผ์šฐ์ €์˜ (left = x ์ขŒํ‘œ 0, top = y์ขŒํ‘œ 1000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ค๊ณ , ๋™์ž‘์€ "smooth" ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์Šคํฌ๋กค๋˜์–ด ๋„˜์–ด๊ฐ€๋„๋ก ์„ค์ •
        
        // scrollTo() : (์ ˆ๋Œ€ ์ขŒํ‘œ) ์™ผ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ „์ฒด ํฌ๊ธฐ์˜ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ์ง€์ •๋œ ์ขŒํ‘œ๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollTo(0, 1000);                // ๋ธŒ๋ผ์šฐ์ €์˜ (x ์ขŒํ‘œ 0, y ์ขŒํ‘œ 2000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollTo({left: 0, top: 2000});   // ๋ธŒ๋ผ์šฐ์ €์˜ (left = x ์ขŒํ‘œ 0, top = y ์ขŒํ‘œ 2000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollTo({left: 0, top: 2000, behavior: "smooth"});
        // ๋ธŒ๋ผ์šฐ์ €์˜ (left = x ์ขŒํ‘œ 0, top = y์ขŒํ‘œ 2000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ค๊ณ , ๋™์ž‘์€ "smooth" ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์Šคํฌ๋กค๋˜์–ด ๋„˜์–ด๊ฐ€๋„๋ก ์„ค์ •
        // scrollTo()์˜ ๊ฐ’์€ scroll()์˜ ๊ฐ’๊ณผ ๊ฐ™์œผ๋‚˜, scrollTo()๊ฐ€ ํ˜ธํ™˜์„ฑ์ด ๋” ์ข‹์Œ
        
        // scrollBy() : (์ƒ๋Œ€ ์ขŒํ‘œ) ํ˜„์žฌ ์Šคํฌ๋กค์ด ์žˆ๋Š” ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ์ง€์ •๋œ ์ขŒํ‘œ๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollBy(0, 1000);                // ์Šคํฌ๋กค์ด ์žˆ๋Š” ์ขŒํ‘œ์—์„œ (x ์ขŒํ‘œ 0, y ์ขŒํ‘œ 3000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollBy({left: 0, top: 3000});   // ์Šคํฌ๋กค์ด ์žˆ๋Š” ์ขŒํ‘œ์—์„œ (left = x ์ขŒํ‘œ 0, top = y ์ขŒํ‘œ 3000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ด
        // window.scrollBy({left: 0, top: 3000, behavior: "smooth"});
        /// ๋ธŒ๋ผ์šฐ์ €์˜ (left = x ์ขŒํ‘œ 0, top = y์ขŒํ‘œ 3000) ์œ„์น˜๋กœ ์Šคํฌ๋กค์„ ์ด๋™์‹œํ‚ค๊ณ , ๋™์ž‘์€ "smooth" ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์Šคํฌ๋กค๋˜์–ด ๋„˜์–ด๊ฐ€๋„๋ก ์„ค์ •
    });
});
        
        
        
// ์Šคํฌ๋กค ๋‚ด๋ฆด ๋•Œ, ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” section$ ์˜ Y์ถ• ์œ„์น˜์— ๋”ฐ๋ผ ๋ฉ”๋‰ด์˜ ์ ์— active ํšจ๊ณผ ์ฃผ๊ธฐ
        
window.addEventListener("scroll", () => {           // ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์Šคํฌ๋กค ํ–ˆ์„ ๋•Œ,
    // let scrollTop = window.pageYOffset;                      // 3๊ฐ€์ง€ ์‹์€
    // let scrollTop = window.scrollY;                          // ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค.
    // let scrollTop = document.documentElement.scrollTop;      // ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ฉ์ณ์„œ ํ‘œํ˜„ํ•จ
        
    // scrollTop ๋ณ€์ˆ˜ ์•ˆ์— ๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€์˜ ์„ธ๋กœ ์Šคํฌ๋กค ๊ฐ’์„ ์ €์žฅํ•˜์‹œ์˜ค.  |๋˜๋Š”|
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
        
    // ๊ทธ๋ฆฌ๊ณ  scrollTop ๊ฐ’์„ parallax__info span ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค.
    document.querySelector("#parallax__info span").innerText = Math.floor(scrollTop);       // ์†Œ์ˆ˜์ ; ๋‚ด๋ฆผ Math.floor( ), ์˜ฌ๋ฆผ Math.ceil( ), ๋ฐ˜์˜ฌ๋ฆผ Math.round( )
    // script ๋งจ ์œ„์ชฝ์—   const scroll = document.querySelector("#parallax__info span");    ์ด๋ ‡๊ฒŒ scroll ์ƒ์ˆ˜ ์„ค์ • ํ›„,
    // scroll.innerText = Math.round(scrollTop);    ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•ด๋„ ์ถœ๋ ฅ๋จ
        
        
    // .contents__item(section ์˜์—ญ)์—์„œ์˜ ์š”์†Œ๊ฐ’, ์œ„์น˜๊ฐ’ ์ค‘์—์„œ
    document.querySelectorAll(".contents__item").forEach((e, i) => {
        
        // scrollTop ์˜ ๊ฐ’์ด ์š”์†Œ๊ฐ’์˜ Y์ถ• ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด,
        if(scrollTop >= e.offsetTop -10){       // ์˜ค์ฐจ๊ฐ€ ์ƒ๊ธฐ๋ฏ€๋กœ ์˜ค์ฐจ์— ์˜ํ•œ ์˜ค๋ฅ˜๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ( - ์•„๋ฌด ์ˆซ์ž๋“  ์ƒ๊ด€ ์—†์Œ)
        
        document.querySelectorAll("#parallax__dot li").forEach(li => {      // ๋ชจ๋“  parallax__dot li ์— ๋Œ€ํ•˜์—ฌ,
            li.classList.remove("active");                                  // li ์˜ ํด๋ž˜์Šค์— ์žˆ๋Š” active๋ฃฐ ์ œ๊ฑฐํ•˜๊ณ 
        });
        
            // parallax__dot li ์˜ (index +1)๋ฒˆ์งธ li ์˜ ํด๋ž˜์Šค์— active๋ฃฐ ์ถ”๊ฐ€ํ•˜์‹œ์˜ค.
            document.querySelector("#parallax__dot li:nth-child(" + ( i + 1 ) + ")").classList.add("active");
        };                                              // index ๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ +1 ํ•ด์คŒ (๊ทธ๋ž˜์•ผ ๋งˆ์ง€๋ง‰ ์˜์—ญ๊นŒ์ง€ ํฌํ•จ๋จ)
    });
});

03. ๊ฒฐ๊ณผ๋ณด๊ธฐ

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€