首页 > 代码库 > html5 RequestAnimationFrame 动画Demo
html5 RequestAnimationFrame 动画Demo
<!DOCTYPE html> <html> <head> <title>Script-based animation using requestAnimationFrame</title> <style type="text/css"> div { position: absolute; left: 10px; top:100px; padding: 50px; background: crimson; color: white; } </style> <script type="text/javascript"> var requestId = 0; var startime = 0; var lpos = 0; var elm; function init() { elm = document.getElementById("animated"); } function render() { elm.style.left = ((lpos += 3) % 600) + "px"; requestId = window.requestAFrame(render); } function start() { if (window.performance.now) { startime = window.performance.now(); } else { startime = Date.now(); } requestId = window.requestAFrame(render); } function stop() { if (requestId) window.cancelAFrame(requestId); } // handle multiple browsers for requestAnimationFrame() window.requestAFrame = (function () { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || // if all else fails, use setTimeout function (callback) { return window.setTimeout(callback, 1000 / 60); // shoot for 60 fps }; })(); // handle multiple browsers for cancelAnimationFrame() window.cancelAFrame = (function () { return window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || function (id) { window.clearTimeout(id); }; })(); </script> </head> <body onload="init();"> <div id="animated">Hello there.</div> <button onclick="start()">Start</button> <button onclick="stop()">Stop</button> </body> </html>
参考文献:http://msdn.microsoft.com/zh-cn/library/hh920765(v=vs.85).aspx
html5 RequestAnimationFrame 动画Demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。