CSSチュートリアル

CSSホーム CSSの紹介 CSS構文 CSSセレクター CSSハウツー CSSコメント CSSの色 CSSの背景 CSSボーダー CSSマージン CSSパディング CSSの高さ/幅 CSSボックスモデル CSSの概要 CSSテキスト CSSフォント CSSアイコン CSSリンク CSSリスト CSSテーブル CSSディスプレイ CSSの最大幅 CSSの位置 CSSZ-index CSSオーバーフロー CSSフロート CSSインラインブロック CSS整列 CSSコンビネータ CSS疑似クラス CSS疑似要素 CSSの不透明度 CSSナビゲーションバー CSSドロップダウン CSS画像ギャラリー CSS画像スプライト CSS AttrSelectors CSSフォーム CSSカウンター CSSWebサイトのレイアウト CSSユニット CSSの特異性 CSS!重要 CSS数学関数

CSS Advanced

CSSの丸みを帯びたコーナー CSSボーダー画像 CSSの背景 CSSの色 CSSカラーキーワード CSSグラデーション CSSシャドウ CSSテキスト効果 CSSWebフォント CSS2D変換 CSS3D変換 CSSトランジション CSSアニメーション CSSツールチップ CSSスタイルの画像 CSS画像反射 CSSオブジェクト適合 CSSオブジェクトの位置 CSSマスキング CSSボタン CSSページ付け CSSの複数の列 CSSユーザーインターフェイス CSS変数 CSSボックスのサイズ設定 CSSメディアクエリ CSSMQの例 CSSFlexbox

CSSレスポンシブ

RWDイントロ RWDビューポート RWDグリッドビュー RWDメディアクエリ RWD画像 RWDビデオ RWDフレームワーク RWDテンプレート

CSSグリッド

グリッドイントロ グリッドコンテナ グリッドアイテム

CSS SASS

SASSチュートリアル

CSSの

CSSテンプレート CSSの例 cssクイズ CSS演習 CSS証明書

CSSリファレンス

CSSリファレンス CSSセレクター CSS関数 CSSリファレンスオーラル CSSWebセーフフォント CSSアニメーション可能 CSSユニット CSSPX-EMコンバーター CSSの色 CSSの色の値 CSSのデフォルト値 CSSブラウザのサポート

CSSアニメーション


CSSアニメーション

CSSを使用すると、JavaScriptやFlashを使用せずにHTML要素のアニメーションを作成できます。

CSS

この章では、次のプロパティについて学習します。

  • @keyframes
  • animation-name
  • animation-duration
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-timing-function
  • animation-fill-mode
  • animation

アニメーションのブラウザサポート

表の数字は、プロパティを完全にサポートする最初のブラウザバージョンを示しています。

Property
@keyframes 43.0 10.0 16.0 9.0 30.0
animation-name 43.0 10.0 16.0 9.0 30.0
animation-duration 43.0 10.0 16.0 9.0 30.0
animation-delay 43.0 10.0 16.0 9.0 30.0
animation-iteration-count 43.0 10.0 16.0 9.0 30.0
animation-direction 43.0 10.0 16.0 9.0 30.0
animation-timing-function 43.0 10.0 16.0 9.0 30.0
animation-fill-mode 43.0 10.0 16.0 9.0 30.0
animation 43.0 10.0 16.0 9.0 30.0

CSSアニメーションとは何ですか?

アニメーションでは、要素をあるスタイルから別のスタイルに徐々に変更できます。

CSSプロパティは、何度でも変更できます。

CSSアニメーションを使用するには、最初にアニメーションのいくつかのキーフレームを指定する必要があります。

キーフレームは、要素が特定の時間に持つスタイルを保持します。


@keyframesルール

ルール内でCSSスタイルを指定する@keyframes と、アニメーションは特定の時間に現在のスタイルから新しいスタイルに徐々に変化します。

アニメーションを機能させるには、アニメーションを要素にバインドする必要があります。

次の例では、「example」アニメーションを<div>要素にバインドします。アニメーションは4秒間続き、<div>要素の背景色が「赤」から「黄色」に徐々に変化します。

/* The animation code */
@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}

/* The element to apply the animation to */
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

注:このanimation-durationプロパティは、アニメーションが完了するまでにかかる時間を定義します。プロパティが指定されていない場合animation-duration、デフォルト値は0秒(0秒)であるため、アニメーションは発生しません。 

上記の例では、キーワード「from」と「to」(0%(開始)と100%(完了)を表す)を使用して、スタイルがいつ変更されるかを指定しました。

パーセントを使用することも可能です。パーセントを使用すると、必要な数のスタイル変更を追加できます。

次の例では、アニメーションが25%完了したとき、50%完了したとき、およびアニメーションが100%完了したときに、<div>要素の背景色を変更します。

/* The animation code */
@keyframes example {
  0%   {background-color: red;}
  25%  {background-color: yellow;}
  50%  {background-color: blue;}
  100% {background-color: green;}
}

/* The element to apply the animation to */
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

次の例では、アニメーションが25%完了、50%完了、およびアニメーションが100%完了したときに、背景色と<div>要素の位置の両方を変更します。

/* The animation code */
@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}

/* The element to apply the animation to */
div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}


アニメーションを遅らせる

このanimation-delayプロパティは、アニメーションの開始の遅延を指定します。

次の例では、アニメーションを開始する前に2秒の遅延があります。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: 2s;
}

負の値も許可されます。負の値を使用すると、アニメーションはすでにN秒間再生されているかのように開始されます。

次の例では、アニメーションはすでに2秒間再生されているかのように開始されます。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: -2s;
}

アニメーションを実行する回数を設定する

このanimation-iteration-countプロパティは、アニメーションを実行する回数を指定します。

次の例では、アニメーションが停止する前に3回実行します。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 3;
}

次の例では、値「infinite」を使用して、アニメーションを永久に継続させます。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

アニメーションを逆方向または交互のサイクルで実行する

このanimation-directionプロパティは、アニメーションを順方向、逆方向、または交互のサイクルで再生するかどうかを指定します。

Animation-directionプロパティには、次の値を指定できます。

  • normal-アニメーションは通常どおり再生されます(順方向)。これはデフォルトです
  • reverse -アニメーションは逆方向(逆方向)に再生されます
  • alternate -アニメーションは最初に順方向に再生され、次に逆方向に再生されます
  • alternate-reverse -アニメーションは最初に逆方向に再生され、次に順方向に再生されます

次の例では、アニメーションを逆方向(逆方向)に実行します。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-direction: reverse;
}

次の例では、値「alternate」を使用して、アニメーションを最初に順方向に実行し、次に逆方向に実行します。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate;
}

次の例では、値「alternate-reverse」を使用して、アニメーションを最初に逆方向に実行し、次に順方向に実行します。

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate-reverse;
}

アニメーションの速度曲線を指定します

このanimation-timing-functionプロパティは、アニメーションの速度曲線を指定します。

alarm-timing-functionプロパティには、次の値を指定できます。

  • ease -開始が遅く、次に速く、次にゆっくり終了するアニメーションを指定します(これがデフォルトです)
  • linear -開始から終了まで同じ速度のアニメーションを指定します
  • ease-in -開始が遅いアニメーションを指定します
  • ease-out -スローエンドのアニメーションを指定します
  • ease-in-out -開始と終了が遅いアニメーションを指定します
  • cubic-bezier(n,n,n,n) -キュービックベジェ関数で独自の値を定義できます

次の例は、使用できるさまざまな速度曲線の一部を示しています。

#div1 {animation-timing-function: linear;}
#div2 {animation-timing-function: ease;}
#div3 {animation-timing-function: ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 {animation-timing-function: ease-in-out;}

アニメーションの塗りつぶしモードを指定します

CSSアニメーションは、最初のキーフレームが再生される前、または最後のキーフレームが再生された後、要素に影響を与えません。Animation-fill-modeプロパティは、この動作をオーバーライドできます。

このanimation-fill-modeプロパティは、アニメーションが再生されていないとき(開始前、終了後、またはその両方)のターゲット要素のスタイルを指定します。

Animation-fill-modeプロパティには、次の値を指定できます。

  • none-デフォルト値。アニメーションは、実行前または実行後に要素にスタイルを適用しません
  • forwards -要素は、最後のキーフレームによって設定されたスタイル値を保持します(animation-directionとanimation-iteration-countによって異なります)
  • backwards -要素は、最初のキーフレームによって設定されたスタイル値を取得し(アニメーションの方向によって異なります)、アニメーションの遅延期間中はこれを保持します
  • both -アニメーションは順方向と逆方向の両方のルールに従い、アニメーションのプロパティを両方向に拡張します

次の例では、アニメーションの終了時に<div>要素が最後のキーフレームのスタイル値を保持します。

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-fill-mode: forwards;
}

次の例では、<div>要素で、アニメーションが開始する前(アニメーションの遅延期間中)に最初のキーフレームで設定されたスタイル値を取得します。

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-delay: 2s;
  animation-fill-mode: backwards;
}

次の例では、<div>要素がアニメーション開始前に最初のキーフレームによって設定されたスタイル値を取得し、アニメーションが終了したときに最後のキーフレームからのスタイル値を保持します。

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-delay: 2s;
  animation-fill-mode: both;
}

アニメーション速記プロパティ

以下の例では、6つのアニメーションプロパティを使用しています。

div {
  animation-name: example;
  animation-duration: 5s;
  animation-timing-function: linear;
  animation-delay: 2s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

上記と同じアニメーション効果は、省略形の animationプロパティを使用して実現できます。

div {
  animation: example 5s linear 2s infinite alternate;
}

エクササイズで自分をテストする

エクササイズ:

<div>要素に2秒のアニメーションを追加します。これにより、色が赤から青に変わります。アニメーションを「例」と呼びます。

<style>
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: ;
  : 2s;
}

@keyframes example {
  from {: red;}
  to {: blue;}
}
</style>

<body>
  <div>This is a div</div>
</body>


CSSアニメーションのプロパティ

次の表に、@ keyframesルールとすべてのCSSアニメーションプロパティを示します。

Property Description
@keyframes Specifies the animation code
animation A shorthand property for setting all the animation properties
animation-delay Specifies a delay for the start of an animation
animation-direction Specifies whether an animation should be played forwards, backwards or in alternate cycles
animation-duration Specifies how long time an animation should take to complete one cycle
animation-fill-mode Specifies a style for the element when the animation is not playing (before it starts, after it ends, or both)
animation-iteration-count Specifies the number of times an animation should be played
animation-name Specifies the name of the @keyframes animation
animation-play-state Specifies whether the animation is running or paused
animation-timing-function Specifies the speed curve of the animation