โญ๏ธ 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 ํด์ค (๊ทธ๋์ผ ๋ง์ง๋ง ์์ญ๊น์ง ํฌํจ๋จ)
});
});
๋๊ธ