0Pricing
React Academy · レッスン

Motion Componentsと基本アニメーション

divをmotion.divに置き換え、animate、initial、exit propsでシンプルなアニメーションを実装します。

「Motion Componentsと基本アニメーション」はCoddyKit上の無料React Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはReact Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 React Academyコースには全4レッスンが含まれています。

Framer Motion とは

Framer Motion は React 用のアニメーションライブラリです。HTML 要素をmotion.*の対応する要素に置き換えることで、CSS キーフレームではなく props を使った宣言的なアニメーションを追加できます。

インストール

Framer Motion をインストールし、そこから motion をインポートしてください。

npm install framer-motion

import { motion } from 'framer-motion';

最初のアニメーション

<div>を<motion.div>に置き換え、initial、animate、transitionの props を追加してください。

<motion.div
  initial={{ opacity: 0, y: -20 }}
  animate={{ opacity: 1, y: 0 }}
  transition={{ duration: 0.4 }}
>
  Hello, Framer Motion!
</motion.div>

initial prop

initialはアニメーションの開始状態を定義します。要素がanimateの状態へアニメーションする前の開始地点です。

// Fade in from left
<motion.div
  initial={{ opacity: 0, x: -50 }}
  animate={{ opacity: 1, x: 0 }}
/>

// Scale up from center
<motion.button
  initial={{ scale: 0 }}
  animate={{ scale: 1 }}
/>

animate prop

animateは目標状態を定義します。Framer Motion は、スプリング物理または指定したイージングを使ってinitialとanimateの間を補間します。

<motion.h1
  initial={{ opacity: 0 }}
  animate={{ opacity: 1, color: '#4f46e5' }}
  transition={{ duration: 0.6, ease: 'easeOut' }}
>
  Welcome
</motion.h1>

Transition オプション

transition prop はアニメーションの動きを制御します。duration、ease、delay、type('spring' | 'tween' | 'inertia')を指定できます。

transition={{
  type: 'spring',
  stiffness: 300,
  damping: 20,
  delay: 0.2,
}}

animate でのキーフレーム

アニメーションする任意の値に配列を渡すと、複数の状態を経由するキーフレームアニメーションを作成できます。

<motion.div
  animate={{ x: [0, 30, 0] }}
  transition={{ duration: 1, repeat: Infinity, ease: 'easeInOut' }}
/>

ホバーとタップの状態

whileHoverとwhileTapは、ホバーや押下操作中のアニメーション状態を定義します。ジェスチャーが終わると自動的に元に戻ります。

<motion.button
  whileHover={{ scale: 1.05, backgroundColor: '#4f46e5' }}
  whileTap={{ scale: 0.95 }}
  style={{ backgroundColor: '#6366f1' }}
>
  Press me
</motion.button>

inView によるスクロール時のアニメーション

whileInView prop を使うと、要素がビューポート内にスクロールされて入ったときにアニメーションを開始できます。

<motion.section
  initial={{ opacity: 0, y: 40 }}
  whileInView={{ opacity: 1, y: 0 }}
  viewport={{ once: true, amount: 0.3 }}
  transition={{ duration: 0.5 }}
>
  <h2>Revealed on scroll</h2>
</motion.section>

AnimatePresence と exit prop

exitはコンポーネントのアンマウント時のアニメーションを定義します。<AnimatePresence>でラップした場合にのみ機能します。

import { motion, AnimatePresence } from 'framer-motion';

<AnimatePresence>
  {isVisible && (
    <motion.div
      key="modal"
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      exit={{ opacity: 0, scale: 0.95 }}
    />
  )}
</AnimatePresence>

命令型制御のための useAnimate

useAnimate()は、宣言的な prop システムの外部からプログラムでアニメーションを制御するためのscope ref とanimate関数を返します。

import { useAnimate } from 'framer-motion';

function ShakeButton() {
  const [scope, animate] = useAnimate();
  const handleClick = async () => {
    await animate(scope.current, { x: [-10, 10, -10, 0] }, { duration: 0.3 });
  };
  return <motion.button ref={scope} onClick={handleClick}>Shake</motion.button>;
}

モーションの削減

useReducedMotion()フックを使って、ユーザーの視覚効果を減らす設定を尊重してください。

import { useReducedMotion } from 'framer-motion';

function AnimatedCard() {
  const shouldReduce = useReducedMotion();
  return (
    <motion.div
      animate={{ opacity: 1, y: shouldReduce ? 0 : -20 }}
    />
  );
}

確認問題

コンポーネントが到達すべきアニメーション状態(目標)を定義する Framer Motion の prop はどれですか?

まとめ

HTML 要素をmotion.*の対応する要素に置き換えます。開始状態にはinitial、目標状態にはanimate、タイミングにはtransition、ジェスチャーにはwhileHover/whileTap、スクロールをトリガーにするアニメーションにはwhileInViewを使います。

よくある質問

「Motion Componentsと基本アニメーション」レッスンは無料ですか?

はい。「Motion Componentsと基本アニメーション」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、React Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 React Academyコースには全4レッスンが含まれています。

「Motion Componentsと基本アニメーション」で何を学びますか?

divをmotion.divに置き換え、animate、initial、exit propsでシンプルなアニメーションを実装します。 ブラウザで直接実行するハンズオンコードでReact Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

React Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのReact Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「Motion Componentsと基本アニメーション」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このReact Academyレッスンでコードを書いて実行できますか?

はい。すべてのReact Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Motion Componentsと基本アニメーション
  2. アニメーションを連携するVariants
  3. ジェスチャーアニメーション:Hover、Tap、Drag
  4. レイアウトアニメーションとAnimatePresence
← React Academyに戻る