JavaScript

animation, transition event

presentKey 2024. 6. 19. 20:04

๐ŸŽจ Animation ์ด๋ฒคํŠธ

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

CSS ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์‹œ์ž‘, ์ข…๋ฃŒ, ๋ฐ˜๋ณต๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค.

  • animationstart
  • animationend
  • animationiteration
// react
export default function EventPage() {
  const animationStart = () => console.log('animation start');
  const animationEnd = () => console.log('animation end');
  const animationIteration = () => console.log('animation iteration');

  return (
    <div>
      <h3>animation event</h3>
      <div
        className={styles.box}
        onAnimationStart={animationStart}
        onAnimationEnd={animationEnd}
        onAnimationIteration={animationIteration}
      ></div>
    </div>
  );
}

// css
.box {
  width: 100px;
  height: 100px;
  background-color: #5f9ea0;
  animation: spin 2s;
  animation-iteration-count: 3;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

 

 

๐ŸŽจ Transtion ์ด๋ฒคํŠธ

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

CSS ํŠธ๋žœ์ง€์…˜์ด ์‹œ์ž‘, ์ข…๋ฃŒ, ์ทจ์†Œ๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค.

  • transtionstart
  • transitionend
  • transitioncancel
// react
// transitionEnd ์ด๋ฒคํŠธ ์™ธ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์ด๋ฒคํŠธ๋Š” ์ง์ ‘ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค.
export default function EventPage() {
  const divRef = useRef<HTMLDivElement>(null);
  const transitionStart = () => console.log('transition start');
  const transitionEnd = () => console.log('transition end');
  const transitionCancel = () => console.log('transition cancel');

  useEffect(() => {
    const divElement = divRef.current;
    if (!divElement) return;
    divElement.addEventListener('transitionstart', transitionStart);
    divElement.addEventListener('transitioncancel', transitionCancel);

    return () => {
      divElement.removeEventListener('transitionstart', transitionStart);
      divElement.removeEventListener('transitioncancel', transitionCancel);
    };
  }, []);

  return (
    <div>
      <h3>animation event</h3>
      <div
        className={styles.box2}
        ref={divRef}
        onTransitionEnd={transitionEnd}
      ></div>
    </div>
  );
}

// CSS
.box2 {
  width: 100px;
  height: 100px;
  background-color: #5f9ea0;
  transition: transform 2s ease;
}

.box2:hover {
  transform: scaleX(2);
}