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

Using the Web Animations API - API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Using the Web Animations API

Web Animations API JavaScript. c .

In this article

Web Animations API

Web Animations API ( ) JavaScript. API CSS Animations CSS Transitions, . , , , , Window.requestAnimationFrame().

Web Animations API, JavaScript, . DOM-heavy CSS- . CSS, JavaScript . Web Animations API . , !

CSS Animations Web Animations API

Web Animations API -: CSS Animations. CSS Animations , .

CSS

CSS, , , (. Codepen):

Alice Tumbling down the rabbit's hole. [Alice Tumbling down the rabbit's hole.]

, , . . CSS, .

css
#alice {
  animation: aliceTumbling infinite 3s linear;
}

@keyframes aliceTumbling {
  0% {
    color: #000;
    transform: rotate(0) translate3D(-50%, -50%, 0);
  }
  30% {
    color: #431236;
  }
  100% {
    color: #000;
    transform: rotate(360deg) translate3D(-50%, -50%, 0);
  }
}

3 () . @keyframes , 30% ( 9 ), , .

JavaScript

Web Animations API.

Keyframe Object CSS @keyframes :

js
var aliceTumbling = [
  { transform: "rotate(0) translate3D(-50%, -50%, 0)", color: "#000" },
  { color: "#431236", offset: 0.333 },
  { transform: "rotate(360deg) translate3D(-50%, -50%, 0)", color: "#000" },
];

, . CSS. , CSS, Web Animations API . , . , Keyframe 50% .

, , , . , 30%, 50% , : 0,333.

( ). keyframe , Element.animate() , NotSupportedError exception.

, , . , ?

(an AnimationEffectTimingProperties object) :

js
var aliceTiming = {
  duration: 3000,
  iterations: Infinity,
};

, CSS:

: , CSS , -. , - "", JavaScript- . - . , , CSS-, animation-timing-function , - API -

Element.animate() :

js
document.getElementById("alice").animate(aliceTumbling, aliceTiming);

, ( version on Codepen).

The animate() DOM-, CSS. . , , :

js
document.getElementById("alice").animate(
  [
    { transform: "rotate(0) translate3D(-50%, -50%, 0)", color: "#000" },
    { color: "#431236", offset: 0.333 },
    { transform: "rotate(360deg) translate3D(-50%, -50%, 0)", color: "#000" },
  ],
  {
    duration: 3000,
    iterations: Infinity,
  },
);

, , ( ), :

js
document.getElementById("alice").animate(
  [
    { transform: "rotate(0) translate3D(-50%, -50%, 0)", color: "#000" },
    { color: "#431236", offset: 0.333 },
    { transform: "rotate(360deg) translate3D(-50%, -50%, 0)", color: "#000" },
  ],
  3000,
);

play(), pause(), reverse() and playbackRate

CSS- Web Animations API, API . Web Animations API . / ( full code on Codepen):

Playing the growing and shrinking game with Alice. [Playing the growing and shrinking game with Alice.]

, , , . .

, :

js
var nommingCake = document
  .getElementById("eat-me_sprite")
  .animate(
    [{ transform: "translateY(0)" }, { transform: "translateY(-80%)" }],
    {
      fill: "forwards",
      easing: "steps(4, end)",
      duration: aliceChange.effect.timing.duration / 2,
    },
  );

Element.animate() . , , Animation.pause() , :

js
nommingCake.pause();

Animation.play() :

js
nommingCake.play();

, , . :

js
var growAlice = function () {
  // Play Alice's animation.
  aliceChange.play();

  // Play the cake's animation.
  nommingCake.play();
};

, growAlice, :

js
cake.addEventListener("mousedown", growAlice, false);
cake.addEventListener("touchstart", growAlice, false);

, :

playbackRate . , . , playbackRate 1 -1:

js
var shrinkAlice = function () {
  aliceChange.playbackRate = -1;
  aliceChange.play();
};

bottle.addEventListener("mousedown", shrinkAlice, false);
bottle.addEventListener("touchstart", shrinkAlice, false);

Through the Looking-Glass, , , , . , , ( full code on Codepen):

Alice and the Red Queen race to get to the next square in this game. [Alice and the Red Queen race to get to the next square in this game.]

, , . ( ) playbackRate .

js
setInterval(function () {
  // Make sure the playback rate never falls below .4
  if (redQueen_alice.playbackRate > 0.4) {
    redQueen_alice.playbackRate *= 0.9;
  }
}, 3000);

, playbackRate ( )

js
var goFaster = function () {
  redQueen_alice.playbackRate *= 1.1;
};

document.addEventListener("click", goFaster);
document.addEventListener("touchstart", goFaster);

. , ? , ?

, playbackRate, , . CSS CSS, - API, ( !) document.getAnimations() :

js
document.getAnimations().forEach(function (animation) {
  animation.playbackRate *= 0.5;
});

Web Animations API .

, CSS Animations, . , :

js
duration: aliceChange.effect.timing.duration / 2;

, , :

js
var aliceChange = document
  .getElementById("alice")
  .animate(
    [
      { transform: "translate(-50%, -50%) scale(.5)" },
      { transform: "translate(-50%, -50%) scale(2)" },
    ],
    {
      duration: 8000,
      easing: "ease-in-out",
      fill: "both",
    },
  );

8 . :

js
aliceChange.pause();

, , . "" , . , Animation.currentTime 4 , :

js
aliceChange.currentTime = 4000;

. (currentTime), . (aliceChange) Animation.effect , , :

js
aliceChange.currentTime = aliceChange.effect.timing.duration / 2;

keyframe () - aliceChange.effect.timing , ( AnimationEffectTimingReadOnly) AnimationEffectTimingReadOnly.duration. , .

,

js
var drinking = document
  .getElementById("liquid")
  .animate([{ height: "100%" }, { height: "0" }], {
    fill: "forwards",
    duration: aliceChange.effect.timing.duration / 2,
  });
drinking.pause();

.

, .

- API, . , . . , . , , currentTime activeDuration:

js
var endGame = function() {

  // get Alice's timeline's playhead location
  var alicePlayhead = aliceChange.currentTime;
  var aliceTimeline = aliceChange.effect.activeDuration;

  // stops Alice's and other animations
  stopPlayingAlice();

  // depending on which third it falls into
  var aliceHeight = alicePlayhead/aliceTimeline;

  if (aliceHeight <= .333){
    // Alice got smaller!
    ...

  } else if (aliceHeight >= .666) {
    // Alice got bigger!
    ...

  } else {
    // Alice didn't change significantly
    ...

  }
}

: getAnimations() , polyfill .

CSS Animations Transitions () Web Animations API:

, , endGame.

js
// When the cake or runs out...
nommingCake.onfinish = endGame;
drinking.onfinish = endGame;

// ...or Alice reaches the end of her animation
aliceChange.onfinish = endGame;

? Web Animations API : onfinish oncancel.

: .

- API, Firefox Chrome. " " ! API , #WAAPI. , , !


Web Proxy Viewer  |  New URL  |  Original Page