home
twitter
ページトップへ
目次
  • 「animation-direction」プロパティは「@keyframes」と一緒に使われる。
  • 「animation-direction」を使うと何ができるのか?
  • 「animation-direction」の使い方!
  • アニメーションを設定する要素自体に「animation-direction」の指定をする!
  • 「animation-direction」に設定できる値は?
  • 下記のコードを使って解説します。
  • 「normal」を使うと?
  • 「reverse」を使うと?
  • 「alternate」を使うと?
  • 「alternate-reverse」を使うと?
  • 「カンマ区切り」で「複数の値」も書くことができる!
  • 参考リンク!
  • おすすめの再生リスト!
  • まとめ!
about
categories
page
archive
皇帝ペンギンブログ

2024/09/05 (更新日:2024/09/09)

【CSS】「animation-direction」プロパティの使い方!【分かりやすい】

Categories > CSSのアニメーション編
こんばんは!初めましての方は初めまして!
しゅーた(@chibasyuta)です!

この記事ではCSSの「animation-direction」プロパティの使い方について解説します!丁寧にまとめていくので、興味がある方はぜひご一読ください。

「animation-direction」を使うと、「アニメーションが進む向き」の指定をすることができます。

この記事の内容!

  • 「animation-direction」プロパティを使うと何ができるのか?
  • 「animation-direction」プロパティの使い方!
また、下の動画でもCSSの「animation-direction」プロパティの使い方について詳しく解説しているので、一緒に実際にコードを書きながら学びたい方はこちらをどうぞ!

それでは内容へ入ります!

目次
非表示
  • 「animation-direction」プロパティは「@keyframes」と一緒に使われる。
  • 「animation-direction」を使うと何ができるのか?
  • 「animation-direction」の使い方!
  • アニメーションを設定する要素自体に「animation-direction」の指定をする!
  • 「animation-direction」に設定できる値は?
  • 下記のコードを使って解説します。
  • 「normal」を使うと?
  • 「reverse」を使うと?
  • 「alternate」を使うと?
  • 「alternate-reverse」を使うと?
  • 「カンマ区切り」で「複数の値」も書くことができる!
  • 参考リンク!
  • おすすめの再生リスト!
  • まとめ!

「animation-direction」プロパティは「@keyframes」と一緒に使われる。

「animation-direction」プロパティは単体で使うのではなく「@keyframes」を使ったアニメーションの定義と一緒に使われます。

なのでもし「@keyframes」の使い方がいまいち分からないという方は下記の記事で詳しくまとめているので、こちらも一緒に読んでみてください!

【CSS】「@keyframes」を使ってアニメーションをつける基本的な方法!【分かりやすい】

関連記事

「animation-direction」を使うと何ができるのか?

https://chibasyuta.org/wp-content/uploads/2024/09/demo_new-3.mp4
<div class="box n1"></div>
<div class="box n2"></div>
<div class="box n3"></div>
@keyframes rotate {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}
.box {
    width: 100px;
    height: 100px;
    margin-bottom: 50px;
    background-color: skyblue;
    animation-name: rotate;
    animation-duration: 1s;
    animation-iteration-count: infinite;
    animation-direction: reverse;
}

「animation-direction」を使うと、「アニメーションが進む向き」の指定をすることができます。

「@keyframes」の「0%」地点から「100%」地点へとアニメーションが進む感じにしたり、

逆に「100%」から「0%」へと逆方向にアニメーションさせることなんかもできますね。

ふむふむ。

「animation-direction」の使い方!

「animation-direction」の使い方をまとめていきます!

アニメーションを設定する要素自体に「animation-direction」の指定をする!

@keyframes rotate {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}
.box {
    width: 100px;
    height: 100px;
    margin-bottom: 50px;
    background-color: skyblue;
    animation-name: rotate;
    animation-duration: 1s;
    animation-iteration-count: infinite;
    animation-direction: reverse;
}

「animation-direction」の指定は、アニメーションをつける要素に対して行えばOKです!

上の例だと、「@keyframes」を使って「rotate」というアニメーションを定義して、そのアニメーションを「.box」というクラスがついた要素に「animation-name」プロパティを使って設定しています。

そしてその「.box」というクラスがついた要素に「animation-direction」プロパティを使って、「アニメーションの進行方向」の設定をしている、という感じになりますね。

「animation-direction」に設定できる値は?

「animation-direction」に設定できる値は下記の通りです。

  • normal
  • reverse
  • alternate
  • alternate-reverse

これらがありますね。

初期値は「normal」です。

下記のコードを使って解説します。

@keyframes width {
    0% {
        width: 100px;
    }
    100% {
        width: 1000px;
    }
}
.box {
    width: 100px;
    height: 100px;
    margin-bottom: 50px;
    background-color: skyblue;
    animation-name: width;
    animation-duration: 5s;
    animation-iteration-count: infinite;
    animation-direction: normal;
}
上のコードを使って、「animation-direction」の値だけ変更しながら、その挙動の解説を行います!

「normal」を使うと?

https://chibasyuta.org/wp-content/uploads/2024/09/animation-direction-normal_new.mp4

「animation-direction」に「normal」を使うと、「@keyframes」で作ったアニメーションは「0%」→「100%」の方向で進みます。

これを「順方向」と呼びます。

アニメーションが繰り返される際は、再び「0%」→「100%」の形で繰り返されます。

「reverse」を使うと?

https://chibasyuta.org/wp-content/uploads/2024/09/animation-direction-reverse.mp4

「animation-direction」に「reverse」を使うと、「@keyframes」で作ったアニメーションは「100%」→「0%」の方向で進みます。

これを「逆方向」と呼びます。

アニメーションが繰り返される際は、再び「100%」→「0%」の形で繰り返されます。

「alternate」を使うと?

https://chibasyuta.org/wp-content/uploads/2024/09/animation-direction-alternate_new.mp4

「animation-direction」に「reverse」を使うと、「@keyframes」で作ったアニメーションは、まず「0%」→「100%」の方向で進みます。「順方向」ですね。

そしてアニメーションが繰り返される際は、まず「100%」→「0%」の形で繰り返され、3回目のアニメーションは「0%」→「100%」というように「行ったり来たり」する形でアニメーションが繰り返されます。

「alternate-reverse」を使うと?

https://chibasyuta.org/wp-content/uploads/2024/09/animation-direction-alternate-reverse_new.mp4

「animation-direction」に「reverse」を使うと、「@keyframes」で作ったアニメーションは、まず「100%」→「0%」の方向で進みます。「逆方向」ですね。

そしてアニメーションが繰り返される際は、まず「0%」→「100%」の形で繰り返され、3回目のアニメーションは「0%」→「100%」というように「行ったり来たり」する形でアニメーションが繰り返されます。

「カンマ区切り」で「複数の値」も書くことができる!

.box {
    animation-name: width, rotate;
    animation-direction: reverse, alternate;
}

このように「animation-direction」には「カンマ区切り」で「複数の値」を書くことができます。

その場合は、「animation-name」に書かれた順番に対応する形で「animation-direction」の値がそれぞれのアニメーションに対して別々に指定される感じになります。

この辺の挙動は下記の記事で詳しくまとめているので、もし気になる方は下記の記事を読んでみてください!

【CSS】「animation-name」プロパティの使い方!【分かりやすい】

関連記事

参考リンク!

仕様書。
https://drafts.csswg.org/css-animations/#animation-direction

MDN。
https://developer.mozilla.org/ja/docs/Web/CSS/animation-direction

おすすめの再生リスト!

こちらは僕が制作したYouTubeの再生リスト「CSSのアニメーション編」です。

「CSSのアニメーション編」はCSSを使ったアニメーションの知識が網羅的に学習できるコンテンツとなります。

こちらの再生リストでCSSアニメーションについて丁寧に解説しているので、もし気になる方はチェックしてみてください!一緒に楽しく学習しましょう!!

まとめ!

「animation-direction」を使うと、「アニメーションが進む向き」の指定をすることができる。

「animation-direction」の指定は、アニメーションをつける要素に対して行えばOK!

「animation-direction」に設定できる値は下記の通り。

  • normal
  • reverse
  • alternate
  • alternate-reverse

「animation-direction」には「カンマ区切り」で「複数の値」を書くことも可能!

こんな感じですかね!

この記事が気に入った方は僕のTwitter(@chibasyuta)のチェックもお願いします!

また、下の動画でもCSSの「animation-direction」プロパティの使い方について詳しく解説しているので、一緒に実際にコードを書きながら学びたい方はこちらをどうぞ!

それではこんな感じでこの記事を終わります!

ではまた!

自分のロゴ!

皇帝ペンギン

全力で、書きます。
少しでもお役に立てたら幸いです。
楽しんで読んで下さい 。-_- 。
よろしくお願いします!

x(旧twitter) youtube
Categories
  • トップへ
  • ITパスポート (1)
  • programming (168)
    • CSSのアニメーション編 (26)
    • CSSのグリッドレイアウト編 (1)
    • CSSのセレクター編 (58)
    • CSSのフレックスボックス編 (1)
    • CSSのメディアクエリー編 (7)
    • CSSの基本編 (34)
    • HTMLのformタグ編 (1)
    • HTMLの基本編 (3)
    • JavaScriptの入門編 (30)
    • JavaScriptの基本編 (5)
    • Web制作をする環境を整えよう! (1)
  • Uncategorized (23)
  • VScode (1)
  • おすすめ商品! (1)
  • クラロワ (2)
  • マーケティング (17)
  • 大学生活 (7)
  • 心理学 (9)
  • 映画 (1)
  • 法学 (4)
  • 経済学 (4)
  • 読書 (7)
Pages
  • プライバシーポリシー
  • 免責事項
  • プロフィール
Archive
  • 2025年3月 (1)
  • 2025年1月 (9)
  • 2024年12月 (13)
  • 2024年11月 (5)
  • 2024年10月 (10)
  • 2024年9月 (15)
  • 2024年8月 (5)
  • 2024年7月 (6)
  • 2024年6月 (5)
  • 2024年5月 (10)
  • 2024年4月 (24)
  • 2024年3月 (11)
  • 2024年2月 (6)
  • 2024年1月 (10)
  • 2023年12月 (4)
  • 2023年11月 (3)
  • 2023年10月 (2)
  • 2023年9月 (3)
  • 2023年8月 (6)
  • 2023年7月 (3)
  • 2023年6月 (8)
  • 2023年5月 (2)
  • 2023年3月 (1)
  • 2023年2月 (1)
  • 2022年10月 (2)
  • 2022年9月 (2)
  • 2022年8月 (8)
  • 2022年7月 (2)
  • 2022年6月 (6)
  • 2022年5月 (9)
  • 2022年4月 (2)
  • 2022年2月 (4)
  • 2021年12月 (1)
  • 2021年11月 (4)
  • 2021年10月 (8)
  • 2021年9月 (14)
  • 2021年8月 (6)
  • 2021年6月 (1)
  • 2021年5月 (2)
  • 2021年4月 (1)
  • 2021年3月 (1)
  • 2021年2月 (2)
  • 2020年10月 (1)
  • 2020年7月 (1)
  • 2020年4月 (1)
  • 2020年1月 (1)
  • 2019年12月 (2)
© Copyright Syuta Chiba, 2019 All Rights Reserved.