Currently, I am in the process of creating an animated effect for a website that involves moving two elements' positions over time and resetting them to their original position. Only one element is displayed at a time, and the animation should run smoothly.
Unfortunately, a CSS-only solution is not feasible because the animation needs to be dynamically generated and synchronized. For the purpose of this question, I'll focus on a simplified example where a box moves to the right. The following discussion will revolve around this specific scenario unless stated otherwise.
The movement is controlled by utilizing the CSS transition property to handle the animation. To reset the element's position instantly, the transition must be removed before reapplying it when needed to start moving again immediately. However, this method doesn't seem to work as expected, which brings us to the issue at hand.
If you examine the Javascript code provided at the end of this question or on the linked JSFiddle, you'll notice that I'm attempting to remove the delay between transitions by using setTimeout. Unfortunately, this implementation introduces a 25ms delay, causing the element to intermittently remain stationary without the desired effect. Increasing the delay somewhat resolves the issue but creates a minor jitter in the animation due to its two-part nature and lack of designed delays.
While this behavior might appear to be a browser bug, testing across Chrome, Firefox 52, and the most recent version of Firefox yielded similar results. I have yet to come across any reported instances of this problem or potential solutions/workarounds. Any assistance in resolving this matter and achieving the intended functionality would be greatly appreciated. :)
Here is a link to the JSFiddle page showcasing the issue.
The relevant markup and code are also provided below:
var box = document.getElementById("box");
//Adjust this value or set it to 0 (eliminating the timeout)
//for optimal intermittent performance.
var delay = 25;
setInterval(function() {
box.style.transition = "none";
box.style.left = "1em";
setTimeout(function() {
box.style.transition = "1s linear";
box.style.left = "11em";
}, delay);
}, 1000);
#box {
width: 5em;
height: 5em;
background-color: cyan;
position: absolute;
top: 1em;
left: 1em;
}
<div id="box"></div>