JSFiddle - React, Tailwind, and code Playground

HTML

<div id="test"></div>

CSS

#test {
    width:100px;
    height:100px;
    background:red;
    position:absolute;
    left:0;
    top:0;
}

JavaScript

var f = 0;
var pos = 0;
function MoveBg(){ 
    if(pos == 250) f = -5;
    else if(pos == 0) f = 5;
    pos = pos + f;
    document.getElementById("test").style.left =  pos + "px";
}
//MoveBg()


var i = setInterval(MoveBg ,50);
setTimeout(function(){clearInterval(i)},10000);