1 (function(){ 2 var lastTime = 0; requestAnimationFrame = window.requestAnimationFrame; 6 var cancelAnimationFrame = window.cancelAnimationFrame; prefix; ( var i = 0; i < prefixes.length; i++ ) { 11 if ( requestAnimationFrame && cancelAnimationFrame ) { 12 break; 13 } 14 prefix = prefixes[i]; 15 requestAnimationFrame = requestAnimationFrame || window[ prefix + 'RequestAnimationFrame' ]; 16 cancelAnimationFrame = cancelAnimationFrame || window[ prefix + 'CancelAnimationFrame' ] || window[ prefix + 'CancelRequestAnimationFrame' ]; 17 } ( !requestAnimationFrame || !cancelAnimationFrame ) { 21 requestAnimationFrame = function( callback, element ) { 22 var currTime = new Date().getTime(); timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); 25 var id = window.setTimeout( function() { 26 callback( currTime + timeToCall ); 27 }, timeToCall ); 28 lastTime = currTime + timeToCall; 29 return id; 30 }; 31 cancelAnimationFrame = function( id ) { 32 window.clearTimeout( id ); 33 }; 34 } 35 36 window.requestAnimationFrame = requestAnimationFrame; 37 window.cancelAnimationFrame = cancelAnimationFrame; 38 }());
附上<script>调用