简单的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>

猜你喜欢

转载自www.cnblogs.com/aSnow/p/8906012.html