Simple requestAnimationFrame animation

html part

<div id="test" style="width:1px;height:17px;background:#0f0;">0%</div>
    <input type="button" value="Run" id="run"/>
    <script>
        window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
            var start = null;
            handhave= document.getElementById("test");
            var progress = 0;
            function step(timestamp) {
                progress += 1;
                ele.style.width = progress + "%";
                ele.innerHTML=progress + "%";
                if (progress < 100) {
                    setTimeout(function(){
                        requestAnimationFrame(step);
                    },50);
                    
                }
            }
            requestAnimationFrame(step);
            document.getElementById("run").addEventListener("click", function() {
                ele.style.width = "1px";
                progress = 0;
                requestAnimationFrame(step); // call method 
            }, false );
     </ script >

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324617187&siteId=291194637