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では配布されていません)。
- <script src="gsap.min.js"></script>
- <script src="SplitText.min.js"></script>
- <script>
- gsap.registerPlugin(SplitText);
- </script>
基本コード
- <h1 class="headline">Hello GSAP SplitText</h1>
- // テキストを分割
- const split = new SplitText(".headline", { type: "chars, words, lines" });
- // 文字ごとにアニメーション
- gsap.from(split.chars, {
- opacity: 0,
- y: 50,
- stagger: 0.05, // 0.05秒ずつずれて再生
- duration: 1,
- ease: "power2.out"
- });
3. SplitTextのオプション
- new SplitText(target, {
- type: "chars, words, lines", // 分割の種類
- wordsClass: "myWord", // 各単語に付けるクラス
- charsClass: "myChar", // 各文字に付けるクラス
- linesClass: "myLine" // 各行に付けるクラス
- });
• type
• “chars” → 文字単位
• “words” → 単語単位
• “lines” → 行単位
• カンマ区切りで複数指定可能
• charsClass, wordsClass, linesClass
→ それぞれにクラスを付与できるので、CSS制御も可能。
4. よくあるアニメーション例
1. 文字ごとに出現
- <!-- HTML -->
- <h2 class="title">Hello GSAP SplitText</h2>
- <!-- JS -->
- <script>
- gsap.registerPlugin(SplitText);
- const splitChars = new SplitText(".title", { type: "chars" });
- gsap.from(splitChars.chars, {
- opacity: 0,
- y: 30,
- stagger: 0.05,
- ease: "back.out",
- duration: 1
- });
- </script>
2. 単語ごとにフェードイン
- <!-- HTML -->
- <h2 class="title">Hello GSAP SplitText</h2>
- <!-- JS -->
- <script>
- gsap.registerPlugin(SplitText);
- <!-- HTML -->
- <p class="sentence">SplitText makes text animations so much easier!</p>
- <!-- JS -->
- <script>
- const splitWords = new SplitText(".sentence", { type: "words" });
- gsap.from(splitWords.words, {
- opacity: 0,
- y: 20,
- stagger: 0.2,
- ease: "power2.out",
- duration: 1
- });
- </script>
3. 行ごとにスライドイン
- <!-- HTML -->
- <p class="paragraph">
- GSAP SplitText allows you<br>
- to animate each line<br>
- individually with ease.
- </p>
- <!-- JS -->
- <script>
- const splitLines = new SplitText(".paragraph", { type: "lines" });
- gsap.from(splitLines.lines, {
- opacity: 0,
- x: -100,
- stagger: 0.3,
- ease: "power3.out",
- duration: 1
- });
- </script>
5. まとめ
• SplitTextは文字・単語・行を分割して、個別にアニメーションできるようにするプラグイン
• type: “chars, words, lines” で分割方法を指定
• split.chars / split.words / split.lines をGSAPでアニメーション
• フェードイン、バラバラ出現、行ごとのスライドなど演出の幅が広がる
MILMONA MILMONA MILMONA MILMONA MILMONA
MILMONA MILMONA MILMONA MILMONA MILMONA