To achieve text scroll playback, it can be implemented using CSS3 animation and controlled by Js. Since there are limitations when using CSS animation to control the offset, it is usually implemented using Js.
Using the CSS animation method, the position: relative property is used in conjunction with the left property to control the relative offset of the text element from the left side. The main issue with this method is that left is relative to the width of the parent element when set to 100%, so the value of this property depends on the width of the parent element and the width of the text content itself.
Using the CSS animation method, the transform: translateX() property is used to control the relative offset of the text element from the left side. This method also has the same issue mentioned above.
With JavaScript, we can achieve seamless scrolling by duplicating the element and placing it behind the original element. When the first element finishes scrolling, we immediately reset its position.
<!DOCTYPEhtml><html><head><title>Javascript</title><styletype="text/css">.container{display: flex;border: 1px solid #eee;overflow: hidden;text-overflow: ellipsis;width: 150px;}.text{padding: 0 5px;white-space:nowrap;}</style></head><body><divclass="container"><divclass="text">Tick tock, tick tock, tick tock</div><div></body><scripttype="text/javascript">(function(win, doc){const container = doc.querySelector(".container");// Container elementconst textNode = doc.querySelector(".container > .text");// Text element container.appendChild(textNode.cloneNode(true));// Duplicate the element and place it behindconst textWidth = textNode.offsetWidth;// Get the width of the text elementlet count = container.offsetWidth;// Initialize the left offset to the size of the containerconstloop=()=>{if(count <=-textWidth){// If the text offset exceeds the width of a text element, reset it textNode.style["margin-left"]=0;// Reset count =0;// Reset} textNode.style["margin-left"]=`${count--}px`;// Continue moving to the left window.requestAnimationFrame(()=>loop());// Recursive animation call} window.requestAnimationFrame(()=>loop());// Start the animation// requestAnimationFrame pauses rendering when the page is not active, so it will continue from where it left off when the page is activated. setInterval needs to be cleared and reset using the visibilitychange event listener.})(window, document);</script></html>