requestAnimationFrame基本用法

兼容性处理

1
2
3
4
5
6
requestAnimationFrame = window.requestAnimationFrame || 
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) { setTimeout(callback, 1000 / 60); };

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function animate() {  
// Do whatever
requestAnimationFrame(animate);
// Do something animate

}
//go->
requestAnimationFrame(animate);


requestAnimationFrame(function () {
// Do whatever
requestAnimationFrame(animate);
// Do something animate
})

设定结束

1
2
3
4
5
6
7
8
9
10
11
var globalID;  
function animate() {
// Do whatever
globalID=requestAnimationFrame(animate);
// Do something animate

}
//when ot start
globalID=requestAnimationFrame(animate);
//when to stop
cancelAnimationFrame(globalID);