順番に動かす(Sequence)

このページでは、GSAP(timeline・stagger)と Framer Motion(stagger()・animate() が返す timeline)を使ったシーケンスアニメーションの実装パターンを、インタラクティブなデモとコードで確認できます。

シーケンスアニメーションとは、複数の要素や動きを「順番に」制御するアニメーション。カード一覧の順次出現・ローディングのステップ表示・複雑なUIの状態遷移など、実務で頻出するパターン。

GSAPはtimeline()でアニメーションの順序・タイミング・再生制御を直感的に書ける。Framer Motion(Motion 14)も、animate()に配列を渡すとtimelineが返り、逆再生・一時停止・速度の変更を同じように書ける。stagger は親のdelayChildrenにstagger()を渡す(staggerChildrenは非推奨)。

Framer Motion

順に出すのは、親の transition の delayChildren に stagger() を渡す(grid も指定できる)。useAnimate の animate() は timeline を返すので、call() で途中に処理を挟み、playTo() で再生・逆再生できる。

プレビュー

読み込み中...

tsx
'use client';
import { useEffect, useRef, useState } from 'react';
import { motion, stagger, useAnimate } from 'motion/react';
import type { DOMSegmentWithTransition, Timeline, Variants } from 'motion/react';

const STEPS = ['データ取得', '検証', '処理中', '完了'];
const STEP_SECONDS = 0.6;
const POP = [0.34, 1.56, 0.64, 1] as const;

// ① 親の transition の delayChildren に stagger() を渡すと、子の動きが順にずれる
const rowParent: Variants = { hidden: {}, show: { transition: { delayChildren: stagger(0.12) } } };
const rowItem: Variants = {
  hidden: { opacity: 0, y: 30 },
  show: { opacity: 1, y: 0, transition: { duration: 0.5, ease: POP } },
};
// ② stagger() の grid に [列, 行] を渡すと、左上からの距離に応じてずれる
const gridParent: Variants = { hidden: {}, show: { transition: { delayChildren: stagger(0.2, { grid: [4, 3] }) } } };
const gridItem: Variants = {
  hidden: { opacity: 0, scale: 0.5 },
  show: { opacity: 1, scale: 1, transition: { duration: 0.4, ease: POP } },
};

function Stagger({ parent, item, count, columns }: { parent: Variants; item: Variants; count: number; columns: number }) {
  const [shown, setShown] = useState(false);
  // key を変えて作り直すと、initial(hidden)からやり直せる
  const [key, setKey] = useState(0);
  return (
    <div>
      <button onClick={() => { setShown(true); setKey((k) => k + 1); }}>再生</button>
      <button onClick={() => { setShown(false); setKey((k) => k + 1); }}>リセット</button>
      <motion.div
        key={key} variants={parent} initial="hidden" animate={shown ? 'show' : 'hidden'}
        style={{ display: 'grid', gridTemplateColumns: `repeat(${columns}, 56px)`, gap: 8, marginTop: 12 }}
      >
        {Array.from({ length: count }, (_, i) => (
          <motion.div key={i} variants={item} style={{ width: 56, height: 56, borderRadius: 10, background: '#c084fc' }} />
        ))}
      </motion.div>
    </div>
  );
}

// ③ 進捗バーを animate() で伸ばし、その timeline の call() で段階を進める。
// call() は、再生位置がその時刻を通ったときに呼ばれる(引数は向き。1 が順方向)
function Loading() {
  const [scope, animate] = useAnimate();
  const tlRef = useRef<Timeline | null>(null);
  const [active, setActive] = useState(-1);

  const play = () => {
    tlRef.current?.cancel(); // 前の再生が残っていれば止める
    setActive(0);
    const tl = animate('.progress', { scaleX: [0, 1] }, { duration: STEPS.length * STEP_SECONDS, ease: 'linear' });
    for (let i = 1; i <= STEPS.length; i++) {
      tl.call((direction) => { if (direction > 0) setActive(i); }, i * STEP_SECONDS);
    }
    tlRef.current = tl;
  };

  const reset = () => {
    tlRef.current?.cancel(); // cancel() は止めて最初の状態に戻す(進捗バーも 0 に戻る)
    tlRef.current = null;
    setActive(-1);
  };

  return (
    <div ref={scope}>
      <button onClick={play}>再生</button>
      <button onClick={reset}>リセット</button>
      <div style={{ height: 4, background: '#f3f4f6', margin: '12px 0', overflow: 'hidden' }}>
        <div className="progress" style={{ height: '100%', background: '#c084fc', transform: 'scaleX(0)', transformOrigin: 'left' }} />
      </div>
      {STEPS.map((step, i) => (
        <motion.div
          key={step}
          // 色は単色で切り替える(グラデーションは補間できず、警告が出る)
          animate={{ backgroundColor: active > i ? '#f0fdf4' : active === i ? '#fdf4ff' : '#f9fafb' }}
          transition={{ duration: 0.3 }}
          style={{ display: 'flex', gap: 12, padding: '8px 12px', marginTop: 8, borderRadius: 10 }}
        >
          <span>{active > i ? '✓' : i + 1}</span>
          <span>{step}</span>
          <span style={{ marginLeft: 'auto' }}>{active === i ? '処理中...' : active > i ? '完了' : ''}</span>
        </motion.div>
      ))}
    </div>
  );
}

// ④ [要素, 値, 設定] の配列を animate() に渡すと、順に動かす timeline が返る
const SEQUENCE = [0, 1, 2, 3].map(
  (i): DOMSegmentWithTransition => [`.box-${i}`, { x: 160 }, { duration: 0.5, ease: 'easeInOut' }],
);

function TimelineControl() {
  const [scope, animate] = useAnimate();
  const tlRef = useRef<Timeline | null>(null);

  useEffect(() => {
    const tl = animate(SEQUENCE, { autoplay: false }); // 止めた状態で作っておく
    tlRef.current = tl;
    return () => tl.cancel();
  }, [animate]);

  // playTo() は、今の位置から指定した時刻まで進めて(または戻して)止まる
  return (
    <div ref={scope}>
      <button onClick={() => tlRef.current?.playTo(tlRef.current.duration)}>▶ 再生</button>
      <button onClick={() => tlRef.current?.playTo(0)}>◀ 逆再生</button>
      <button onClick={() => tlRef.current?.seek(0).pause()}>リセット</button>
      {[0, 1, 2, 3].map((i) => (
        <div key={i} className={`box-${i}`} style={{ width: 40, height: 40, marginTop: 8, borderRadius: 8, background: '#c084fc' }} />
      ))}
    </div>
  );
}

export default function SequenceDemo() {
  return (
    <div style={{ display: 'grid', gap: 32 }}>
      <Stagger parent={rowParent} item={rowItem} count={5} columns={5} />
      <Stagger parent={gridParent} item={gridItem} count={12} columns={4} />
      <Loading />
      <TimelineControl />
    </div>
  );
}

AIプロンプトテンプレート

Next.js + Motion(motion/react)でシーケンスアニメーションを実装してください。
- 5つの箱を順に出す:親の variants の transition に delayChildren: stagger(0.12) を指定する
- 4列×3行のグリッドを、左上からの距離に応じて順に出す:stagger(0.2, { grid: [4, 3] })
- 4段階のローディング:useAnimate の animate() で進捗バーを伸ばし、返ってきた timeline の call() で段階を進める。リセットでは cancel() で止める
- 4つの箱を順に右へ動かす timeline を animate() に配列で渡して作り(autoplay: false)、playTo() で再生・逆再生する
- TypeScript / Tailwind CSS v4 を使用

このプロンプトはあくまでたたき台です。AIの回答はモデルやバージョン、会話の文脈によって毎回異なります。意図通りに動かない場合は、条件を追記・修正してお使いください。

💡 上記のデモは、React / Next.jsです。Next.jsの基本セットアップは 公式ドキュメントを参照してください。

実装方法の比較

項目Framer MotionGSAP
stagger親の transition の delayChildren に stagger() を渡す。grid・from(どこから)も指定できるstaggerオプションで直感的に指定。配列・グリッド・カスタム関数も対応
シーケンス制御animate() に [要素, 値, 設定] の配列を渡すと timeline が返る。at で重ねも指定できるtimeline()にtweenを並べるだけ。位置パラメータで重ねも自在
逆再生timeline の playTo(0)・reverse()timeline.reverse()で即座に逆再生
一時停止・再開timeline の pause() / play()timeline.pause() / timeline.resume() でシンプルに制御
再生速度変更timeline の speed(例:tl.speed = 2)timeline.timeScale(2) で再生速度を変更