🐡

【GSAP】GSAP Practice【#2 Lenis Smooth Scroll】

2024/11/25に公開

【#2 Lenis Smooth Scroll】

YouTube: https://youtu.be/N1sRwkCvdHk
https://youtu.be/N1sRwkCvdHk

今回はSmooth Scroll用のライブラリ
「Lenis」の設定を行います。

https://lenis.darkroom.engineering/

スクロールできる要素の作成と
「Lenis」の読み込みを行います。

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Gsap practice</title>
    <link rel="stylesheet" href="css/main.css" />
  </head>
  <body>
    <div class="section-1">section1</div>
    <div class="section-2">section2</div>

    <script src="https://unpkg.com/lenis@1.1.16/dist/lenis.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/ScrollTrigger.min.js"></script>
    <script src="js/main.js"></script>
  </body>
</html>
css/main.css
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  width: 100%;
  height: 100%;
}
.section-1,
.section-2 {
  width: 100%;
  height: 100vh;
}
.section-1 {
  background-color: salmon;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.section-2 {
  background-color: skyblue;
}
js/main.js
// use a script tag or an external JS file
document.addEventListener("DOMContentLoaded", (event) => {
  gsap.registerPlugin(ScrollTrigger);

  // Initialize Lenis
  const lenis = new Lenis();

  // Use requestAnimationFrame to continuously update the scroll
  function raf(time) {
    lenis.raf(time);
    requestAnimationFrame(raf);
  }

  requestAnimationFrame(raf);

  // gsap code here!
});

Discussion