If you want to change JavaScript after a CSS Transition or Animation completes, you can use the transitionend or animationend event.
(Itโs rare to use these two properties though; use them only if you need to).
Transitionend
The transitionend event triggers when a transition completes.
document.addEventListener('transitionend', e => { // Do something here})When transitionend occurs, youโre mostly interested in two properties:
event.targetโthe element that was transitionedevent.propertyNameโthe property that was transitioned
document.addEventListener('transitionend', e => { console.log('event.target is ', e.target) console.log('event.propertyName is ', e.propertyName)})
Transitionend for multiple properties
transitionend can be used to detect transitions with multiple properties. It triggers once per property, when the transition for that property completes.
/* CSS */button { background-color: pink; transition: background-color 0.3s ease-out, transform 0.3s ease-out; /* Other properties */}
button.is-highlighted { background-color: goldenrod; transform: translateX(1em);}// Add is-highlighted class to button on clickconst button = document.querySelector('button')button.addEventListener('click', e => e.target.classList.add('is-highlighted'))
// The transitionend eventdocument.addEventListener('transitionend', e => { console.log('event.target is ', e.target) console.log('event.propertyName is ', e.propertyName)})
Animationend
The animationend event triggers when an animation completes. Like transitionend, animationend can only be attached to the document.
Note: if the animation repeats infinitely, animationend will not trigger.
document.addEventListener('animationend', e => { // Do something here})When animationend occurs, youโre mostly interested in two properties:
event.targetโthe element that was animatedevent.animationNameโthe name of the completed animation
document.addEventListener('animationend', e => { console.log('event.target is ', e.target) console.log('event.animationName is ', e.animationName)})
Wrapping up
transitionend and animationend are events you can use to tell if your transition or animation is completes. Theyโre invaluable if you need to sync up your transition or animation with JavaScript.
Exercise
- Create three elements with three different
transition-propertyandtransition-duration. Usetransitionendto remove elements when their transition is completes. - Create three elements with three different animations. Use
animationendto remove elements when their transition is completes.
Create three elements with three different transition-property and transition-duration. Use transitionend to remove elements when their transition completes.
(Iโm just going to create one element for this answer. You can create the other two for practice)
.element { position: relative; height: 100px; width: 100px; background-color: red; transition: transform 0.3s ease-out;}
.element:hover { transform: translateX(100px);}const element = document.querySelector('.element')
element.addEventListener('transitionend', e => { const el = e.currentTarget el.parentNode.removeChild(el)})Create three elements with three different animations. Use animationend to remove elements when their transition completes.
(Again, Iโm going to create one element for this answer. You can create the other two for practice)
@keyframes move { 0% { transform: tranlateX(0); } 100% { transform: translateX(100px); }}
.element { position: relative; height: 100px; width: 100px; background-color: red; animation: 1s move paused;}const element = document.querySelector('.element')
element.addEventListener('click', ev => { ev.currentTarget.style.animationPlayState = 'running'})
element.addEventListener('animationend', e => { const el = e.currentTarget el.parentNode.removeChild(el)})Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson