animation-duration
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
* Some parts of this feature may have varying levels of support.
animation-duration は CSS のプロパティで、 1 回のアニメーション周期が完了するまでの再生時間を設定します。
試してみましょう
animation-duration: 750ms;
animation-duration: 3s;
animation-duration: 0s;
<section class="flex-column" id="default-example">
<div class="animating" id="example-element"></div>
<button id="play-pause">再生</button>
</section>
#example-element {
animation-direction: alternate;
animation-iteration-count: infinite;
animation-name: slide;
animation-play-state: paused;
animation-timing-function: ease-in;
background-color: #1766aa;
border-radius: 50%;
border: 5px solid #333;
color: white;
height: 150px;
margin: auto;
margin-left: 0;
width: 150px;
}
#example-element.running {
animation-play-state: running;
}
#play-pause {
font-size: 2rem;
}
@keyframes slide {
from {
background-color: orange;
color: black;
margin-left: 0;
}
to {
background-color: orange;
color: black;
margin-left: 80%;
}
}
"use strict";
window.addEventListener("load", () => {
const el = document.getElementById("example-element");
const button = document.getElementById("play-pause");
button.addEventListener("click", () => {
if (el.classList.contains("running")) {
el.classList.remove("running");
button.textContent = "再生";
} else {
el.classList.add("running");
button.textContent = "一時停止";
}
});
});
アニメーションのプロパティすべてを一度に設定するには、一括指定プロパティである animation プロパティを使用すると便利です。
構文
/* 単一のアニメーション */
animation-duration: auto; /* 既定値 */
animation-duration: 6s;
animation-duration: 120ms;
/* 複数のアニメーション */
animation-duration: 1.64s, 15.22s;
animation-duration: 10s, 35s, 230ms;
/* グローバル値 */
animation-duration: inherit;
animation-duration: initial;
animation-duration: revert;
animation-duration: revert-layer;
animation-duration: unset;
値
auto-
時間ベースのアニメーションでは、
autoは0sの値と等価です(下記参照)。 CSS スクロール駆動アニメーションでは、autoはタイムライン全体をアニメーションで埋めます。 <time>-
1 回のアニメーションの周期にかかる時間。この値は、秒 (
s) またはミリ秒 (ms) で指定することができます。値は正の数か 0 でなければならず、単位は必須です。値が指定されなかった場合、既定値である
0sが使用され、この場合でもアニメーションは実行されます(animationStartとanimationEndイベントが発生します)。長さが0sのときにアニメーションが表示されるかどうかは、下記で説明するanimation-fill-modeの値によります。animation-fill-modeをbackwardsまたはbothに設定した場合、animation-directionで定義したアニメーションの最初のフレームがanimation-delay(/ja/docs/Web/CSS/animation-delay) のカウントダウン中に表示されます。animation-fill-modeがforwardsまたはbothに設定した場合、アニメーションの最後のフレームはanimation-delayが経過した後に、animation-directionで定義したように表示されます。animation-fill-modeをnoneに設定すると、アニメーションは目に見える効果はありません。
メモ:
負の数は無効であり、宣言が無視されます。一部、初期の接頭辞付きの実装は 0s と等価に解釈するかもしれません。
メモ:
animation-* プロパティにカンマ区切りで複数の値を指定した場合、 animation-name に現れる順にアニメーションに適用されます。アニメーションの数と animation-* プロパティの値が一致しない場合は、複数のアニメーションプロパティ値の設定 を参照してください。
メモ:
CSS スクロール駆動アニメーションを作成するとき、animation-duration の値を秒やミリ秒で指定することは実際には意味がありません。テストしたところ、スクロール進行タイムラインアニメーションには効果がないように見えましたが、ビュー進行タイムラインアニメーションでは、アニメーションがタイムラインの終わりに近づいて起こるようになるようでした。しかし、Firefox がアニメーションを正常に適用するには animation-duration の設定が要求されます。そのため、Firefoxでアニメーションが動作するように animation-duration を 1ms に設定することをお勧めします。
公式定義
形式文法
animation-duration =
[ auto | <time [0s,∞]> ]#
例
>アニメーション時間の設定
これは animation-duration が 0.7 秒のアニメーションです。
HTML
<div class="box"></div>
CSS
.box {
background-color: rebeccapurple;
border-radius: 10px;
width: 100px;
height: 100px;
}
.box:hover {
animation-name: rotate;
animation-duration: 0.7s;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
結果
矩形の上にカーソルを当てると、アニメーションを開始します。
他の例については、 CSS アニメーションを参照してください。
仕様書
| Specification |
|---|
| CSS Animations Level 1> # animation-duration> |
ブラウザーの互換性
Loading…
関連情報
- CSS アニメーションの使用
- JavaScript の
AnimationEventAPI - その他のアニメーション関連プロパティ:
animation,animation-composition,animation-delay,animation-direction,animation-fill-mode,animation-iteration-count,animation-name,animation-play-state,animation-timeline,animation-timing-function