[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/Web_Animations_API/Tips [Back]  [Original]

API - Web API | MDN

MDN Web Docs

View in English Always switch to English

API

CSS

CSS animation-play-state "running" JavaScript

HTML

HTML <div>

html
<div class="box"></div>
<button class="runButton"></button>

CSS

CSS

css
.box {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  margin-bottom: 1rem;
}

JavaScript

JavaScript playAnimation() @keyframes JavaScript

js
const box = document.querySelector(".box");
const button = document.querySelector(".runButton");

/*
   CSS @keyframes 

  @keyframes colorChange {
    0% {
      background-color: grey;
    }
    100% {
      background-color: lime;
    }
  }
*/
const colorChangeFrames = { backgroundColor: ["grey", "lime"] };

function playAnimation() {
  box.animate(colorChangeFrames, 4000);
}

playAnimation Element.animate() animate() colorChangeFrames

js
button.addEventListener("click", playAnimation);

0% from run finish animation-iteration

playAnimation() finish

js
function playAnimation() {
  button.setAttribute("disabled", true);
  const anim = box.animate(colorChangeFrames, 4000);

  anim.addEventListener("finish", (event) => {
    button.removeAttribute("disabled");
  });
}

CSS will-change will-change

animation-fill-mode: forwards both will-change


Web Proxy Viewer  |  New URL  |  Original Page