PORTFOLIO & WEB DESIGN MEMORANDOM BLOG   
PORTFOLIO & WEB DESIGN MEMORANDOM BLOG   

【JavaScript】GSAP:SplitTextの使い方

GSAPのSplitText は文字アニメーションを作るプラグインです。

1. SplitTextとは?

通常のHTMLテキストはただの1つの塊ですが、SplitTextを使うと次のように自動で分割されます:

• lines(行ごと)
• words(単語ごと)
• chars(文字ごと)

その結果、文字や単語をバラバラに動かすアニメーションが簡単に作れます。

2. 基本的な使い方

インポート

SplitTextは GSAP Club GreenSockの有料プラグイン です。
公式サイトで入手して読み込む必要があります(CDNでは配布されていません)。

  1. <script src="gsap.min.js"></script>
  2. <script src="SplitText.min.js"></script>
  3. <script>
  4.   gsap.registerPlugin(SplitText);
  5. </script>

基本コード

  1. <h1 class="headline">Hello GSAP SplitText</h1>

  1. // テキストを分割
  2. const split = new SplitText(".headline", { type: "chars, words, lines" });
  3. // 文字ごとにアニメーション
  4. gsap.from(split.chars, {
  5.   opacity: 0,
  6.   y: 50,
  7.   stagger: 0.05, // 0.05秒ずつずれて再生
  8.   duration: 1,
  9.   ease: "power2.out"
  10. });

3. SplitTextのオプション

  1. new SplitText(target, {
  2.   type: "chars, words, lines", // 分割の種類
  3.   wordsClass: "myWord", // 各単語に付けるクラス
  4.   charsClass: "myChar", // 各文字に付けるクラス
  5.   linesClass: "myLine" // 各行に付けるクラス
  6. });

• type
• “chars” → 文字単位
• “words” → 単語単位
• “lines” → 行単位
• カンマ区切りで複数指定可能
• charsClass, wordsClass, linesClass
→ それぞれにクラスを付与できるので、CSS制御も可能。

4. よくあるアニメーション例

1. 文字ごとに出現

  1. <!-- HTML -->
  2. <h2 class="title">Hello GSAP SplitText</h2>
  3. <!-- JS -->
  4. <script>
  5.   gsap.registerPlugin(SplitText);
  6.   const splitChars = new SplitText(".title", { type: "chars" });
  7.   gsap.from(splitChars.chars, {
  8.     opacity: 0,
  9.     y: 30,
  10.     stagger: 0.05,
  11.     ease: "back.out",
  12.     duration: 1
  13.   });
  14. </script>

2. 単語ごとにフェードイン

  1. <!-- HTML -->
  2. <h2 class="title">Hello GSAP SplitText</h2>
  3. <!-- JS -->
  4. <script>
  5.   gsap.registerPlugin(SplitText);
  6. <!-- HTML -->
  7. <p class="sentence">SplitText makes text animations so much easier!</p>
  8. <!-- JS -->
  9. <script>
  10.   const splitWords = new SplitText(".sentence", { type: "words" });
  11.   gsap.from(splitWords.words, {
  12.     opacity: 0,
  13.     y: 20,
  14.     stagger: 0.2,
  15.     ease: "power2.out",
  16.     duration: 1
  17.   });
  18. </script>

3. 行ごとにスライドイン

  1. <!-- HTML -->
  2. <p class="paragraph">
  3.   GSAP SplitText allows you<br>
  4.   to animate each line<br>
  5.   individually with ease.
  6. </p>
  7. <!-- JS -->
  8. <script>
  9.   const splitLines = new SplitText(".paragraph", { type: "lines" });
  10.   gsap.from(splitLines.lines, {
  11.     opacity: 0,
  12.     x: -100,
  13.     stagger: 0.3,
  14.     ease: "power3.out",
  15.     duration: 1
  16.   });
  17. </script>

5. まとめ

• SplitTextは文字・単語・行を分割して、個別にアニメーションできるようにするプラグイン
• type: “chars, words, lines” で分割方法を指定
• split.chars / split.words / split.lines をGSAPでアニメーション
• フェードイン、バラバラ出現、行ごとのスライドなど演出の幅が広がる

MILMONA   MILMONA   MILMONA   MILMONA   MILMONA   
MILMONA   MILMONA   MILMONA   MILMONA   MILMONA