I am working on a game that needs an animation to play when I call a function. The function moves two divs slowly up and down similarly to a slot machine. Here are some animation frames:
Start:
In-between:
End:
This is being displayed with html and CSS. Here is the animation code:
Html
<div id="day-swapper-contain" onclick="day_switch()">
<div class="day-swapper-inner-green center">
<div>Good</div>
</div>
<div class="day-swapper-inner-red center">
<div>Bad</div>
</div>
</div>
CSS
#day-swapper-contain{
width: 15%;
overflow: hidden;
height: 75px;
}
.day-swapper-inner-red, .day-swapper-inner-green{
color: var(--text-color);
text-align: center;
font-size: 4rem;
line-height: 5rem;
height: 100%;
width: 100%;
border: var(--bg-color) solid 5px;
justify-content: space-evenly;
position: relative;
animation-name: good-bad;
animation-duration: 3s;
animation-timing-function: ease-in-out;
animation-play-state: paused;
}
.day-swapper-inner-green{
background-color: green;
}
.day-swapper-inner-red{
background-color: red;
}
@keyframes good-bad{
0% {
top: 0px;
}
100% {
top: -75px;
}
}
Here is the JavaScript I am using to trying to play it at a function call.
let good_day = document.querySelector('.day-swapper-inner-green');
let bad_day = document.querySelector('.day-swapper-inner-red');
let good_day_state = true;
function day_switch(){
state = !good_day_state;
if (state){
good_day.style.animationDirection = 'reverse';
bad_day.style.animationDirection = 'reverse';
good_day.style.animationPlayState = 'running';
bad_day.style.animationPlayState = 'running';
} else {
good_day.style.animationDirection = 'normal';
bad_day.style.animationDirection = 'normal';
good_day.style.animationPlayState = 'running';
bad_day.style.animationPlayState = 'running';
}
return state;
}
Is there anything I am doing wrong?
Is there any functions I should know to get this to work?
Is there any code you could show me to help me with my implementation?
All help is appreciated.