标签:style http io ar color os sp java for
<!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
标签:style http io ar color os sp java for
原文地址:http://my.oschina.net/ososchina/blog/350452