😸

【GSAP】GSAP Practice【#8 GSAP Sample1 Loading】

2024/12/14に公開

【#8 GSAP Sample1 Loading】

YouTube: https://youtu.be/aVEhwoJu-CM
https://youtu.be/aVEhwoJu-CM

今回からScrollTriggerの解説に入る前に
ここまでの応用でいくつかサンプルを作成していこうと思います。

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">
      <div id="loading">Loading</div>
    </div>
    <div class="section-2">
      <div class="box box-4">gsap.stagger</div>
      <div class="box box-4">gsap.stagger</div>
      <div class="box box-4">gsap.stagger</div>
    </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;
  align-items: center;
  justify-content: center;
}

#loading {
  color: white;
  font-size: 60px;
  display: flex;
}

#loading span {
  display: block;
}

.section-2 {
  background-color: skyblue;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.box {
  width: 160px;
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}
.box-1 {
  background-color: red;
}
.box-2 {
  background-color: blue;
}
.box-3 {
  background-color: green;
}
.box-4 {
  background-color: purple;
}
js/main.js
// use a script tag or an external JS file
document.addEventListener("DOMContentLoaded", (event) => {
  gsap.registerPlugin(ScrollTrigger);
  new App();
});

class App {
  constructor() {
    this._initialize();
    this._renderLenis();
  }

  _initialize() {
    this._createLenis();
    this._sec1Anime();
    this._sec2Anime();
  }

  _createLenis() {
    this.lenis = new Lenis();
  }

  _renderLenis(time) {
    this.lenis.raf(time);
    requestAnimationFrame(this._renderLenis.bind(this));
  }

  _sec1Anime() {
    const loadingEl = document.querySelector("#loading");
    const loadingTexts = loadingEl.textContent.split("");
    let innerText = "";

    loadingTexts.forEach((text) => {
      innerText += `<span>${text}</span>`;
    });

    // console.log(loadingTexts);
    // console.log(innerText);

    loadingEl.innerHTML = innerText;

    const tl = gsap.timeline({ repeat: -1 });

    tl.to("#loading span", {
      duration: 1,
      y: -50,
      stagger: {
        each: 0.2,
        from: "left",
      },
    }).to("#loading span", {
      duration: 1,
      y: 0,
      stagger: {
        each: 0.2,
        from: "left",
      },
      ease: "bounce.out",
    });
  }

  _sec2Anime() {
    gsap.to(".box-4", {
      x: 600,
      duration: 3,
      opacity: 0,
      stagger: {
        each: 1,
        from: "center",
        yoyo: true,
        repeat: -1,
        ease: "power4.out",
      },
    });
  }
}

Discussion