โญ๏ธ Parallax Scrolling ํจ๋ด๋์ค ์คํฌ๋กค๋ง 01 : ์๋จ ๋ฉ๋ด active & ๋ถ๋๋ฝ๊ฒ ์ด๋๋๋ ํจ๊ณผ โญ๏ธ
01. Parallax Scrolling
ํจ๋ด๋์ค(Parallax)๋ '์์ฐจ'๋ฅผ ๋ปํ๋ ๋จ์ด๋ก, ๋ฉ๋ฆฌ ์๋ ๋ฌผ์ฒด๋ ์ฒ์ฒํ ์์ง์ด๊ณ ๊ฐ๊น์ด ์๋ ๋ฌผ์ฒด๋ ๋น ๋ฅด๊ฒ ์์ง์ด๋ ํ์์ ์๋ฏธํฉ๋๋ค.
ํจ๋ด๋์ค ์คํฌ๋กค๋ง (Parallax Scrolling) ๊ธฐ์ ์ ๋ฐฐ๊ฒฝ ๋๋ ์ค๋ธ์ ํธ์ ๋ ์ด์ดํ, ์คํฌ๋กค๊ณผ ์ ๋๋ฉ์ด์
์ ์๋ ์กฐ์ ์ ํตํด ๊น์ด์ ๋ํ ์ฐฉ์๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ธฐ์ ์
๋๋ค.
์ด ๊ธฐ์ ์ ํ ํ์ด์ง๋ก ๊ตฌ์ฑํ ์ ์์ผ๋ฉฐ, ์๊ฐ์ ์ธ ๋ชฐ์
๊ฐ์ ์ฃผ๊ณ ํจ๊ณผ์ ์ธ ์คํ ๋ฆฌํ
๋ง์ ๊ฐ๋ฅํ๊ฒ ํ๋ ์ฅ์ ์ด ์์ต๋๋ค.
๋ค๋ง ๋ด์ฉ์ด ๋๋ฌด ๊ธธ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ์๊ฐ ์ง๋ฃจํจ์ด๋ ๋ถํธํจ์ ๋๋ ์ ์๊ณ , ํ๋ฉด ๊ตฌ์ฑ์ด ๋ณต์กํด์ง๋ฉฐ ๋ง์ ๋ฆฌ์์ค๋ก ์ธํด ๋ก๋ฉ ์๋๊ฐ ๋๋ฆด ์ ์์ต๋๋ค.
02. ํจ๋ด๋์ค ์คํฌ๋กค๋ง ๋ง๋ค์ด๋ณด๊ธฐ : ๋ชฉํ
๐ง ์คํฌ๋กคํ์ฌ ํ๋ฉด์ ์์ง์ด๋ฉด, ํ๋ฉด ์์ ์์น์ ๋ฐ๋ผ ๋ฉ๋ด๋ฐ์ active ํจ๊ณผ๊ฐ ์ ํ๋๋๋ก ์ค์ ํฉ๋๋ค.
๐ง ๋ฉ๋ด๋ฐ๋ฅผ ๋๋ฌ ๊ฐ ์น์
์ ๋ด์ฉ์ด ์๋ ์์น๋ก ์ด๋ํ๊ฒ ๋ ๋, ํ๋ฉด์ด ๋ถ๋๋ฝ๊ฒ ์ด๋๋๋ ํจ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
[1] ๋ฉ๋ด๋ฐ ๋ง๋ค๊ธฐ (html)
1) ์๋ฉํฑ ๊ตฌ์กฐ๋ฅผ ์ํด nav ํ๊ทธ๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์์ ์์ ํ๊ทธ๋ก ul ๊ณผ li ํ๊ทธ๋ฅผ ์ด์ฉํด ๋ฉ๋ด๋ฐ ๋ชฉ๋ก์ ์์ฑํฉ๋๋ค.
2) ๋ฉ๋ด๋ฐ๋ฅผ ๋๋ ์ ๋ ์ด๋์ด ๋๋๋ก a ํ๊ทธ๋ก ๋งํฌ๋ฅผ ๊ฑธ์ด์ฃผ๊ณ , class ๋ก active ๋ฅผ '๋ฉ๋ด1'์ ์
๋ ฅํฉ๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
`1 a href ์ class ="ํด๋์ค๊ฐ" ๋๋ id ="์์ด๋๊ฐ" ์ ๋ฃ์ด์ฃผ๋ฉด ๋งํฌ ์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค.
<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>
[2] ๋ฉ๋ด๋ฐ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
#parallax__nav {
position: fixed; /* ์ค๋ฅธ์ชฝ ์์ ๊ณ ์ ํด์ ๋์๋๊ธฐ์ํด */
right: 20px; /* ๊ณ ์ ํด ๋ ์์น : ์ค๋ฅธ์ชฝ ๊ฐ๊ฒฉ ์ค์ */
top: 20px; /* ๊ณ ์ ํด ๋ ์์น : ์์ชฝ ๊ฐ๊ฒฉ ์ค์ */
z-index: 2000; /* ๋ฉ๋ด๋ฐ๋ฅผ ๋๋ฌ์ ์๋ํด์ผ ํ๋ฏ๋ก, ๋ ์ด์์์ ๋งจ ์์ชฝ(ํ๋ฉด)์ ์์น์ํด */
background-color: rgba(0, 0, 0, 0.4); /* rgba : ํฌ๋ช
์ ์ง์ */
padding: 20px 30px; /* ์์ชฝ ์ฌ๋ฐฑ : ์ํ ์ข์ฐ */
border-radius: 50px; /* ํ
๋๋ฆฌ ๊ณก๋ฅ */
}
#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] ๋ด์ฉ ๊ตฌ์กฐ ์ง๊ธฐ (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] ์์น๊ฐ ํ์ธ์นธ ๋ง๋ค๊ธฐ (html)
1) ์๋ฉํฑ ๊ตฌ์กฐ๋ฅผ ์ํด aside ํ๊ทธ๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์์ ์์ ํ๊ทธ๋ก div ๋ฐ ul ๊ณผ li ํ๊ทธ๋ฅผ ์ด์ฉํด ์์น๊ฐ ๋ชฉ๋ก์ ์์ฑํฉ๋๋ค.
2) css ์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด, id ์ class ๋ก ์ด๋ฆ์ ์ ํฉ๋๋ค.
<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>
[6] ์์น ํ์ธ์นธ ๋์์ธํ๊ธฐ (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 ์ ๋ด์ฉ ๊ธ์์ ๋ํ ์ค๊ฐ๊ฒฉ ์ค์ */
}
# ์๋์ ์๋ฆฌ ์๊ฐํ๊ธฐ
scrollTop ์ ๊ฐ์ด ๊ฐ section ์ Y์ถ ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋, ๋ฉ๋ด์ active ๊ฐ ๋ค์ section ์ผ๋ก ์ฎ๊ฒจ์ ธ์ผ ํฉ๋๋ค.

[7] ๋ณํ๋ ๊ฐ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํ๊ธฐ (script)
1) ๊ฐ์ ์ด๋ป๊ฒ ๊ฐ์ ธ์์ผ ํ ์ง ์๊ฐํด๋ณด๊ณ , ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๊ตฌ์ํด๋ด
๋๋ค.
2) ๊ฐ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ, ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๋ค์ํ๊ฒ ์๊ฐํด๋ณด๊ณ ์ฌ๋ฌ ๊ฐ์ง๋ก ์ถ๋ ฅํด๋ด
์๋ค.
// parallax__info ์ ๋ค์ด๊ฐ๋ ๊ฐ ์ถ๋ ฅํ๊ธฐ
// ๊ณ์ฐ ์กฐ๊ฑด๊ณผ ์ ์
๋ ฅ
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 ๊ฐ ์ถ๋ ฅํ๊ธฐ 01. ์ผ์ผ์ด ํ๋์ฉ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
// ์์ offset$ ์์ ์ถ๋ ฅํ์์ค = ์์ด๋(section$)์์์ Y์ถ ๊ฐ(๋ฌธ์ ๊ธฐ์ค)์
// 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;
// offset ๊ฐ ์ถ๋ ฅํ๊ธฐ 02. for๋ฌธ์ผ๋ก ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
// i ๊ฐ 1๋ถํฐ 9๊น์ง ์ฆ๊ฐํ ๋,
for (let i=1; i<=9; i++) {
document.querySelector(".offset" + i).innerText = document.getElementById("section" + i).offsetTop;
} // ์์ (offset + i) ์์ ์ถ๋ ฅํ์์ค. = ์์ด๋ (section + i) ์์์ Y์ถ ๊ฐ(๋ฌธ์ ๊ธฐ์ค)์
// ๋ฉ๋ด ๋ฒํผ์ ๋๋ ์ ๋, ๋ถ๋๋ฝ๊ฒ ์ด๋ํ๋ ํจ๊ณผ ์ฃผ๊ธฐ
document.querySelectorAll("#parallax__nav li a").forEach( li => { // ๋ชจ๋ parallax__nav li a ์ ๋ํด์,
li.addEventListener("click", (e) => { // ๋ฉ๋ด์ ๋งํฌ ๋ฒํผ์ ํด๋ฆญํ์ ๋,
e.preventDefault(); // ๋งํฌ ์ด๋ ๋๋ ์๋ก๊ณ ์นจ ๋ง์์ฃผ๊ธฐ
document.querySelector(li.getAttribute("href")).scrollIntoView({ // li์ ์์ฑ href ๋ฅผ ๋ถ๋ฌ์ฌ ๋. ์คํฌ๋กค ์ด๋์ ์ค์ ํจ
behavior: "smooth" // ๋ถ๋๋ฝ๊ฒ ์คํฌ๋กค๋์ด ๋์ด๊ฐ๋๋ก (ex. ๋ฉ๋ด1์์ ๋ฉ๋ด5๋ฅผ ๋๋ฅด๋ฉด, section1 ~ section5 ์์๋ฅผ ๊ฑฐ์ณ์ ์ด๋ํจ)
});
});
});
// ์คํฌ๋กค ๋ด๋ฆด ๋, ํ๋ฉด์ ๋ณด์ฌ์ง๋ section$ ์ Y์ถ ์์น์ ๋ฐ๋ผ ๋ฉ๋ด$ ์ active ํจ๊ณผ ์ฃผ๊ธฐ
// active ํจ๊ณผ ์ถ๋ ฅํ๊ธฐ 01. ์ผ์ผ์ด ํ๋์ฉ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
// if (๋ง์ฝ ~ ์ด๋ฉด)
// if(scrollTop >= document.getElementById("section1").offsetTop){ // scrollTop ์ ๊ฐ์ด section1 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(1)").classList.add("active"); // parallax__nav ์ฒซ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section2").offsetTop){ // scrollTop ์ ๊ฐ์ด section2 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(2)").classList.add("active"); // parallax__nav ๋๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section3").offsetTop){ // scrollTop ์ ๊ฐ์ด section3 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(3)").classList.add("active"); // parallax__nav ์ธ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section4").offsetTop){ // scrollTop ์ ๊ฐ์ด section4 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(4)").classList.add("active"); // parallax__nav ๋ค๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section5").offsetTop){ // scrollTop ์ ๊ฐ์ด section5 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(5)").classList.add("active"); // parallax__nav ๋ค์ฏ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section6").offsetTop){ // scrollTop ์ ๊ฐ์ด section6 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(6)").classList.add("active"); // parallax__nav ์ฌ์ฏ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section7").offsetTop){ // scrollTop ์ ๊ฐ์ด section7 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(7)").classList.add("active"); // parallax__nav ์ผ๊ณฑ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section8").offsetTop){ // scrollTop ์ ๊ฐ์ด section8 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(8)").classList.add("active"); // parallax__nav ์ฌ๋๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// if(scrollTop >= document.getElementById("section9").offsetTop){ // scrollTop ์ ๊ฐ์ด section9 ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // parallax__nav li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(9)").classList.add("active"); // parallax__nav ์ํ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// active ํจ๊ณผ ์ถ๋ ฅํ๊ธฐ 02. for๋ฌธ์ผ๋ก ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ; for๋ฌธ ์์ ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ ๋ฃ์ด์ฃผ๊ธฐ
// i ๊ฐ 1๋ถํฐ 9๊น์ง ์ฆ๊ฐํ ๋,
// for( let i=1; i<=9; i++){
// if(scrollTop >= document.getElementById("section" +i).offsetTop){ // scrollTop ์ ๊ฐ์ด (section +i) ์์์ Y์ถ ๊ฐ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด,
// document.querySelectorAll("#parallax__nav li").forEach(li => { // ๋ชจ๋ parallax__nav li ์ ๋ํ์ฌ,
// li.classList.remove("active"); // li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
// });
// document.querySelector("#parallax__nav li:nth-child(" + i + ")").classList.add("active"); // parallax__nav li ์ i ๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
// };
// }
// active ํจ๊ณผ ์ถ๋ ฅํ๊ธฐ 03. forEach ์ผ๋ก ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
// .contents__item(section ์์ญ)์์์ ์์๊ฐ, ์์น๊ฐ ์ค์์
document.querySelectorAll(".contents__item").forEach((element, index) => {
// scrollTop ์ ๊ฐ์ด ์์๊ฐ์ Y์ถ ๊ฐ๋ณด๋ค ํฌ๋ค๋ฉด,
if(scrollTop >= element.offsetTop -2){ // ์ค์ฐจ๊ฐ ์๊ธฐ๋ฏ๋ก ์ค์ฐจ์ ์ํ ์ค๋ฅ๋ฅผ ๋ง๊ธฐ ์ํด์ ( - ์๋ฌด ์ซ์๋ ์๊ด ์์)
document.querySelectorAll("#parallax__nav li").forEach(li => { // ๋ชจ๋ parallax__nav li ์ ๋ํ์ฌ,
li.classList.remove("active"); // li ์ ํด๋์ค์ ์๋ active๋ฃฐ ์ ๊ฑฐํ๊ณ
});
// parallax__nav li ์ (index +1)๋ฒ์งธ li ์ ํด๋์ค์ active๋ฃฐ ์ถ๊ฐํ์์ค.
document.querySelector("#parallax__nav li:nth-child(" + ( index + 1 ) + ")").classList.add("active");
}; // index ๊ฐ 0๋ถํฐ ์์ํ๋ฏ๋ก +1 ํด์ค (๊ทธ๋์ผ ๋ง์ง๋ง ์์ญ๊น์ง ํฌํจ๋จ)
});
});
03. ๊ฒฐ๊ณผ๋ณด๊ธฐ
'Javascript > Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Parallax Scrolling ํจ๋ด๋์ค ์คํฌ๋กค๋ง 03 : ์๋จ ์จ๊น ๋ฉ๋ด active & ํ๋จ top ์จ๊น ๋ฒํผ & ๋ถ๋๋ฌ์ด ์ด๋ (2) | 2022.09.14 |
---|---|
Parallax Scrolling ํจ๋ด๋์ค ์คํฌ๋กค๋ง 02 : ์ฌ์ด๋ dot ๋ฉ๋ด active & ๋ถ๋๋ฝ๊ฒ ์ด๋๋๋ ํจ๊ณผ (2) | 2022.09.14 |
์ฌ๋ผ์ด๋ ์ดํํธ (3) (4) | 2022.09.02 |
์ฌ๋ผ์ด๋ ์ดํํธ (2) (3) | 2022.08.29 |
์ฌ๋ผ์ด๋ ์ดํํธ (1) (3) | 2022.08.29 |
๋๊ธ