My webpage currently features a setup similar to this.
function wiggle(){
var parent = document.getElementById("wiggle");
var string = parent.innerHTML;
parent.innerHTML = "";
string.split("");
var i = 0, length = string.length;
for (i; i < length; i++) {
parent.innerHTML += "<span style='--n:"+ (100 * i - 10000 + 'ms') + ";'>" + string[i] + "</span>";
}
}
wiggle()
#wiggle span{
animation-delay: var(--n);
animation: wave 2s linear var(--n) infinite forwards running;
position: relative;
}
@keyframes wave{
0% {top: 0px;}
25% {top: -4px;}
50% {top: 0px;}
75% {top: 4px;}
100% {top: 0px;}
}
<h1 id="wiggle">This text should wiggle...</h1>
<h2 id="wiggle">...while this text should not.</h2>
The current setup involves JavaScript splitting the ID of each letter into separate <span>
tags, while CSS assigns animation delays and specifies the height of the animation waves. However, I aim to extend this animation to multiple headers on my page by targeting classes instead of IDs. Unfortunately, changing getElementById
to getElementsByClassName
, as well as modifying #wiggle
to .wiggle
, did not yield the desired outcome, and the animation no longer displays. Is there a way to modify the JavaScript to target classes without disrupting its functionality?