Web animations API 사용하기 (animate)

웹 애니메이션을 별도의 라이브러리 설치 없이 사용할 수 있다.

내장된 Web api를 사용하여 간단한 애니메이션을 구현할 수 있다.

 

  • HTML
<div>
  <h1>title</h1>
  <p class="phrase">lorem ippsum</p>
</div>
  • CSS
.phrase {
  background: #ff99ff;
  text-align: center;
  padding: 10px;
}
  • Javascript
const tag = document.querySelector('.phrase');

tag.addEventListener('click', () => {
  tag.animate(
    [
      {transform: 'translateY(0px)'},
      {transform: 'translateY(15px)'},
      {transform: 'translateY(0px)'},
    ],
    {
      duration: 1500,
      iterations: Infinity,
      direction: 'normal',
      easing: 'ease',
    },
  );
});

p태그를 클릭하면, 애니메이션이 실행되는 것을 확인할 수 있다.

 

 

⛈ 참고 자료

https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API

 

Web Animations API - Web APIs | MDN

The Web Animations API allows for synchronizing and timing changes to the presentation of a Web page, i.e. animation of DOM elements. It does so by combining two models: the Timing Model and the Animation Model.

developer.mozilla.org

https://developer.mozilla.org/en-US/docs/Web/API/Element/animate

 

Element: animate() method - Web APIs | MDN

The Element interface's animate() method is a shortcut method which creates a new Animation, applies it to the element, then plays the animation. It returns the created Animation object instance.

developer.mozilla.org