스크롤에 따라 기울어지며 나타나는 이미지

UI

  • scroll
  • rotate

2023-05-02 08:40

<!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">
  <link rel="stylesheet" href="./index.css">
  <title>Fast Bank</title>
</head>

<body>
  <main>
    <section id="panel1-img">
      <img id="flying-santa-image" src="./santa_flying.png" alt="santa flying">
    </section>
  </main>
</body>
html
const panel1Img = document.getElementById('panel1-img')
  const flyingSantaImage = document.getElementById('flying-santa-image')

  window.addEventListener("scroll", () => {
    const scrollYBottom = window.scrollY + document.documentElement.clientHeight

    if (scrollYBottom > panel1Img.offsetTop && scrollYBottom < panel1Img.offsetTop + panel1Img.offsetHeight + 100) {
      console.log('over')
      const translateX = 80 - 80 * 1.3 * (scrollYBottom - panel1Img.offsetTop) / (panel1Img.offsetHeight + 100)
      const translateY = -13 + 13 * (scrollYBottom - panel1Img.offsetTop) / (panel1Img.offsetHeight + 100)

      const rotationDegree = 23 - 23 * 1.7 * (scrollYBottom - panel1Img.offsetTop) / (panel1Img.offsetHeight + 100)

      flyingSantaImage.style.transform = `translate(${translateX}px, ${translateY}px) rotate(${rotationDegree}deg)`
    }
  })
javascript