์๋ฐ์คํฌ๋ฆฝํธ : ํจ๋ด๋์ค ํจ๊ณผ : ์ฌ์ด๋ ๋ฉ๋ด
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ค์ํ ํจ๊ณผ๋ค ์ค, ํจ๋ด๋์ค ํจ๊ณผ์ ๋๋ค. ์ด๋ฒ์๋ ์๋จ ๋ฉ๋ด๊ฐ ์๋ ์ฌ์ด๋ ๋ฉ๋ด๋ฅผ ๋ง๋ค์ด ์ฃผ๊ณ , ํ์ฌ ์์นํ ์คํฌ๋กค๊ฐ์ ๋ฐ๋ผ ๋ฉ๋ด์ ์ ๋๋ฉ์ด์ ํจ๊ณผ๋ฅผ ์ถ๊ฐํด ์ฃผ์์ต๋๋ค.
HTML ์์ค
HTML์ ๋จผ์ nav, main, aside ์ธ ํํธ๋ก ๋๋์ด ์ฃผ์์ต๋๋ค. nav์๋ 9๊ฐ์ ๋ฉ๋ด, main์๋ 9๊ฐ์ ์ฝํ ์ธ , aside์๋ ์คํฌ๋กค์ ์์น๊ฐ์ ๋ณผ ์ ์๊ฒ๋ ๋ง๋ค์ด ์ฃผ์์ต๋๋ค.
<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>
<!-- //parallax__nav -->
<main id="parallax__cont">
<div id="contents">
<section id="section1" class="content__item">
<span class="content__item__num">01</span>
<h2 class="content__item__title">section1</h2>
<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>
<h2 class="content__item__title">section2</h2>
<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>
<h2 class="content__item__title">section3</h2>
<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>
<h2 class="content__item__title">section4</h2>
<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>
<h2 class="content__item__title">section5</h2>
<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>
<h2 class="content__item__title">section6</h2>
<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>
<h2 class="content__item__title">section7</h2>
<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>
<h2 class="content__item__title">section8</h2>
<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>
<h2 class="content__item__title">section9</h2>
<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>
</aside>
CSS ์์ค
CSS๋ก ์คํ์ผ ์์ฑ์ ์ถ๊ฐํด ์ฃผ์์ต๋๋ค. ๋ฉ๋ด์ info ๋ฐ์ค๋ position: fixed๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ฉด์ ๊ณ ์ ๋๊ฒ ์ค์ ํด ์ฃผ์์ต๋๋ค.
/* parallaxType */
/* parallax__nav */
#parallax__nav {
position: fixed;
right: 20px;
top: 20px;
z-index: 2000;
background-color: rgba(0, 0, 0, 0.4);
padding: 20px 30px;
border-radius: 50px;
}
#parallax__nav li {
display: inline;
margin: 0 5px;
}
#parallax__nav li a {
display: inline-block;
height: 30px;
padding: 5px 10px;
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;
width: 98%;
margin: 0 auto;
/* background-color: rgba(255, 255, 255, 0.1); */
}
.content__item {
width: 1000px;
max-width: 70vw;
margin: 30vw auto;
/* background-color: rgba(255, 255, 255, 0.3); */
text-align: left;
margin-right: 0;
position: relative;
padding-top: 7vw;
}
/* nth-child์ ๊ดํธ ์์๋ ์์์ ์ฌ์ฉํ ์ ์๋ค, even = 2n (์ง์๋ง ์ ํ) */
.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 {
/* left ๊ฐ ์ด๊ธฐํ */
left: auto;
right: -5vw;
}
.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;
background-image: url(../assets/img/effect_bg20-min.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
width: 110%;
height: 110%;
left: -5%;
top: -5%;
filter: saturate(0%);
transition: all 1s;
}
.content__item:nth-child(1) .content__item__img {
background-image: url(../assets/img/effect_bg18-min.jpg);
}
.content__item:nth-child(2) .content__item__img {
background-image: url(../assets/img/effect_bg17-min.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: url(../assets/img/effect_bg16-min.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: url(../assets/img/effect_bg15-min.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: url(../assets/img/effect_bg14-min.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: url(../assets/img/effect_bg13-min.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: url(../assets/img/effect_bg19-min.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: url(../assets/img/effect_bg20-min.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: url(../assets/img/effect_bg04-min.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: 10px;
bottom: 10px;
z-index: 2000;
background: rgba(0, 0, 0, 0.6);
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: 10px;
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__dot {
position: fixed;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 10000;
background: rgba(0, 0, 0, 0.4);
padding: 20px 10px;
border-radius: 30px;
}
#parallax__dot li {
position: relative;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 12px 8px;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0);
transition: box-shadow 0.2s ease;
}
#parallax__dot li a {
background: #fff;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
border-radius: 50%;
transition: transform 0.2s ease;
}
#parallax__dot li.active {
box-shadow: 0 0 0 2px rgba(255, 255, 255, 1);
}
#parallax__dot li.active a {
transform: scale(0.4);
}
#parallax__dot li span {
display: none;
}
JAVASCRIPT ์์ค
preventDefault() | ํด๋ฆญ ์ ํ์ด์ง ์ด๋ ์ด๋ฒคํธ๋ฅผ ์ ๊ฑฐํฉ๋๋ค. |
Attribute() | ์ ํํ ์์์ ํน์ ์์ฑ ๊ฐ์ ๊ฐ์ ธ์ต๋๋ค. |
scrollIntoView() | ํน์ ์์์ ์์น๋ก ์คํฌ๋กค์ ์ด๋์ํต๋๋ค.
behavior: smooth ์ต์ ์ ์ถ๊ฐํ๋ฉด ๋ถ๋๋ฝ๊ฒ ์ด๋ํฉ๋๋ค. |
document.querySelectorAll("#parallax__dot a").forEach((el) => {
el.addEventListener("click", (e) => {
e.preventDefault();
document.querySelector(el.getAttribute("href")).scrollIntoView({ behavior: "smooth" });
});
});
window.addEventListener("scroll", () => {
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
document.querySelector("#parallax__info span").innerText = Math.floor(scrollTop);
document.querySelectorAll(".content__item").forEach((e, i) => {
if (scrollTop >= e.offsetTop - window.innerHeight - 10) { /* ์ค์ฐจ๋ฅผ ์ค์ด๊ธฐ ์ํด -10 */
/* ์คํฌ๋กค์ ํ์ฌ ์์น >= ๊ฐ ์น์
์ ์์น - ๋ธ๋ผ์ฐ์ ์ ๋์ด */
document.querySelectorAll("#parallax__dot li").forEach((li) => {
li.classList.remove("active"); /* ๊ฐ ์น์
์ ๋์ฐฉํ๋ฉด active ํด๋์ค๋ฅผ ์ ๊ฑฐ */
});
document.querySelector("#parallax__dot li:nth-child(" + (i + 1) + ")").classList.add("active");
}
});
});
// window.scroll(0, 1000);
// window.scroll({ left: 0, top: 2000 });
// window.scroll({ left: 0, top: 2000, behivor: "smooth" });
// window.scrollTo(0, 1000);
// window.scrollTo({ left: 0, top: 2000 });
// window.scrollTo({ left: 0, top: 2000, behivor: "smooth" });
// ์ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ ๊ฐ์ ๊ฐ์ด ๋์ด (์คํฌ๋กค๊ฐ 1000์ผ๋ก ์ด๋)
// window.scrollBy(0, 1000); /* ํ์ฌ ์์น ๊ธฐ์ค์ผ๋ก 1000์ฉ ๋ค์ด */
// window.scrollBy({ left: 0, top: 2000 });
// window.scrollBy({ left: 0, top: 2000, behivor: "smooth" });
๊ฒฐ๊ณผ
๋ฐ์ํ์ผ๋ก๋ ์คํฌ๋กค์ ๋ฒ์์ ์ค์ฐจ๊ฐ ์๊ธฐ์ง๋ง, ์ง๊ธ์ผ๋ก์๋ ๊ณ ์น ๋ฐฉ๋ฒ์ด ์์ผ๋ ํํด์ ๋ฐ์ง ์๊ฒ ์ต๋๋ค.
'Javascript Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ํ๋๊ธฐ (4) | 2022.09.18 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ฉ๋ด ์จ๊น, TOP๋ฒํผ (6) | 2022.09.10 |
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ฉ๋ด ์ด๋ (9) | 2022.09.06 |
์๋ฐ์คํฌ๋ฆฝํธ ๋ง์ฐ์ค ํจ๊ณผ : ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ (2) | 2022.09.06 |
์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ผ์ด๋ ํจ๊ณผ : ์ข๋ก ์์ง์ด๋ ์ ํ(์ฐ์์ ) (9) | 2022.09.01 |
๋๊ธ