Every time I click the button I want the "Message" animation to play. At the moment it only plays on the first click. With each further click, the animation is no longer played.
The problem is that the d-none
class is removed, but is no longer added. This is my current solution. What do I need to change to make the animation appear every time the button is clicked?
function showMessage() {
let message = document.getElementById('child');
if(message.classList.contains('d-none')){
message.classList.remove('d-none');
}
//message.classList.add('d-none');
}
.parent {
position: relative;
background-color: #000;
width: 600px;
height: 150px;
}
@keyframes msgSuccess {
50% {opacity: 1;}
100% {opacity: 0;}
}
.child {
position: absolute;
width: 100%;
height: 50px;
background-color: #ccc;
animation-name: msgSuccess;
animation-duration: 3s;
opacity: 0;
}
.d-none {
display: none;
}
<div id="parent" class="parent">
<div id="child" class="child d-none">Message</div>
</div>
<button onclick="showMessage()">Click Me</button>