--- title: Element.animate() slug: Web/API/Element/animate translation_of: Web/API/Element/animate ---
{{domxref("Element")}} インターフェースの animate() メソッドは、新たに {{domxref("Animation")}} の作成、対象要素への適用、そしてアニメーションの再生を行うショートカットメソッドです。戻り値として {{domxref("Animation")}} オブジェクトのインスタンスを返します。
element.animate(keyframes, options);
keyframeselement.animate({
opacity: [ 0, 1 ], // [ フレーム 1, フレーム 2 ]
color: [ "#fff", "#000" ] // [ フレーム 1, フレーム 2 ]
}, 2000);
element.animate([
{ // フレーム 1
opacity: 0,
color: "#fff"
},
{ // フレーム 2
opacity: 1,
color: "#000"
}
], 2000);
optionsanimate() に渡すことができます。idcompositereplace です。
add dictates an additive effect, where each successive iteration builds on the last. 例として transform を挙げると、translateX(-200px) は自身よりも前に指定されていた rotate(20deg) の値を上書きすることはありませんが、合成結果は translateX(-200px) rotate(20deg) になります。accumulate を指定した場合、add に似ていますがよりスマートな結果が得られ、blur(2) と blur(5) の合成結果は blur(7) になります(blur(2) blur(5) ではありません)。replace を指定した場合、前回の値は新しい値で上書きされます。iterationCompositeaccumulate または replace を指定できます(上記参照)。デフォルト値は replace です。{{domxref("Animation")}} を返します。
Down the Rabbit Hole (with the Web Animation API) のデモでは、上に向かって永遠に流れ続けるアニメーションが #tunnel 要素に施されています。ここでは、アニメーションを素早く作成して再生できる animate() メソッドが用いられています。keyframes として渡されているオブジェクト配列と、timing options として渡されているオブジェクトに注目してください。
document.getElementById("tunnel").animate([
// keyframes
{ transform: 'translate3D(0, 0, 0)' },
{ transform: 'translate3D(0, -300px, 0)' }
], {
// timing options
duration: 1000,
iterations: Infinity
});
| 仕様書 | 策定状況 | 備考 |
|---|---|---|
| {{SpecName('Web Animations', '#the-animatable-interface', 'animate()' )}} | {{Spec2('Web Animations')}} | Editor's draft. |
{{Compat("api.Element.animate")}}