<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Scroll Animation</title>
</head>
<body>
<h1>scroll to see the animtation</h1>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<div class="box"><h2>content</h2></div>
<script src="script.js"></script>
</body>
</html>
* {
box-sizing: border-box;
}
body {
background-color: #efedd6;
font-family: 'Roboto', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
}
h1 {
margin: 10px;
}
.box {
background-color: steelblue;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
width: 400px;
height: 200px;
margin: 10px;
border-radius: 10px;
box-shadow: 2px 4px 5px rgba(0, 0, 0, 0.3);
transform: translateX(400%);
transition: transform 0.4s ease;
}
.box:nth-of-type(even) {
transform: translateX(-400%);
}
.box.show {
transform: translateX(0);
}
.box h2 {
font-size: 45px;
}
const boxes = document.querySelectorAll('.box');
window.addEventListener('scroll', checkBoxes);
checkBoxes();
function checkBoxes() {
const triggerBottom = (window.innerHeight / 5) * 4;
boxes.forEach((box) => {
const boxTop = box.getBoundingClientRect().top;
if (boxTop < triggerBottom) {
box.classList.add('show');
} else {
box.classList.remove('show');
}
});
}
π μ΄ μ½λμ μν
π νμ΄μ§λ₯Ό μ€ν¬λ‘€ν λ λ°μ€(.box)κ° νλ©΄μ λ€μ΄μ€λ©΄ μ λλ©μ΄μ μ²λΌ λνλκ² ν΄μ£Όλ μ½λμμ.
(μ¦, μ€ν¬λ‘€ν λ λ°μ€κ° μ¬λΌμ΄λλλ©΄μ 보μ΄λ ν¨κ³Ό π¬)π μ½λ μ€λͺ
1. .boxλΌλ μμλ€ κ°μ Έμ€κΈ°
const boxes = document.querySelectorAll('.box');HTMLμμ
<div class="box">μ΄λ° λ°μ€λ₯Ό μ λΆ μ°Ύμμ boxesμ μ μ₯.μ¬λ¬ κ°λκΉ λ°°μ΄ κ°μ λ©μ΄λ¦¬(NodeList) λ‘ λͺ¨μμ§.
- μ€ν¬λ‘€ μ΄λ²€νΈ κ°μ§νκΈ°
window.addEventListener('scroll', checkBoxes);μ¬μ©μκ° μ€ν¬λ‘€ν λλ§λ€ checkBoxes() ν¨μλ₯Ό μ€νμν΄.
μ¦, μ€ν¬λ‘€ μμ§μΌ λλ§λ€ λ°μ€κ° νλ©΄μ λ€μ΄μλμ§ νμΈ.
- μ²μμλ ν λ² μ€ν
checkBoxes();μ€ν¬λ‘€ μ ν΄λ μ²μ λ‘λ©λ λ ν λ² μ€ν β νλ©΄μ 보μ΄λ λ°μ€λ λ°λ‘ λνλλλ‘ ν¨.
- checkBoxes ν¨μ
function checkBoxes() {
const triggerBottom = (window.innerHeight / 5) * 4;window.innerHeight = νμ¬ νλ©΄(λΈλΌμ°μ μ°½)μ λμ΄
/ 5 * 4 β νλ©΄ λμ΄μ 80% μμΉ μ§μ μ κ³μ°
μ¦, "νλ©΄ μλμμ 20% λ¨μ μ§μ "μ κΈ°μ€μ (triggerBottom)μΌλ‘ μ€μ
- λͺ¨λ λ°μ€ νμΈνκΈ°
boxes.forEach((box) => {
const boxTop = box.getBoundingClientRect().top;getBoundingClientRect().top = λ°μ€μ μλΆλΆμ΄ νλ©΄μ μ΄λμ―€ μλμ§ μλ €μ€
(μ«μκ° μμΌλ©΄ νλ©΄ μμͺ½, ν¬λ©΄ νλ©΄ μλμͺ½μ μλ€λ λ»)
- 쑰건문μΌλ‘ ν΄λμ€ μΆκ°/μμ
if (boxTop < triggerBottom) {
box.classList.add('show');
} else {
box.classList.remove('show');
}λ°μ€μ top μμΉκ° κΈ°μ€μ (triggerBottom)λ³΄λ€ μμͺ½μ μμΌλ©΄ β νλ©΄μ μΆ©λΆν λ€μ΄μμΌλ .show ν΄λμ€ λΆμ
μμ§ νλ©΄μ μ λ€μ΄μμΌλ©΄ .show ν΄λμ€ μ κ±°
π CSSμμ .showμ μ λλ©μ΄μ (μ¬λΌμ΄λ/νμ΄λ λ±)μ κ±Έμ΄λμκΈ° λλ¬Έμ, μ€ν¬λ‘€νλ©΄μ λ°μ€κ° λνλλ ν¨κ³Όκ° λλ κ²!
πΌοΈ λΉμ λ‘ μ€λͺ
νλ©΄μ 무λλΌκ³ μκ°ν΄μ π
triggerBottomμ "무λ μλ‘ μ¬λΌμ€κΈ° μ§μ λΌμΈ"
λ°μ€(.box)κ° κ·Έ λΌμΈμ λμ΄μλ©΄ μ‘°λͺ μ΄ μΌμ§κ³ (show ν΄λμ€ μΆκ°) κ΄κ°μκ² λ³΄μ΄λ κ²
무λ λ°μ μμΌλ©΄ λ€μ μ‘°λͺ μ΄ κΊΌμ§(show μ κ±°)