简单的requestAnimationFrame动画

html部分

<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;
            var ele = 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);//调用方法
            }, false);
    </script>

posted @ 2018-04-22 10:40  xue11hua  阅读(92)  评论(0编辑  收藏  举报