비전공자 개발일기

Scroll Animation 본문

HTML _CSS

Scroll Animation

HiroDaegu 2022. 6. 17. 00:46
728x90
SMALL

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SCROLL ANIMATION</title>
  <link rel="stylesheet" href="style.css">
  <script defer src="main.js"></script>
</head>
<body>
  <h1>Scroll to see the animation</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>
</body>
</html>
* {
  box-sizing: border-box;
}

body {
  background-color: #EFEDD6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 0;
  overflow-x: hidden;
}

h1 {
  margin: 10px;
}

.box {
  background-color: #9535F0;
  color: #FFF;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70%;
  height: 200px;
  margin: 10px;
  border-radius: 10px;
  box-shadow: 0 3px 12px #9535F0;
  transform: translateX(400%);
  transition: transform .8s ease;
}

.box:nth-of-type(even) {
  transform: translateX(-400%);
}

.box.show {
  transform: translateX(0);
}

.box h2 {
  font-size: 45px;
}
const boxes = document.querySelectorAll(".box");

const 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");
  });
};

window.addEventListener("scroll", checkBoxes);
checkBoxes()
728x90
LIST

'HTML _CSS' 카테고리의 다른 글

Project Management Dashboard  (0) 2022.06.22
Sliding Card UI Design  (0) 2022.06.19
Mobile Battery percentage Check  (0) 2022.06.16
CSS Input Text Field Animation  (0) 2022.06.14
Reavl Text Animation  (0) 2022.06.13