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);
}