After conducting research, I found a helpful tutorial that addresses your question.
Essentially, to achieve the desired outcome, you must specify a JavaScript handler type, animationend
, by utilizing an eventhandler
add function. This function requires the method you want to execute as the second argument, while also indicating the element undergoing animation as the target for the event.
Key components have been annotated within the code
//Obtain reference to the animated element
const animated = document.getElementById("divBoii");
//Include animation event listener along with associated function
animated.addEventListener('animationend', () => {
console.log('Animation ended');
document.getElementById("alert").innerHTML=":)";
});
<head>
<style>
/*Animation assigned to animated div */
@keyframes animation{
0%{
padding:5%;
height:auto;
}
100%{
padding:10%;
height:auto;
}
}
body{
width:100%;
height:100%;
background-color:teal;
}
#divBoii{
margin:40%;
margin-top:10%;
width:10%;
padding:5%;
background-color:green;
}
/*Assign animation to div*/
#divBoii:hover {
animation-name:animation;
animation-duration:3s;
}
</style>
<body><div id="alert">
This will be a smiley face when animation ends
</div>
<div id="divBoii">
Here is some content we are going to animate
</div>
</body>
</head>