I encountered a significant challenge while delving into CSS animation.
My goal is to create a "transform: translate" animation that displays text overflowing the content width as depicted in the image below.
https://i.stack.imgur.com/sRF6C.png
See it in action (Youtube video link)
var div1 = document.getElementById("div1");
var cont = document.getElementById("content");
var inf = document.getElementById("inf");
inf.innerHTML = "<p> Does the text inside h1 cut off? : " + (cont.offsetWidth < cont.scrollWidth) +
"<br><b>Length of text overflow</b>: " + (cont.offsetWidth - cont.scrollWidth) +
"<br> h1 width: " + (cont.offsetWidth) + ", h1's text length: " + (cont.scrollWidth) + "</p>";
div1.style.backgroundColor = "#A13DAF";
cont.style.webkitAnimationName = "moving";
cont.style.animationName = "moving";
cont.style.animationDuration = "3s";
cont.style.webkitAnimationDuration = "3s";
cont.style.animationTimingFunction = "linear";
cont.style.webkitAnimationTimingFunction = "linear";
#div1 {
margin: 10px;
width: 300px;
background: rgb(40, 40, 40);
white-space: nowrap;
overflow: hidden;
}
#content {
color: white;
width: 100%;
}
@keyframes moving {
0% {
transform: none;
-webkit-transform: none;
}
65% {
transform: translate( calc(#content.scrollWidth - #content.offsetWidth), 0);
-webkit-transform: translate( calc(#content.scrollWidth - #content.offsetWidth), 0);
transform: translate( -668px, 0);
-webkit-transform: translate( -668px, 0);
}
100% {
transform: translate( -668px, 0);
-webkit-transform: translate( -668px, 0);
}
}
<div id="div1">
<h1 id="content">
The only thing that matters now is everything You think of me
</h1>
</div>
<div id="inf"></div>
Inspired by the above code,
I am aiming to devise a mechanism where the text within the content (h1
) shifts left precisely by the amount of overflow. Yet, I'm struggling to reference values of content directly in CSS.
Does a method exist to adjust a CSS value based on another element's specific value without relying on JavaScript??
I am striving to minimize reliance on Javascript for altering keyframes despite its potential complexity.
Your guidance is greatly appreciated.