I am trying to replicate a marquee tag using CSS3 animation and I'd like to call a function when the state of the animation changes from running to paused or initial.
HTML:
<div class='animationBackground'><p id="marqueeText">Scrolling Text Goes Here</p></div>
<div id="animationState">Animation State</div>
<button id='stop' type"button" onclick=stopInterval()>Stop Logging</button>
CSS:
@keyframes marquee
{
0% { transform: translate(0%, 0); }
100% { transform: translate(-200%, 0);}
}
p {
margin-left: 100%;
padding-inline-end: 50px;
display: inline-block;
white-space: nowrap;
color: #ffffff;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 30pt;
z-index: 10;
animation: marquee 25s linear 0s 1
}
.animation{
width: 100%;
background-color: darkblue;
vertical-align: bottom;
}
JavaScript:
var myVar = setInterval(myTimer, 5000);
function myTimer() {
var marqueeText = document.getElementById('marqueeText');
var animationState = document.getElementById('animationState');
animationState.innerHTML = marqueeText.style.animationPlayState;
console.log(marqueeText.style.animationPlayState);
if(marqueeText.style.animationPlayState == "running"){
doSomething();
}
}
function stopInterval(){
clearInterval(myVar);
}
The line below doesn't yield anything:
animationState.innerHTML = animatedText.style.animationPlayState;
nor does this one. I get a blank <div> and the console also doesn't print anything.
console.log(animatedText.style.animationPlayState);
Is it possible to get any of the states so as to manipulate them using Javascript? e.g running|paused|initial|inherit using the doSomething() function.
element.styleonly allows you to read styles that where explicitly set before (style attribute, or JS) - but that is not the case anywhere in your code. Try getComputedStyle instead.