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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。