ν¨λ΄λμ€ μ΄ννΈ 2 - μ¬μ΄λ λ©λ΄
>ν¨λ΄λ μ€ μ΄ννΈ μ¬μ΄λ λ©λ΄
μ€ν¬λ‘€μ μμ§μμ λ μ¬μ΄λμ μλ μ λͺ¨μμ λ©λ΄μ λ³νκ° μκΉλλ€. μ€ν¬λ‘€μ μμΉκ° ν΄λΉ μΉμ μ μμΉ/μ¬μ΄λ λ©λ΄μ μ μ ν΄λ¦νλ©΄ ν΄λΉ μΉμ μ λμνλ μ¬μ΄λ λ©λ΄μ μ μ΄ νμ±νλ©λλ€.
html μμ€μ λλ€
<nav id="parallax__dot">
<ul>
<li class="active"><a href="#section01"><span>λ©λ΄1</span></a></li>
<li><a href="#section02"><span>λ©λ΄2</span></a></li>
<li><a href="#section03"><span>λ©λ΄3</span></a></li>
<li><a href="#section04"><span>λ©λ΄4</span></a></li>
<li><a href="#section05"><span>λ©λ΄5</span></a></li>
<li><a href="#section06"><span>λ©λ΄6</span></a></li>
<li><a href="#section07"><span>λ©λ΄7</span></a></li>
<li><a href="#section08"><span>λ©λ΄8</span></a></li>
<li><a href="#section09"><span>λ©λ΄9</span></a></li>
</ul>
</nav>
<!-- parallax nav -->
<main id="parallax__cont">
<div id="contents">
<section id="section01" 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>
<!-- section01 -->
<section id="section02" class="content__item">
<span class="content__item__num">02</span>
<h2 class="content__item__title">section02</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
λΉλ‘ λ΄μΌ μ§κ΅¬μ μ’
λ§μ΄ μ¨λ€ νλλΌλ λλ μ€λ ν 그루μ μ¬κ³Όλ무λ₯Ό μ¬κ² λ€.
</p>
</section>
<!-- section02 -->
<section id="section03" class="content__item">
<span class="content__item__num">03</span>
<h2 class="content__item__title">section03</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">κ·Έλ€μ΄ μ£½μμ λλ €μνλ κ²μ΄ μ΄μν©λλ€. μΆμ΄ μ£½μλ³΄λ€ ν¨μ¬ λ μνλ€.</p>
</section>
<!-- section03 -->
<section id="section04" class="content__item">
<span class="content__item__num">04</span>
<h2 class="content__item__title">section04</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
λͺ¨λ κ²μ΄ λ³νλ€λ κ²μ κΉ¨λ¬μΌλ©΄ λΆμ‘μΌλ € ν κ²μ΄ μλ€.
</p>
</section>
<!-- section04 -->
<section id="section05" class="content__item">
<span class="content__item__num">05</span>
<h2 class="content__item__title">section05</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
μ΄λ€ μκ°μ λ°μλ€μ΄μ§ μκ³ μ¦κ²κ² ν μ μλ κ²μ κ΅μ μλ μ μ μ νμ§μ΄λ€.
</p>
</section>
<!-- section05 -->
<section id="section06" class="content__item">
<span class="content__item__num">06</span>
<h2 class="content__item__title">section06</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
μ² νμ μΈμ λ μ°λ¦¬ λ μμ λμ¬ μλ κ·Έ μλν μ±
μ κΈ°λ‘λμ΄ μμ΅λλ€. μ¦, μ°μ£Όλ₯Ό μλ―Έν©λλ€.
</p>
</section>
<!-- section06 -->
<section id="section07" class="content__item">
<span class="content__item__num">07</span>
<h2 class="content__item__title">section07</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
ν볡μ νμ΄ μ¦κ°νκ³ μ νμ΄ κ·Ήλ³΅λκ³ μλ€λ λλμ
λλ€.
</p>
</section>
<!-- section07 -->
<section id="section08" class="content__item">
<span class="content__item__num">08</span>
<h2 class="content__item__title">section08</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">μ¬λμ κ΄κΈ°λ νλμ μΆλ³΅ μ€ κ°μ₯ ν° κ²μ
λλ€.</p>
</section>
<!-- section08 -->
<section id="section09" class="content__item">
<span class="content__item__num">09</span>
<h2 class="content__item__title">section09</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">κ°ν μκ³ ! λΉμ μ μ§λ₯μ μ¬μ©ν μ©κΈ°λ₯Ό κ°μ§μμμ€.</p>
</section>
<!-- section09 -->
</div>
</main>
<!-- //main -->
<aside id="parallax__info">
<div class="scroll">scrollTop : <span>0</span>px</div>
</aside>
css μμ€μ λλ€
/* parallax__cont */
#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(img/effect_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(img/effect_bg09.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item__desc {
font-size: 3vw;
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;
}
#parllax__nav li.active a {
border-radius: 5px;
}
#parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 2000;
background-color: rgba(0, 0, 0, 0.6);
color: #fff;
padding: 20px;
border-radius: 10px;
}
#parallax__info li,
.scrollTop {
line-height: 1.4;
}
#parallax__info li.active a {
border-radius: 5px;
}
@media (max-width: 800px) {
#parallax__cont {
margin-top: 70vw;
}
#parallax__info {
left: 10;
bottom: 10px;
}
}
#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,2551);
}
#parallax__dot li.active a {
transform: scale(0.4);
}
#parallax__dot li span {
display: none;
}
javascript μμ€μ λλ€
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 - 10){
document.querySelectorAll("#parallax__dot li").forEach(li =>{
li.classList.remove("active")
});
document.querySelector("#parallax__dot li:nth-child("+(i+1)+")").classList.add("active");
}
})
});
μ μ ν΄λ¦νμ λ μ€ν¬λ‘€ μ΄λμν€κΈ°
1. μ¬μ΄λ λ©λ΄μ μ μ 보 κ°μ Έμ€κΈ°
- λ©λ΄μ a νκ·ΈμΈ μ μ querySelectorAllμ μ΄μ©ν΄ λͺ¨λ μ ννμ¬ forEachλ¬Έμ μμ±ν©λλ€. μ¬κΈ°μ elμ κ°κ°μ μ μ μλ―Έν©λλ€.
document.querySelectorAll("#parallax__dot a").forEach(el => {});
2. μ¬μ΄λ λ©λ΄μ μ μ ν΄λ¦νμλ μ€ν¬λ‘€ μ΄λμν€κΈ°
- el(κ° μ / a νκ·Έ)λ₯Ό λμμΌλ‘ addEventListener() λ©μλλ‘ "click" μ΄λ²€νΈλ₯Ό λ°μμν΅λλ€.
- getAttribute("href")μ μ΄μ©ν΄ href μμ±μ λΆλ¬μ μ νν©λλ€.
- scrollIntoView()λ©μλλ₯Ό μ¬μ©νμ¬ μ μ ν΄λ¦νμμ λ μ€ν¬λ‘€μ μ΄λμν€κ² ν©λλ€.
- scrollIntoView()μ behavior: "smooth" μμ±μ μ¬μ©νλ©΄ μ€ν¬λ‘€ λμ΄ νλ©΄μ΄ μ΄λν λ, νλ©΄μ΄ λΆλλ½κ² μμ§μ
λλ€.
β» preventDefault
preventDefault λ₯Ό μ¬μ©νλ©΄ μ΄λ¬ν λμμ λ§μμ€ μ μμ΅λλ€.
el.addEventListener("click", e => {
e.preventDefault();
document.querySelector(el.getAttribute("href")).scrollIntoView({behavior: "smooth"});
});
μ€ν¬λ‘€μ νμ λ ν΄λΉ μΉμ μ λμνλ λ©λ΄μ μ νμ±νμν€κΈ°
1. μ€ν¬λ‘€ νμ λ μ΄λ²€νΈ λ°μμν€κΈ°
- μ°½(window)λ₯Ό λμμΌλ‘ scroll μ΄λ²€νΈλ₯Ό λ°μμν΅λλ€.
window.addEventListener("scroll", () => {});
2. λ³μ μμ±
- μ€ν¬λ‘€ κ°μ ꡬνκΈ° μν λ³μλ₯Ό μμ±μν€κ³ λ³μ μμ μ€ν¬λ‘€ κ°μ μ μ₯ν©λλ€.
β» λ³μμ μΈκ°μ§ κ°μ λͺ¨λ μ μ₯νλ μ΄μ
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
3. scrollTopμ κ° λ³΄μ¬μ£ΌκΈ°
- μ μ₯ν scrollTop κ°μ 보μ¬μ€ μ μλλ‘, λ§λ€μ΄ λ span νκ·Έ μμ innerTextλ₯Ό μ΄μ©ν΄ μμ±ν΄ μ€λλ€.
β» Math.floor()
document.querySelector("#parallax__info span").innerText = Math.floor(scrollTop);
4. κ° μΉμ μ ν
- κ° μΉμ
μ κ³΅ν΅ μμμΈ content__item ν΄λμ€λ₯Ό μ΄μ©ν΄, κ°κ°μ μΉμ
μ λͺ¨λ μ νν΄ μ€λλ€.
- μ΄λ κ° μΉμ
μ offsetTop κ°κ³Ό scrollTop κ°μ λΉκ΅ν΄ λ²νΌμ νμ±ννκΈ° μν¨μ
λλ€.
document.querySelectorAll(".content__item").forEach((e, i) => {});
5. λ©λ΄ νμ±ν / λΉνμ±ν
- ifλ¬Έμ μ¬μ©ν΄ λ©λ΄λ₯Ό νμ±ν νλ 쑰건문μ μμ±ν΄ μ€λλ€.
- scrollTop κ°μ΄ e(κ° μΉμ
)μ offsetTop κ°λ³΄λ€ ν¬κ±°λ κ°λ€λ κ²μ νλ©΄μ κ·Έ μΉμ
μ΄ μμΉνλ€λ μλ―Έμ
λλ€.
<μ€μ>
- querySelectorAllλ₯Ό μ΄μ©ν΄ λͺ¨λ λ©λ΄λ₯Ό μ νν΄ μ€ ν, forEachλ¬Έμ μμ±ν΄, λͺ¨λ λ©λ΄μ active ν΄λμ€λ₯Ό μ κ±°ν©λλ€.
- μ΄λ μ΄λ―Έ μ§λκ°λ²λ¦° μΉμ
μ λνλ΄λ λ©λ΄λ νμ±ν λμ§ μλλ‘ νκΈ° μν¨μ
λλ€.
- querySelectorλ₯Ό μ΄μ©ν΄ i+1 λ²μ§Έ λ©λ΄(νμ¬ λλ¬ν μΉμ
μ λνλ΄λ λ©λ΄)λ₯Ό μ νν΄ μ€ ν, active ν΄λμ€λ₯Ό μΆκ°ν©λλ€.
- νμ¬ λλ¬ν μΉμ
μ λνλ΄λ λ©λ΄λ₯Ό νμ±ν νκΈ° μν¨μ
λλ€.
if(scrollTop >= e.offsetTop -10){
document.querySelectorAll("#parallax__dot li").forEach(li => {
li.classList.remove("active");
});
document.querySelector("#parallax__dot li:nth-child("+ (i+1) +")").classList.add("active");
}
'μ΄ννΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
ν¨λ΄λμ€ μ΄ννΈ 4. λ΄μ© λνλκΈ° (3) | 2022.09.19 |
---|---|
ν¨λ΄λμ€ μ΄ννΈ 3. λ©λ΄ λνλκΈ° / νλ²νΌ (4) | 2022.09.19 |
ν¨λ΄λ μ€ μ΄ννΈ - 1. λ©λ΄ μ΄λ (6) | 2022.09.06 |
λ§μ°μ€ μ΄ννΈ - λ§μ°μ€ λ°λΌλ€λκΈ° (1) | 2022.09.06 |
03. μ¬λΌμ΄λ μ΄ννΈ - μ’λ‘ μμ§μ΄κΈ° μ°μ (1) | 2022.09.02 |
λκΈ