<div class="section">
  <div class="parallax-element background"></div>  
  <div class="parallax-element square"></div>
  <div class="parallax-element circle"></div>
  <strong class="parallax-element title">텍스트는 빠르고 <br /> 배경은 느림</strong>
</div>

<div class="extra-content">
  <p>Parallax Scrolling</p>
</div>
.section {
  height: 200vh;
  position: relative;
  overflow: hidden;
}

.parallax-element {
  position: absolute;
  width: 100%;
  height: 100%;
  transform: translateZ(0);
}

.background {
  position: relative;
  background: url(https://images.unsplash.com/photo-1604782206219-3b9576575203?q=80&w=1994&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D) no-repeat 0 0 / cover;
  background-attachment: fixed;
  z-index: 1;
}
.background::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #000;
  opacity: 0.2;
}

.circle {
  top: 300px;
  left: -300px;
  width: 300px;
  height: 300px;
  background-color: aquamarine;
  z-index: 3;
  opacity: 0.5;
  border-radius: 50%;
}

.square {
  bottom: 500px;
  right: -100px;
  width: 200px;
  height: 200px;
  background-color: lightsalmon;
  z-index: 3;
  opacity: 0.7;
}

.title {
  position: fixed;
  font-size: 3em;
  color: white;
  text-align: center;
  width: 100%;
  height: auto;
  z-index: 2;
  top: 40%; /* 초기 위치 조정 */
  transform: scale(0);
  line-height: 1.2;
  font-weight: bold;
}

.extra-content {
  position: relative;
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 높이를 충분히 주어 스크롤 생성 */
  background-color: #333;
  padding: 20px;
  box-sizing: border-box;
  font-size: 3rem;
  color: #fff;
}
View Compiled
window.addEventListener('scroll', function () {
  // 스크롤 이벤트 리스너 등록
  const sections = document.querySelector('.section'); // 모든 섹션을 가져옴

  const background = sections.querySelector('.background'); // 배경 요소
  const title = sections.querySelector('.title'); // 제목 요소
  const circle = sections.querySelector('.circle'); // 원 요소
  const square = sections.querySelector('.square'); // 사각형 요소

  const scrolled = window.scrollY // 윈도우의 스크롤값
  
  console.log("scrollY : " + scrolled)

  background.style.transform = `translateY(${scrolled * 0.8}px)`; 
  // 제목을 스크롤 속도의 80%(빠르게)로 이동
  title.style.transform = `scale(${scrolled * 0.01})`; 
  // 배경을 스크롤 속도의 30%(느리게)로 이동
  circle.style.transform = `translate(${scrolled}px, ${scrolled}px)`; 
  // circle 왼쪽에서 오른쪽으로 이동
  square.style.transform = `translate(${scrolled * -0.5}px, ${scrolled * 0.5}px)`; 
  // square 오른쪽에서 왼쪽으로 이동
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.