I recently studied javascript to improve my pages.
I tried to do a simple exercise in which I change an element every 5 seconds, but when I tried to add a fadein animation using css and apply only for the first time, at the moment I don’t know if my javascript is really not written correctly, or does it have any relation to my css.
Can someone with more knowledge and experience give me some advice?
Ok, so the problem is that I cannot get the animation to repeat as sentences, you can check it here:
var $slogan = document.getElementById("slogan");
var sloganArray = ["This sentence will change every 5 seconds!", "See? I'm changing!", "Knock Knock.", "Who there?", "Heck I don't know!"];
var sloganIndex = 0;
function changeSlogan() {
$slogan.innerHTML = sloganArray[sloganIndex];
++sloganIndex;
if (sloganIndex >= sloganArray.length) {
sloganIndex = 0;
}
}
setInterval(changeSlogan,5000);
#slogan {
margin: 0;
font-family: Arial, sans-serif;
font-size: 20px;
color: #000;
opacity: 1;
transition: opacity .3s ease-out;
-webkit-animation:fadeIn ease-out ;
-moz-animation:fadeIn ease-out ;
animation:fadeIn ease-out;
-webkit-animation-fill-mode:forward;
-moz-animation-fill-mode:forward;
animation-fill-mode:forward;
-webkit-animation-duration: 1.5s;
-moz-animation-duration: 1.5s;
animation-duration: 1.5s;
-webkit-animation-delay: 4.9s;
animation-delay: 4.9s;
animation-iteration-count: infinite;
}
<h3 id="slogan"><em>This is a dynamic sentence!</em></h3>
Run codeThank you for your time!