I want to change the text as per the code. Here other span tags are also getting affected by changing of this text. How do I make the other span tags be unaffected?
var para_values = [
{
content: "BRAND "
},
{
content: "MISSION"
}
];
function showVal(index) {
var val = document.getElementsByClassName("header-para")[0];
console.log(val);
val.innerHTML = para_values[index].content;
}
showVal(0); // called for visibility on the screen
// function for changing the content
function textChange() {
var index = 0;
var intrvl = setInterval(() => {
index = index + 1;
if (index == para_values.length) {
index = 0;
}
showVal(index)
}, 4000)
}
textChange();
.header-para {
height: 60px;
background-color: rgb(51, 235, 198);
}
.text-overlay span {
color: black;
font-size: 56px;
}
<div class="text-overlay">
<span>Your</span> <span class="header-para"></span><span>Understand</span>
</div>
2
Answers
To target the various
<span>
elements use CSS selectors withdocument.querySelector('#my-span')
.For my answer:
I gave each span a display inline block so I can control it as spans are typically inline and you are limited to what you can do with it.
I gave the target span a set with and text-align:center so the text will be aligned within that span.
Then I created a CSS class for reveal that has an animation on it that slides down.
Then I add that class in your showval function. I also used an animation end event listener to remove the class from the span after each animation is completed. So that way it will be restarted everytime I add the class to the span.
Also I switched to using querySelector instead of getElementsByClassName.