- Currently, I am facing a situation where I have a
span
node with aninnerText
that I will be changing at runtime. - As I change the
innerText
, theoffsetWidth
of the node will also change based on the length of the futureinnerText
. - In order to handle CSS transitions for both the
opacity
andwidth
of thespan
element, I need to anticipate the futureoffsetWidth
. - It is crucial in this case to smoothly transition both the
opacity
and thewidth
of thespan
element, which is why I cannot change theinnerText
immediately.
Is there a way for me to accurately predict the exact value of the width
?
On a broader scale, is it possible to "simulate" DOM changes to determine the anticipated CSS properties?
Here is a potential starting code snippet:
let h1 = document.getElementsByTagName("h1")[0];
let span = document.getElementsByTagName("span")[0];
h1.addEventListener("click", function() {
if(span.innerText == "Better") {
span.innerText = "Awsome";
} else {
span.innerText = "Better";
}
})
h1 span {
border: 1px solid red;
display: inline-block;
padding: 4px;
transition-property: all;
}
<h1>Hello <span>Awsome</span> World!</h1>