
โญ๏ธ Parallax Scrolling ํจ๋ด๋์ค ์คํฌ๋กค๋ง 04 : ์๋จ ์จ๊น ๋ฉ๋ด active & ํ๋จ top ์จ๊น ๋ฒํผ & ๋ถ๋๋ฌ์ด ์ด๋ & ์์ ๊ฐ๋ณ์ ์ผ๋ก ๋ํ๋ด๊ธฐ โญ๏ธ
01. Parallax Scrolling
ํจ๋ด๋์ค(Parallax)๋ '์์ฐจ'๋ฅผ ๋ปํ๋ ๋จ์ด๋ก, ๋ฉ๋ฆฌ ์๋ ๋ฌผ์ฒด๋ ์ฒ์ฒํ ์์ง์ด๊ณ ๊ฐ๊น์ด ์๋ ๋ฌผ์ฒด๋ ๋น ๋ฅด๊ฒ ์์ง์ด๋ ํ์์ ์๋ฏธํฉ๋๋ค.
ํจ๋ด๋์ค ์คํฌ๋กค๋ง (Parallax Scrolling) ๊ธฐ์ ์ ๋ฐฐ๊ฒฝ ๋๋ ์ค๋ธ์ ํธ์ ๋ ์ด์ดํ, ์คํฌ๋กค๊ณผ ์ ๋๋ฉ์ด์
์ ์๋ ์กฐ์ ์ ํตํด ๊น์ด์ ๋ํ ์ฐฉ์๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ธฐ์ ์
๋๋ค.
์ด ๊ธฐ์ ์ ํ ํ์ด์ง๋ก ๊ตฌ์ฑํ ์ ์์ผ๋ฉฐ, ์๊ฐ์ ์ธ ๋ชฐ์
๊ฐ์ ์ฃผ๊ณ ํจ๊ณผ์ ์ธ ์คํ ๋ฆฌํ
๋ง์ ๊ฐ๋ฅํ๊ฒ ํ๋ ์ฅ์ ์ด ์์ต๋๋ค.
๋ค๋ง ๋ด์ฉ์ด ๋๋ฌด ๊ธธ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ์๊ฐ ์ง๋ฃจํจ์ด๋ ๋ถํธํจ์ ๋๋ ์ ์๊ณ , ํ๋ฉด ๊ตฌ์ฑ์ด ๋ณต์กํด์ง๋ฉฐ ๋ง์ ๋ฆฌ์์ค๋ก ์ธํด ๋ก๋ฉ ์๋๊ฐ ๋๋ฆด ์ ์์ต๋๋ค.
02. ํจ๋ด๋์ค ์คํฌ๋กค๋ง ๋ง๋ค์ด๋ณด๊ธฐ : ๋ชฉํ
๐ง ์๋ก ์คํฌ๋กคํ๋ฉด, ๋ฉ๋ด๋ฐ๊ฐ ์จ๊ฒจ์ ธ ์๋ค๊ฐ ๋์ค๋๋ก ์ค์ ํฉ๋๋ค.
๐ง ๋ธ๋ผ์ฐ์ ์ ๋งจ ์๋์ ๋๋ฌํ๋ฉด, top ๋ฒํผ์ด ์จ๊ฒจ์ ธ ์๋ค๊ฐ ๋์ค๋๋ก ์ค์ ํฉ๋๋ค.
๐ง ๋ฉ๋ด๋ฐ์ top ๋ฒํผ์ ๋๋ฌ ์ด๋ํ๊ฒ ๋ ๋, ํ๋ฉด์ด ๋ถ๋๋ฝ๊ฒ ์ด๋๋๋ ํจ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
๐ง section ์ ์์๋ค(์ซ์, ์ด๋ฏธ์ง, ๊ธ)์ด ์คํฌ๋กคํ ๋ ์์ํ ๋ํ๋๋ ํจ๊ณผ๋ฅผ ํํํฉ๋๋ค.
[1] ๋ฉ๋ด๋ฐ ๋ง๋ค๊ธฐ (html)
1) ์๋ฉํฑ ๊ตฌ์กฐ๋ฅผ ์ํด nav ํ๊ทธ๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์์ ์์ ํ๊ทธ๋ก ul ๊ณผ li ํ๊ทธ๋ฅผ ์ด์ฉํด ๋ฉ๋ด๋ฐ ๋ชฉ๋ก์ ์์ฑํฉ๋๋ค.
2) ๋ฉ๋ด๋ฐ๋ฅผ ๋๋ ์ ๋ ์ด๋์ด ๋๋๋ก a ํ๊ทธ๋ก ๋งํฌ๋ฅผ ๊ฑธ์ด์ฃผ๊ณ , class ๋ก active ๋ฅผ '๋ฉ๋ด1'์ ์
๋ ฅํฉ๋๋ค.
3) ๋์ค ์์
์ด์ง๋ง, script ์ ๊ณ์ฐ์ ๋ฐ๋ผ ๋งจ ์์ชฝ์์ ๋ฉ๋ด๊ฐ ์ฌ๋ผ์ ธ ๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ class ="show"๋ฅผ ๋ถ์ฌ์ค๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
`1 a href ์ class ="ํด๋์ค๊ฐ" ๋๋ id ="์์ด๋๊ฐ" ์ ๋ฃ์ด์ฃผ๋ฉด ๋งํฌ ์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค.
<nav id="parallax__nav" class="show"> <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>
[2] ๋ฉ๋ด๋ฐ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
#parallax__nav { position: fixed; /* ์ค๋ฅธ์ชฝ ์์ ๊ณ ์ ํด์ ๋์๋๊ธฐ์ํด */ right: 20px; /* ๊ณ ์ ํด ๋ ์์น : ์ค๋ฅธ์ชฝ ๊ฐ๊ฒฉ ์ค์ */ top: -200px; /* parallax__nav ์ show ๊ฐ ์ ๋ถ์์ ๋๋ ์จ๊ฒจ์ฃผ๊ธฐ ์ํด - ๊ฐ์ ์ค */ z-index: 2000; /* ๋ฉ๋ด๋ฐ๋ฅผ ๋๋ฌ์ ์๋ํด์ผ ํ๋ฏ๋ก, ๋ ์ด์์์ ๋งจ ์์ชฝ(ํ๋ฉด)์ ์์น์ํด */ background-color: rgba(0, 0, 0, 0.4); /* rgba : ํฌ๋ช
์ ์ง์ */ padding: 20px 30px; /* ์์ชฝ ์ฌ๋ฐฑ : ์ํ ์ข์ฐ */ border-radius: 50px; /* ํ
๋๋ฆฌ ๊ณก๋ฅ */ transition: top .4s ease; /* parallax__nav ์ show ๊ฐ ๋ถ์์ ๋ top์ ๋ณํ์ ์ํ ๊ฒฐ๊ณผ๋ฅผ 0.4์ด์ ์๊ฐ ๋์ ์์ํ ๋ณด์ฌ์ค */ } #parallax__nav.show { top: 20px; /* parallax__nav ์ show ๊ฐ ๋ถ์์ ๋ ๋์ค๊ฒ ํ๊ธฐ ์ํจ */ } #parallax__nav li { display: inline; /* ์์๊ฐ ํ์๋๋ ์์ญ๋งํผ๋ง ๊ณต๊ฐ์ ๊ฐ๋๋ก ๋ฐฐ์น */ margin: 0 5px; /* ๋ฐ๊นฅ ์ฌ๋ฐฑ : ์ํ ์ข์ฐ */ } #parallax__nav li a { display: inline-block; /* ์์๊ฐ ํ์๋๋ ์์ญ๋งํผ๋ง ๊ณต๊ฐ์ ์ฐจ์งํ๋ฉด์ ํฌ๊ธฐ์ ์ฌ๋ฐฑ ์ค์ ์ด ๊ฐ๋ฅํ๋๋ก ๋ฐฐ์น */ /* width: 30px; ์์๋ก ์ค ๊ฐ, css ์ค์ ํ๋ฉฐ ๋ธ๋ผ์ฐ์ ํ๋ฉด ํ์ธ ํ ์ง์๋๋ค. */ height: 30px; padding: 5px 20px; text-align: center; /* ํ
์คํธ ์ค์ ์ ๋ ฌ */ line-height: 30px; /* ์ค๊ฐ๊ฒฉ : ๊ธฐ์ค ๋์ด */ } #parallax__nav li.active a { background: #fff; /* background ์ต์
์ ์ง์ ํด์ ์ฌ์ฉํ ๋ (color, image, repeat, attachment, position ๋ฑ) ์ฌ๊ธฐ์๋ ๋ฐฐ๊ฒฝ์ */ color: #000; /* ๊ธ์์ */ border-radius: 20px; box-sizing: content-box; /* content-box; ๋๋น๋ก ์ง์ ๋ ๊ฐ์ content ๋๋น๋ก ์ ์ฉ์ํด border-box; ๋๋น๋ก ์ง์ ํ ๊ฐ์ ์ค์ ๋๋น๋ก ์ ์ฉ์ํด */ }
[3] top ๋ฒํผ ๋ง๋ค๊ธฐ (html)
div ํ๊ทธ๋ฅผ ๋ง๋ค๊ณ , css ์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด id๋ก ์ด๋ฆ์ ์ ํฉ๋๋ค.
<div id="parallax__top">top</div> <!-- //parallax__top -->
[4] top ๋ฒํผ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
#parallax__top { position: fixed; /* ์๋์ ๋ณด์ด์ง ์๊ฒ ๊ณ ์ ์์ผ ๋๊ณ script ๊ณผ์ ์ผ๋ก ๋์ฐ๊ณ ์ํจ */ left: 50%; right: 20px; bottom: -220px; /* parallax__top ์ show ๊ฐ ์ ๋ถ์์ ๋๋ ์จ๊ฒจ์ฃผ๊ธฐ ์ํด - ๊ฐ์ ์ค */ transform: translateX(-50%); /* top ๋ฒํผ์ด X์ถ(๊ฐ๋ก)์ ์ค๊ฐ์ ์์นํ๋๋ก */ z-index: 10000; /* top ๋ฒํผ์ ๋๋ฌ์ ์๋ํด์ผ ํ๋ฏ๋ก, ๋ ์ด์์์ ๋งจ ์์ชฝ(ํ๋ฉด)์ ์์น์ํด */ width: 40px; /* parallax__top ๊ฐ ์ฐจ์งํ ์์ญ์ ๋๋น ์ค์ */ height: 40px; /* parallax__top ๊ฐ ์ฐจ์งํ ์์ญ์ ๋์ด ์ค์ */ background: rgba(0,0,0,0.4); /* rgba : ํฌ๋ช
์ ์ง์ */ text-align: center; /* ํ
์คํธ ์ค์ ์ ๋ ฌ */ line-height: 40px; /* ์ค๊ฐ๊ฒฉ : ๊ธฐ์ค ๋์ด */ color: #fff; /* ๊ธ์์ */ border-radius: 50%; /* ๋๋น์ ๋์ด๊ฐ ๊ฐ์ ๋ ํ
๋๋ฆฌ ๊ณก๋ฅ ์ด 50% ์ด๋ฉด ์ ๋ชจ์์ด ๋ง๋ค์ด์ง */ padding: 10px; /* ์์ชฝ ์ฌ๋ฐฑ */ cursor: pointer; /* ์ปค์ ๋ชจ์ ์ง์ */ transition: all 0.3s; } #parallax__top.show { bottom: 20px; /* parallax__top ์ show ๊ฐ ๋ถ์์ ๋ ๋์ค๊ฒ ํ๊ธฐ ์ํจ */ } #parallax__top:hover { /* ๋ง์ฐ์ค ์ค๋ฒ ํจ๊ณผ */ background: #fff; color: #000; }
[5] ๋ด์ฉ ๊ตฌ์กฐ ์ง๊ธฐ (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 -->
[6] ๋ด์ฉ ๊ตฌ์กฐ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
#parallax__cont { max-width: 1600px; /* ์ต๋ ๋๋น ์ค์ */ width: 98%; margin: 0 auto; /* background-color: rgba(255, 255, 255, 0.1); ์์๋ก ์ค ๊ฐ, 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; }
[7] parallax__info ํ์ธ์นธ ๋ง๋ค๊ธฐ (html)
1) ์๋ฉํฑ ๊ตฌ์กฐ๋ฅผ ์ํด aside ํ๊ทธ > div ํ๊ทธ > span ํ๊ทธ๋ฅผ ์์ฑํฉ๋๋ค.
2) css ์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด, id ์ class ๋ก ์ด๋ฆ์ ์ ํฉ๋๋ค.
<aside id="parallax__info"> <div class="scroll">scrollTop: <span>0</span>px</div> <div class="info"> <ul> <li>section$ ๋์ด๊ฐ - ( browser ๋์ด๊ฐ /3 )</li> <li>#s1 - ( b/3 ) : <span class="offset1">0</span>px</li> <li>#s2 - ( b/3 ) : <span class="offset2">0</span>px</li> <li>#s3 - ( b/3 ) : <span class="offset3">0</span>px</li> <li>#s4 - ( b/3 ) : <span class="offset4">0</span>px</li> <li>#s5 - ( b/3 ) : <span class="offset5">0</span>px</li> <li>#s6 - ( b/3 ) : <span class="offset6">0</span>px</li> <li>#s7 - ( b/3 ) : <span class="offset7">0</span>px</li> <li>#s8 - ( b/3 ) : <span class="offset8">0</span>px</li> <li>#s9 - ( b/3 ) : <span class="offset9">0</span>px</li> </ul> </div> </aside> <!-- //parallax__info -->
[8] ์์น ํ์ธ์นธ ๋์์ธํ๊ธฐ (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 li, .scroll { line-height: 1.4; /* parallax__info li ์ .scroll ์ ๋ด์ฉ ๊ธ์์ ๋ํ ์ค๊ฐ๊ฒฉ ์ค์ */ }
[9] ์์๋ค์ด ๋ํ๋๋ ํจ๊ณผ ์ค์ ํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
/* ์์๊ฐ ํ๊บผ๋ฒ์ ๋ํ๋๋๋ก */ /* #contents > section { opacity: 0; transition: all 1s; } #contents > section.show { opacity: 1; } */ /* ์์๊ฐ ๊ฐ๋ณ์ ์ผ๋ก ์๊ฐ์ฐจ๋ฅผ ๋๋ฉด์ ๋ํ๋๋๋ก */ #contents > section .contents__item__num { /* ๋ฐฐ๊ฒฝ ์ซ์ */ opacity: 0; transform: translateY(300px); /* Y์ถ(์๋์ชฝ) ์ด๋(300px) */ transition: all 1s 0.1s ease-in-out; /* all: ๋ชจ๋ ์์ฑ์ ์ ์ฉ, ์ด ์๊ฐ: 1์ด, ์์ ์๊ฐ ์ฐ๊ธฐ: 0.1์ด, ์งํ์๋: ease-in-out = cubic-bezier */ } #contents > section .contents__item_title { opacity: 0; transform: translateX(-100px); /* X์ถ(- ์ด๋ฏ๋ก ์ผ์ชฝ์ผ๋ก) ์ด๋(100px) */ transition: all 1s 0.3s ease-in-out; } #contents > section .contents__item__imgWrap { opacity: 0; transform: translateY(200px) rotate(10deg) skew(5deg); /* Y์ถ(์๋์ชฝ) ์ด๋(200px), ํ์ (10๋), ๋นํ๊ธฐ(5๋) */ transition: all 1s 0.6s ease-in-out; } #contents > section .contents__item__desc { opacity: 0; transform: translateX(-200px); transition: all 1s 0.9s ease-in-out; } /* contents ์ section ์ show ๊ฐ ๋ถ์ผ๋ฉด contents__item__num ๋ */ #contents > section.show .contents__item__num { opacity: 0.07; transform: translateY(0); } #contents > section.show .contents__item_title { opacity: 1; transform: translateX(0); } #contents > section.show .contents__item__imgWrap { opacity: 1; transform: translateY(0) rotate(0) skew(0); } #contents > section.show .contents__item__desc { opacity: 1; transform: translateX(0); } /* contents ์ section ์ค์ ์ง์์ธ ์์์์์ contents__item_title ์ */ #contents > section:nth-child(even).contents__item_title { transform: translateX(100px); } #contents > section:nth-child(even).show .contents__item_title { transform: translateX(0); } #contents > section:nth-child(even).contents__item__desc { transform: translateX(200px); } #contents > section:nth-child(even).show .contents__item__desc { transform: translateX(0); }
# ์๋์ ์๋ฆฌ ์๊ฐํ๊ธฐ
scrollTop ์ ๊ฐ์ด ๊ฐ section ์ Y์ถ ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋, ๋ฉ๋ด์ active ๊ฐ ๋ค์ section ์ผ๋ก ์ฎ๊ฒจ์ ธ์ผ ํฉ๋๋ค.

[10] ๋ณํ๋ ๊ฐ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํ๊ธฐ (script)
1) ๊ฐ์ ์ด๋ป๊ฒ ๊ฐ์ ธ์์ผ ํ ์ง ์๊ฐํด๋ณด๊ณ , ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๊ตฌ์ํด๋ด
๋๋ค.
2) ๊ฐ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ, ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๋ค์ํ๊ฒ ์๊ฐํด๋ณด๊ณ ์ฌ๋ฌ ๊ฐ์ง๋ก ์ถ๋ ฅํด๋ด
์๋ค.
// ๋ฉ๋ด ๋ฒํผ์ ๋๋ ์ ๋, ๋ถ๋๋ฝ๊ฒ ์ด๋ํ๋ ํจ๊ณผ ์ฃผ๊ธฐ // ๊ณ์ฐ ์กฐ๊ฑด๊ณผ ์ ์
๋ ฅ document.querySelectorAll("#parallax__nav li a").forEach( el => { // ๋ชจ๋ parallax__nav li a ์ ๋ํด์, el.addEventListener("click", e => { // ๋ฉ๋ด์ ๋งํฌ ๋ฒํผ์ ํด๋ฆญํ์ ๋, e.preventDefault(); // ๋งํฌ ์ด๋ ๋๋ ์๋ก๊ณ ์นจ ๋ง์์ฃผ๊ธฐ // ๋ฌธ์์์ ์์์ ์์ฑ href ๋ฅผ ๋ถ๋ฌ์ฌ ๋. ์คํฌ๋กค ์ด๋์ ์ค์ ํจ (๋ถ๋๋ฝ๊ฒ ์คํฌ๋กค๋์ด ๋์ด๊ฐ๋๋ก) document.querySelector(el.getAttribute("href")).scrollIntoView({behavior: "smooth"}); }); }); // parallax__info ์ ๋ค์ด๊ฐ๋ ๊ฐ ์ถ๋ ฅํ๊ธฐ // ๊ณ์ฐ ์กฐ๊ฑด๊ณผ ์ ์
๋ ฅ const browser = window.innerHeight; 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 .scroll span ์์ ์ถ๋ ฅํ์์ค. document.querySelector("#parallax__info .scroll span").innerText = Math.round(scrollTop); // ์์์ ; ๋ด๋ฆผ Math.floor( ), ์ฌ๋ฆผ Math.ceil( ), ๋ฐ์ฌ๋ฆผ Math.round( ) // script ๋งจ ์์ชฝ์ const scroll = document.querySelector("#parallax__info .scroll span"); ์ด๋ ๊ฒ scroll ์์ ์ค์ ํ, // scroll.innerText = Math.round(scrollTop); ์ด๋ ๊ฒ ์
๋ ฅํด๋ ์ถ๋ ฅ๋จ // offset ๊ฐ ์ถ๋ ฅํ๊ธฐ : for๋ฌธ์ผ๋ก ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ // i ๊ฐ 1๋ถํฐ 9๊น์ง ์ฆ๊ฐํ ๋, for (let i=1; i<=9; i++) { document.querySelector(".offset" + i).innerText = document.getElementById("section" + i).offsetTop - Math.round(browser/3); } // ์์ (offset + i) ์์ ์ถ๋ ฅํ์์ค. = ์์ด๋ (section + i) ์์์ Y์ถ ๊ฐ(๋ฌธ์ ๊ธฐ์ค)์์ ๋ธ๋ผ์ฐ์ ๋์ด๊ฐ/3 ์ ๋บ ๊ฐ์ // ์จ๊ฒจ์ ธ ์๋ ๋ฉ๋ด๋ฐ ๋ํ๋ด๊ธฐ 01. scrollTop ๊ฐ์ด 0๋ณด๋ค ํฐ ๊ฒฝ์ฐ์ ์๋์ํค๊ธฐ // if(scrollTop > 0){ // scrollTop ๊ฐ์ด 0๋ณด๋ค ํฌ๋ฉด, // document.querySelector("#parallax__nav").classList.add("show"); // parallax__nav ์ ํด๋์ค์ show ๋ฅผ ์ถ๊ฐํ๊ณ // } else { // ๊ทธ๋ ์ง ์์ผ๋ฉด // document.querySelector("#parallax__nav").classList.remove("show"); // parallax__nav ์ ํด๋์ค์ show ๋ฅผ ์ ๊ฑฐํ์์ค. // } // ์จ๊ฒจ์ ธ ์๋ top ๋ฒํผ ๋ํ๋ด๊ธฐ : scrollTop ๊ฐ์ด (๋ฌธ์ ์ ์ฒด ๋์ด - ํ์ฌ ๋ณด์ฌ์ง๋ ๋ธ๋ผ์ฐ์ ๋์ด) ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ์ ์๋์ํค๊ธฐ // scrollTop ๊ฐ์ด (๋ฌธ์ ์ ์ฒด ๋์ด - ํ์ฌ ๋ณด์ฌ์ง๋ ๋ธ๋ผ์ฐ์ ๋์ด) ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ ๊ฒ์ ์คํฌ๋กค์ด ๋ฌธ์์ ๋์ ๋๋ฌํ๋ค๋ ์๋ฏธ if(scrollTop >= document.body.scrollHeight - window.innerHeight){ // scrollTop ๊ฐ์ด (๋ฌธ์ ์ ์ฒด ๋์ด - ํ์ฌ ๋ณด์ฌ์ง๋ ๋ธ๋ผ์ฐ์ ๋์ด) ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋, document.querySelector("#parallax__top").classList.add("show"); // parallax__top ์ ํด๋์ค์ show ๋ฅผ ์ถ๊ฐํ๊ณ } else { // ๊ทธ๋ ์ง ์์ผ๋ฉด document.querySelector("#parallax__top").classList.remove("show"); // parallax__top ์ ํด๋์ค์ show ๋ฅผ ์ ๊ฑฐํ์์ค. } }); // top ๋ฒํผ ๋๋ฅด๋ฉด ์คํฌ๋กค์ด ๋งจ ์๋ก ์ด๋ํ๋๋ก ์๋์ํค๊ธฐ document.querySelector("#parallax__top").addEventListener("click", () => { // top ๋ฒํผ์ ํด๋ฆญํ์ ๋, window.scrollTo({left: 0, top: 0, behavior: "smooth"}); }); // ๋ธ๋ผ์ฐ์ ์ (left = x ์ขํ 0, top = y์ขํ 0) ์์น๋ก ์คํฌ๋กค์ ์ด๋์ํค๊ณ , ๋์์ "smooth" ๋ถ๋๋ฝ๊ฒ ์คํฌ๋กค๋์ด ๋์ด๊ฐ๋๋ก ์ค์ // scrollTo() : (์ ๋ ์ขํ) ์ผ์ชฝ ์๋จ ๋ชจ์๋ฆฌ๋ถํฐ ์์ํ๋ ์ ์ฒด ํฌ๊ธฐ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ๊ธฐ์ค์ผ๋ก, ์ง์ ๋ ์ขํ๋ก ์คํฌ๋กค์ ์ด๋์ํด // ์จ๊ฒจ์ ธ ์๋ ๋ฉ๋ด๋ฐ ๋ํ๋ด๊ธฐ 02. (ํ์ฌ์ ์คํฌ๋กค ๊ฐ - ๊ณผ๊ฑฐ์ ์คํฌ๋กค ๊ฐ) ์ ์ด์ฉํ์ฌ, ์คํฌ๋กค์ ์๋ก ์ฌ๋ฆฌ๋ ๊ฒฝ์ฐ์ ์๋์ํค๊ธฐ // ํ์ฌ์ ์คํฌ๋กค ๊ฐ์ ์คํํ๋ ๋ณ์ let nowScroll = true; // ์คํ O // let nowScroll = false; // ์คํ X // ํธ๋ฆฌ๊ฑฐ ๋ณ์ : ํธ๋ฆฌ๊ฑฐ์ ๋ฐ๋ ์กฐ๊ฑด์ ์ ์ํ๋ ์ฉ๋๋ก ์ฌ์ฉ๋จ. ํ์ฌ ๋ฐ ์ด์ ์คํ์ ๋ํ ์ ๋ณด์ ์ ๊ทผํจ // trigger ๋ ํ๊ทธ๊ฐ ์ธ์ ์คํ๋๋๊ฐ๋ฅผ ์ ํ๋ ํ์ด๋ฐ์ ์๋ฏธํจ (ํ๊ทธ๋ฅผ ์๋์ํค๋ ์๊ฐ์ ๊ฒฐ์ ) let lastScroll = 0; // ๊ณผ๊ฑฐ์ ์คํฌ๋กค ๊ฐ์ ๋ณ์๋ก ์ง์ function scrollProgress(){ // scrollProgress ํจ์๋ nowScroll = true; // ์คํฌ๋กค์ด ์์ง์ผ ๋, setInterval(() => { // setInterval ํจ์๊ฐ ์คํ๋๋๋ก ํจ (setInterval : ์ผ์ ํ ์๊ฐ ๊ฐ๊ฒฉ์ ๋๊ณ ์์
์ ๋ฐ๋ณต ์คํํ ๋ ์ฌ์ฉ) if(nowScroll){ // ํ์ฌ์ ์คํฌ๋กค ๊ฐ์ด ~์ด๋ฉด nowScroll = false; // ์คํฌ๋กค์ด ์์ง์์ ๋๋ง ํจ์๋ฅผ ์คํ์ํค๊ธฐ ์ํจ hasScroll(); } }, 300); // 0.3์ด ๊ฐ๊ฒฉ (์คํฌ๋กคํ ๋๋ง๋ค ์ธ์ํ๋ฉด ๋ฒ๋ฒ
๊ฑฐ๋ฆฌ๋ฏ๋ก, ์ผ์ ํ ๊ฐ๊ฒฉ์ ๋๊ณ ์คํ๋ ์ ์๋๋ก) }; function hasScroll(){ // hasScroll ํจ์๋ let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop; // scrollTop ๋ณ์ ์์ ๋ธ๋ผ์ฐ์ ๊ธฐ์ค์ ์ธ๋ก ์คํฌ๋กค ๊ฐ์ด ์์ ๋, // scrollTop ๊ฐ์ด lastScroll ๊ฐ๋ณด๋ค ์๋ค๋ฉด, ( = ์คํฌ๋กค์ด ์๋ก ์ฌ๋ผ๊ฐ ๋, ) if(scrollTop < lastScroll){ document.querySelector("#parallax__nav").classList.add("show"); // parallax__nav ์ ํด๋์ค์ show ๋ฅผ ์ถ๊ฐํ๊ณ } else { // ๊ทธ๋ ์ง ์์ผ๋ฉด document.querySelector("#parallax__nav").classList.remove("show"); // parallax__nav ์ ํด๋์ค์ show ๋ฅผ ์ ๊ฑฐํ๋๋ก ํจ } // ์ด์ ์คํฌ๋กค ๊ฐ = ํ์ฌ ์คํฌ๋กค ๊ฐ; lastScroll = scrollTop; }; window.addEventListener("scroll", scrollProgress); // ์คํฌ๋กค์ด ๋์ ๋, scrollProgress ํจ์๋ฅผ ์คํํ์์ค. // ์ฌ๊ทํจ์: ์๊ธฐ ์์ ์ ๋ค์ ํธ์ถํ์ฌ ์์
์ ์ํํ๋ ํจ์ // function scroll(){ // alert("ok"); // scroll(); // } // scroll(); ์คํํ๋ฉด ๋ฌดํ ๊ตด๋ ์ ๋น ์ง function scroll(){ // scroll ํจ์๋ let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop; // scrollTop ๋ณ์ ์์ ๋ธ๋ผ์ฐ์ ๊ธฐ์ค์ ์ธ๋ก ์คํฌ๋กค ๊ฐ์ด ์์ ๋, // .contents__item(section ์์ญ) ์ค์์ document.querySelectorAll(".contents__item").forEach(item => { // scrollTop ๊ฐ์ด { ์น์
์ Y์ถ ๊ฐ(๋ฌธ์ ๊ธฐ์ค) - ๋ธ๋ผ์ฐ์ ์ ๋์ด๊ฐ /3 } ๋ณด๋ค ํด ๋ if(scrollTop > item.offsetTop - Math.round(browser/3)){ /* ์คํฌ๋กค์ด ๊ฐ ์น์
์์ญ์ ๋๋ฌํ์ ๋ ์ข ๋ ์์ฐ์ค๋ฝ๊ฒ ๋์ค๊ฒ ํ๊ธฐ ์ํด ๋ธ๋ผ์ฐ์ ์ ๋์ด๊ฐ/3 ๋ฅผ ๋นผ์ค */ item.classList.add("show"); /* item ์ ํด๋์ค์ show ๋ฅผ ์ถ๊ฐํ์์ค. */ } }); requestAnimationFrame(scroll); /* ์ ๋๋ฉ์ด์
์ ๊ตฌํํ๊ธฐ ์ํด ์ต์ ํ๋ ํจ์: ์์ง์์ด ๋ถ๋๋ฝ๊ณ ๋ฉ๋ชจ๋ฆฌ๋ ๋ ์ฐจ์งํจ, 1์ด์ 60๋ฒ ์คํ์ํด */ } scroll();
๋๊ธ