๐ฑ ํ๋ด๋ ์ค ํจ๊ณผ : ๋ฉ๋ด ์คํฌ๋กค๋ง ํจ๊ณผ ๐ฑ
ํ๋ด๋์ค(parallax) ์์ฐจ๋ ์๋ฏธ๋ก ์ฌ์ฉ์๊ฐ ์คํฌ๋กค๋ง์ ํ ๋ ๋ฐฐ๊ฒฝ๊ณผ ์์๋ง๋ค ์๋ ๋ฐ ํฌ๊ธฐ์ ์ฐจ์ด๋ฅผ ์ฃผ์ด ์
์ฒด์ ์ผ๋ก ๋๊ปด์ง๊ฒ ํ๋ ํจ๊ณผ๋ฅผ ๋งํฉ๋๋ค.
์ด๋ฒ ์๊ฐ์๋ ๋ฒํผ์ ๋๋ ์ ๋(active) ํด๋น ํ๋ฉด์ผ๋ก ์คํฌ๋กค์ด ๋ถ๋๋ฝ๊ฒ ์ด๋ํ๋ ํจ๊ณผ์ ์คํฌ๋กค์ ๋ด๋ฆด ๋ ํด๋น ๋ฒํผ์ด ๊ฐ์กฐ๋๋ ํจ๊ณผ๋ฅผ ์ค์ตํด๋ณด๊ฒ ์ต๋๋ค. ๐
#1. HTML / CSS ์ค์ ํ๊ธฐ
! HTML TIP !
๊ฐ ์น์
๋ณ๋ก ๊ตฌ์กฐ๋ฅผ ์ค์ ํฉ๋๋ค. ์ด ๋ ํฐ ํ๋ถํฐ ๊ตฌ์กฐ๋ฅผ ์ค์ ํ๋ ๊ฒ์ด ์ค์ํ ํฌ์ธํธ ์
๋๋ค!
๊ตฌ์กฐ๋ ๋จผ์ ํฌ๊ฒ nav : ๋ฉ๋ด | main : ๋ฉ์ธ ์ฝํ
์ธ (์ด๋ฏธ์ง, ํ
์คํธ) | aside : ์คํฌ๋กค์ ์์น๊ฐ์ผ๋ก ๋๋์ด ์ค๋๋ค.
์ธ๋ถ์ ์ธ ๊ตฌ์กฐ๋ ์๋์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค!
โ๏ธ 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>
<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>
<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>
! CSS TIP !
1. ๋ฐฐ๊ฒฝ, ๋ฉ๋ด, ์ด๋ฏธ์ง์ ํ
์คํธ, ์คํฌ๋กค ์์น๊ฐ ๋ฐ์ค ๋ฑ ๊ฐ ์์ญ ๋ณ๋ก ์์ฑ์ ๋ถ์ฌํฉ๋๋ค.
์์ฑ์ ์ค์ ํ ๋ ๊ณตํต ์์ฑ์ ์ฃผ์ด์ผ ํ ๋ ๊ณตํต ์ ํ์๋ฅผ ๋ถ์ฌํ์ฌ ํ ๋ฒ์ ์์
ํ๋ ๊ฒ์ด ํจ์จ์ ์
๋๋ค!
2.
โ๏ธ CSS ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ : ํ๋ด๋ ์ค ํจ๊ณผ
/* parallax__nav */
#parallax__nav {
position: fixed;
right: 20px;
top: 20px;
z-index: 2000;
background-color: rgba(95, 95, 95, 0.4);
padding: 20px 30px;
border-radius: 50px;
}
#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__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;
}
}
โ๏ธ CSS ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ : ํ์ด์ง ์น์
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #222;
color: #fff;
font-weight: 300;
font-family: 'Pretendard';
}
img {
width: 100%;
vertical-align: top; /* ์ด๋ฏธ์ง ์ ๋ ฌ */
}
a {
color: #fff;
text-decoration: none;
}
li {
list-style: none;
}
/* scroll */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: #28293600;
}
::-webkit-scrollbar-thumb {
background: rgba(136, 136, 136, 0.500);
border-radius: 20px;
}
/* layout */
#header {
z-index: 2000; /* ์ ํธ ์ธ๋ฑ์ค๋ ์๋ก ํ์ ์ฌ์ผ ํ๋ฉฐ ๊ผญ ํฌ์ง์
์ ํจ๊ป ์ฌ์ฉํด์ฃผ์ด์ผ ํ๋. */
}
#main {
width: 100%;
height: 100vh;
position: relative;
z-index: 1000;
}
#footer {
position: relative;
z-index: 3000;
}
/* header */
#header {
position: absolute;
left: 0;
top: 0;
color: #fff;
padding: 20px;
}
#header h1 {
display: inline-block;
font-weight: 200;
margin-bottom: 10px;
border-bottom: 0.5px solid #fff;
}
#header p {
font-weight: 200;
font-size: 18px;
margin-bottom: 10px;
}
#header li {
display: inline-block;
}
#header li a {
border: 1px solid #fff;
border-radius: 50%;
display: inline-block;
width: 30px;
height: 30px;
text-align: center;
line-height: 30px;
font-size: 14px;
}
#header li.active a {
background-color: #fff;
color: #282936;
}
/* footer */
#footer .source {
position: absolute;
right: 20px;
bottom: 20px;
border: 1px solid #fff;
border-radius: 40px;
padding: 10px 30px;
font-size: 14px;
}
/* modal__wrap */
.modal__btn {
color: #fff;
border: 1px solid #fff;
border-radius: 50px;
display: inline-block;
padding: 10px 20px;
position: absolute;
right: 20px;
bottom: 20px;
cursor: pointer;
transition: background-color 0.3s, color 0.3s;
}
.modal__btn:hover {
background-color: #f5f5f5;
color: #555;
}
.modal__cont {
font-size: 14px;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.85);
position: fixed;
left: 0;
top: 0;
overflow-x: hidden; /* X์ถ์ผ๋ก๋ ์คํฌ๋กค ๋ฐ๊ฐ ์๊ธฐ์ง ์๊ฒ ํด์ค๋ค. */
display: flex;
align-items: center; /* ๊ฐ์ด๋ฐ ์ ๋ ฌ aic */
justify-content: center; /* ๊ฐ์ด๋ฐ ์ ๋ ฌ jcc */
transform: scale(0); /* ์์ ์๋ณด์ด๊ฒ ํ๊ธฐ */
}
.modal__box {
width: 70%;
height: 70vh;
border-radius: 0.6rem;
box-shadow: 0 10px 20px -5px hsl(180deg 2% 10%); /* ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ ๋ ํธํ ์ปฌ๋ฌ ์ค์ ๋จ์! */
transform: scale(0);
}
.modal__box .title {
padding-inline: 1rem;
background-color: hsl(252 24% 10%);
display: flex;
align-items: center;
color: #fff;
height: 50px;
border-top-left-radius: 0.5rem;
border-top-right-radius: 0.5rem;
}
.modal__box .title .dot {
width: 15px;
height: 15px;
background-color: hsl(180 2% 50%);
display: inline-block;
border-radius: 50%;
position: relative;
margin-left: 2rem;
}
.modal__box .title .dot::before {
content: "";
position: absolute;
left: 25px;
top: 0;
width: 15px;
height: 15px;
background-color: hsl(180 2% 50%);
border-radius: 50%;
}
.modal__box .title .dot::after {
content: "";
position: absolute;
right: 25px;
top: 0;
width: 15px;
height: 15px;
background-color: hsl(180 2% 50%);
border-radius: 50%;
}
.modal__box .title .plus {
background: hsl(242 19% 21%);
padding: 0.5rem 0.5rem 0.3rem 0.5rem;
border-radius: 0.5rem;
color: #7a7d9d;
}
.modal__box .title .tabs {
display: flex;
margin-left: 50px;
}
.modal__box .title .tabs > div {
color: hsl(39 50% 59%);
background-color: hsl(242 19% 21%);
padding: 0.35rem 0.8rem 0.25rem 0.8rem;
margin-right: 0.5rem;
display: flex;
align-items: center;
border-radius: 0.4rem;
text-transform: uppercase;
cursor: pointer;
}
.modal__box .title .tabs > div.active {
background-color: black;
}
.modal__box .title .tabs > div em {
font-style: normal;
}
.modal__box .title .tabs > div .favicon {
margin-right: 0.4rem;
margin-top: 0.2rem;
}
.modal__box .title .tabs > div .close {
margin-left: 5rem;
}
.modal__box .cont {
background-color: #282936; /* cont ๋ฐฐ๊ฒฝ์ */
height: 100%;
overflow-y: auto;
box-sizing: border-box;
border-bottom-left-radius: 0.5rem;
border-bottom-right-radius: 0.5rem;
}
.modal__box .cont > div {
display: none;
}
.modal__box .cont > div.active {
display: block;
height: 100%;
}
.modal__close {
position: absolute;
right: 20px;
top: 20px;
background-color: #1f224a;
padding: 1rem 1rem 0.8rem 1rem;
border-radius: 5px;
box-shadow: 0 5px 7px -5px hsl(180deg 2% 10%);
cursor: pointer;
transition: all 0.3s;
opacity: 0;
}
.modal__close:hover {
background-color: #33377d;
}
.modal__close svg {
color: #fff;
}
/* ๋ชจ๋ฌ ์ ๋๋ฉ์ด์
*/
.modal__cont.show {
/* ์ ์ฒด ๋ฐฐ๊ฒฝ */
animation: foldOut 1s ease forwards; /* forwards ์ ๋๋ฉ์ด์
์ด ์๋๋ ํ ๊ณ ์ */
}
.modal__cont.show.hide {
animation: foldIn 0.3s 0.5s ease backwards;
}
.modal__cont.show .modal__box {
/* ์คํธ๋ฆฝํธ ๋ฐฐ๊ฒฝ */
transform: scale(0);
animation: zoomOut 0.5s 1s ease forwards;
}
.modal__cont.show.hide .modal__box {
animation: zoomIn 0.5s ease forwards;
}
.modal__cont.show .modal__close {
/* ๋ซ๊ธฐ ๋ฒํผ */
animation: opacityOut 0.8s 1s ease forwards;
}
.modal__cont.show.hide .modal__close {
animation: opacityIn 0.3s ease forwards;
}
@keyframes foldOut {
/* show๊ฐ ๋ถ์์ ๋ ์๋๋๋ ์ ๋๋ฉ์ด์
*/
0% {
transform: scaleX(0) scaleY(0.001);
}
50% {
transform: scaleX(1) scaleY(0.001);
}
100% {
transform: scaleX(1) scaleY(1);
}
}
@keyframes foldIn {
/* show hide๊ฐ ๋ถ์์ ๋ ์๋๋๋ ์ ๋๋ฉ์ด์
*/
0% {
transform: scaleX(1) scaleY(1);
}
50% {
transform: scaleX(1) scaleY(0.001);
}
100% {
transform: scaleX(0) scaleY(0.001);
}
}
@keyframes zoomOut {
/* show๊ฐ ๋ถ์์ ๋ ์๋๋๋ ์ ๋๋ฉ์ด์
*/
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes zoomIn {
/* show hide๊ฐ๊ฐ ๋ถ์์ ๋ ์๋๋๋ ์ ๋๋ฉ์ด์
*/
0% {
transform: scale(1);
}
100% {
transform: scale(0);
}
}
@keyframes opacityOut {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes opacityIn {
/* show hide๊ฐ ๋ถ์์ ๋ ์๋๋๋ ์ ๋๋ฉ์ด์
*/
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@media (max-width: 1100px) {
.modal__box .title {
overflow: hidden;
}
.modal__box .title .dot {
display: none;
}
.modal__box .title .tabs {
margin-left: 0; /* ์ค์์ ๋ ์๋จ ์ ์ง์ฐ๊ธฐ*/
}
.modal__box .title .dot > div .close {
display: none;
}
}
@media (max-width: 800px) {
#header {
width: 100%;
text-align: center;
}
#header h1 {
line-height: 1.4;
}
.modal__box {
width: 96%;
}
}
#2. JAVASCRIPT : ์คํฌ๋ฆฝํธ๋ก ํ๋ด๋์ค ํจ๊ณผ ์ฃผ๊ธฐ
๋ค์์ ๊ฐ์ฅ ์ค์ํ ์คํฌ๋ฆฝํธ ๋ถ๋ถ์ ๋๋ค! ๋จผ์ ์์ ์ฌ์ง์ ๋ณด๋ฉด์ ์คํฌ๋ฆฝํธ๊ฐ ์ด๋ป๊ฒ ์๋๋๋์ง ํ์ธํด๋ด ์๋ค! ๐ฅน
๐ก ์ ์ฒด ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
๋ค์์ ์ ์ฒด์ ์ธ ์คํฌ๋ฆฝํธ ์
๋๋ค. ์ฃผ์์ผ๋ก ์ฒ๋ฆฌ๋ ๋ถ๋ถ์ ์ค๋ช
์ธ์ ๊ฐ๊ฐ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์์ฑ๋ ์คํฌ๋ฆฝํธ๋ก ์ฐธ๊ณ ์ ๋๋ง ํ์๋ฉด ๋ฉ๋๋ค!
์ ๋ค๋ฅธ ๋ฐฉ์์ ์คํฌ๋ฆฝํธ๊ฐ ์๋ ์๋์ ์ต์ข
์คํฌ๋ฆฝํธ๊ฐ ๋์๋์ง ์ ๋๋ก๋ง ๋ณด์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๐
const scroll = document.querySelector("#parallax__info .scroll span");
window.addEventListener("scroll", () => {
// let scrollTop = window.pageYOffset;
// let scrollTop = window.scrollY;
// let scrollTop = document.documentElement.scrollTop;
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
// if(scrollTop >= document.getElementById("section1").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(1)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section2").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(2)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section3").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(3)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section4").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(4)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section5").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(5)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section6").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(6)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section7").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(7)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section8").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(8)").classList.add("active");
// }
// if(scrollTop >= document.getElementById("section9").offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child(9)").classList.add("active");
// }
// for(let i=1; i<=9; i++){
// if(scrollTop >= document.getElementById("section"+i).offsetTop){
// document.querySelectorAll("#parallax__nav li").forEach(li => {
// li.classList.remove("active");
// });
// document.querySelector("#parallax__nav li:nth-child("+i+")").classList.add("active");
// }
// }
// forEach
document.querySelectorAll(".content__item").forEach((el, index) => {
if (scrollTop >= el.offsetTop - 2) {
document.querySelectorAll("#parallax__nav li").forEach((li) => {
li.classList.remove("active");
});
document
.querySelector("#parallax__nav li:nth-child(" + (index + 1) + ")")
.classList.add("active");
}
});
//info
// scroll.innerText = Math.round(scrollTop);
document.querySelector(".scroll span").innerText =
Math.round(scrollTop);
// document.querySelector(".offset1").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset2").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset3").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset4").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset5").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset6").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset7").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset8").innerText = document.getElementById("section1").offsetTop;
// document.querySelector(".offset9").innerText = document.getElementById("section1").offsetTop;
//for()
for (let i = 1; i <= 9; i++) {
document.querySelector(".offset" + i).innerText =
document.getElementById("section" + i).offsetTop;
}
//์คํฌ๋กค ์ด๋
document.querySelectorAll("#parallax__nav li a").forEach((li) => {
li.addEventListener("click", (e) => {
e.preventDefault(); //ํด๋ฆญํ์ ๋ ๋ฉ๋ด์ ๋งํฌ ๋ ์์ญ์ ์์ฐ. ์ฆ a ํ๊ทธ๋ฅผ ๋๋ ์๋๋ href ๋งํฌ๋ก ์ด๋ํ์ง ์๊ฒํจ
document.querySelector(li.getAttribute("href")).scrollIntoView({ //ํน์ ์์น๋ก ํ๋ฉด์ ์ด๋์ํค๋ ๋ฉ์๋ : scrollIntoView ์ฆ ๋ฒํผ์ ๋๋ ์๋ ์ํ๋ ๋ด์ฉ์ด ์๋ ์์น๋ก ์ด๋์์ผ์ผ ํ ๋ ์ฌ์ฉํ ์ ์๋ค.
behavior: "smooth", //๋ถ๋๋ฝ๊ฒ ์์ง์ด๋๋ก : ์คํฌ๋กค์ด ์์ง์ผ ๋.
});
});
});
});
โ๏ธ ๊ทธ๋ผ ๋ค์์ ์ฝ๋๋ฅผ ๋ณด๋ฉฐ ์ฃผ์๊ณผ ํจ๊ป ์คํฌ๋ฆฝํธ๋ฅผ ์์๋๋ก ์์๊ฐ๋ด ์๋ค ๐ฅน
! JAVASCRIPT TIP !
1. .scrollIntoView : ํน์ ์์น๋ก ํ๋ฉด์ ์ด๋์ํค๋ ๋ฉ์๋ : ์ฆ ๋ฒํผ์ ๋๋ ์๋ ์ํ๋ ๋ด์ฉ์ด ์๋ ์์น๋ก ์ด๋์์ผ์ผ ํ ๋ ์ฌ์ฉํ ์ ์๋ค.
2. element.getAttribute() : ์ ํํ ์์์ ํน์ ์์ฑ ๊ฐ์ ๊ฐ์ ธ์ค๋ ๋ฉ์๋
3. ์คํฌ๋กค ์์ง ์์น๊ฐ์ ๋ถ๋ฌ์ค๋ 3๊ฐ์ง ๋ฐฉ๋ฒ : pageYOffset / scrollY / scrollTop
4. event.preventDefault : ์ผ๋ฐ์ ์ผ๋ก ๋ธ๋ผ์ฐ์ ์ ์ํด ์ฒ๋ฆฌ๋๋ ๊ธฐ์กด์ ์ก์
(๋์) ์ฆ ์ด๋ฒคํธ ๋ฐ์์ ๋ง์์ค๋ค. ex) a ํ๊ทธ์ ๊ธฐ๋ณธ ์ด๋ฒคํธ์ธ href ๋งํฌ ์ด๋์ ๋ง์์ ์ด๋ํ์ง ์๊ฒ ์ค์ .
const scroll = document.querySelector("#parallax__info .scroll span");
window.addEventListener("scroll", () => {
//์คํฌ๋กค ์์ง ์์น๊ฐ์ ๋ถ๋ฌ์ค๋ 3๊ฐ์ง ๋ฐฉ๋ฒ : pageYOffset / scrollY / scrollTop : 3๊ฐ์ง ์์ฑ์ ๋ชจ๋ ์ฌ์ฉํด์ผ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ์์น๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
//forEach๋ฌธ - active ์์ ๊ณ ์ฒซ๋ฒ์งธ์๊ฒ ๋ค์์ถ๊ฐ ์์ผ ์ถ๊ฐ๋ ์์๋ง ํจ๊ณผ๊ฐ ๋ํ๋๋๋ก ํ๊ธฐ
//์คํฌ๋กค ์์น ๊ฐ์ด ์์์ offsetTop(์์์ ์์ง ๊ฐ) ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ
document.querySelectorAll(".content__item").forEach((el, index) => {
if (scrollTop >= el.offsetTop -2) { //ํ ํ์ด์ง์์ ์คํฌ๋กค์ด ๋ง์ง๋ง ์์๊ฐ์ ๋ฏธ์น์ง ๋ชปํ๋ ์ค๋ฅ๊ฐ ์์ด ์ค์ฐจ๋ฅผ ์ค์ด๊ธฐ ์ํด -2๋ฅผ ํด์ฃผ์ด์ ์ค๋ฅ๋ฅผ ์ํํด์ฃผ์๋ค.
document.querySelectorAll("#parallax__nav li").forEach((li) => {
li.classList.remove("active"); //li์ ์ ์ฒด๋ฅผ ์ ํํ๊ธฐ ์ํด forEach๋ฌธ์ ํตํด li ๊ฐ์ ๋ฝ์๋ด๊ณ li ์์์ ์ ์ธ๋์ด์๋ ๋ชจ๋ active ์ ํ์๋ฅผ ์ง์์ค๋ค.
});
//์ ์กฐ๊ฑด์ด ์คํ๋ ํ ๋ค์ li ์์์ ์ฒซ๋ฒ์งธ์๊ฒ class๋ก active๋ฅผ ์ ์ธํ๋ค.
//์ธ๋ฑ์ค์ 1์ ์ถ๊ฐํ ์ด์ ๋ ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ. ์ฆ 0๋ถํฐ ์์ํ๋ฉด ๋ง์ง๋ง ์์ญ์ ํฌํจํ์ง ๋ชปํ๋ฏ๋ก 1๋ถํฐ ์์ํ๊ธฐ ์ํด ๋ง์ถฐ์ค ๊ฒ
document.querySelector("#parallax__nav li:nth-child(" + (index + 1) + ")").classList.add("active");
}
});
//info
//์คํฌ๋กค ์์น๊ฐ์ด ์์ซ์ ์ผ๋ก ๋์ค๊ธฐ ๋๋ฌธ์ ์์ซ์ ์ ์ฌ๋ฆผ ๋ฐ ๋ฐ์ฌ๋ฆผ์ ํด์ฃผ์ด ๊ฐ์ด ๋ฑ ๋จ์ด์ง๊ฒ ํด์ค๋ค
document.querySelector(".scroll span").innerText = Math.ceil(scrollTop); //Math.ceil = ๊ฐ์ ์ฌ๋ ค์ ์ซ์๋ก ๋ฐํ์ํค๋
//for()
//์ ํ์ offset์๊ฒ section์ n๋ฒ์งธ offsetTop๊ฐ์ innerText๋ฅผ ํตํด ์ ์ฅ์ํจ๋ค.
for (let i = 1; i <= 9; i++) {
document.querySelector(".offset" + i).innerText = document.getElementById("section" + i).offsetTop;
}
//์คํฌ๋กค ์ด๋
// ๋งํฌ๋ฅผ ํด๋ฆญํ์ ๋ ๋ฑ๋ฑํ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ์คํ๋์ง ์๊ณ ์๋ก ๋ถ๋๋ฌ์ด(smooth) ์ด๋ฒคํธ ๋ง๋ค์ด์ฃผ๊ธฐ
document.querySelectorAll("#parallax__nav li a").forEach((li) =>{
li.addEventListener("click", (e) =>{
e.preventDefault(); // event.preventDefault : ํด๋ฆญํ์ ๋ li๊ฐ ๊ฐ์ง๊ณ ์๋ a์ ์์ฑ์ธ href(๋งํฌ)๋ฅผ ์คํ์ํค์ง ์๋๋ค.
document.querySelector(li.getAttribute("href")).scrollIntoView({
behavior: "smooth", //๋ถ๋๋ฝ๊ฒ ์์ง์ด๋๋ก : ์คํฌ๋กค์ด ์์ง์ผ ๋.
});
});
});
});
'EFFECT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[EFFECT] ํ๋ด๋ ์ค ํจ๊ณผ 03 : top๋ฒํผ ๊ตฌํ๊ณผ ๋ฉ๋ด ์จ๊น ํจ๊ณผ (4) | 2022.09.14 |
---|---|
[EFFECT] ํ๋ด๋ ์ค ํจ๊ณผ02 : ์ค๋ฌด์ค~ํ ๋ฒํผ ์ด๋ (1) | 2022.09.14 |
[EFFECT] ๋ง์ฐ์ค ํจ๊ณผ 01 : ์ปค์ ๋ฐ๋ผ๋ค๋๊ธฐ (1) | 2022.09.06 |
[EFFCET] ์ด๋ฏธ์ง : ์ฌ๋ผ์ด๋ ์ ํ03 (12) | 2022.09.02 |
[EFFCET] ์ด๋ฏธ์ง : ์ฌ๋ผ์ด๋ ์ ํ02 (3) | 2022.08.30 |
๋๊ธ