原文鏈接:https://www.cnblogs.com/xiaohuochai/p/5777186.html
requestAnimationFrame解決了瀏覽器不知道javascript動畫什么時候開始、不知道最佳循環(huán)間隔時間的問題。它是跟著瀏覽器的繪制走的,如果瀏覽器繪制間隔是16.7ms,它就按這個間隔繪制;如果瀏覽器繪制間隔是10ms, 它就按10ms繪制。這樣就不會存在過度繪制的問題,動畫不會丟幀。
前面的話
與setTimeout和setInterval不同,requestAnimationFrame不需要設置時間間隔。這有什么好處呢?為什么requestAnimationFrame被稱為神器呢?本文將詳細介紹HTML5新增的定時器requestAnimationFrame
引入
計時器一直是javascript動畫的核心技術(shù)。而編寫動畫循環(huán)的關(guān)鍵是要知道延遲時間多長合適。一方面,循環(huán)間隔必須足夠短,這樣才能讓不同的動畫效果顯得平滑流暢;另一方面,循環(huán)間隔還要足夠長,這樣才能確保瀏覽器有能力渲染產(chǎn)生的變化
大多數(shù)電腦顯示器的刷新頻率是60Hz,大概相當于每秒鐘重繪60次。大多數(shù)瀏覽器都會對重繪操作加以限制,不超過顯示器的重繪頻率,因為即使超過那個頻率用戶體驗也不會有提升。因此,最平滑動畫的最佳循環(huán)間隔是1000ms/60,約等于16.6ms
而setTimeout和setInterval的問題是,它們都不精確。它們的內(nèi)在運行機制決定了時間間隔參數(shù)實際上只是指定了把動畫代碼添加到瀏覽器UI線程隊列中以等待執(zhí)行的時間。如果隊列前面已經(jīng)加入了其他任務,那動畫代碼就要等前面的任務完成后再執(zhí)行
requestAnimationFrame采用系統(tǒng)時間間隔,保持最佳繪制效率,不會因為間隔時間過短,造成過度繪制,增加開銷;也不會因為間隔時間太長,使用動畫卡頓不流暢,讓各種網(wǎng)頁動畫效果能夠有一個統(tǒng)一的刷新機制,從而節(jié)省系統(tǒng)資源,提高系統(tǒng)性能,改善視覺效果
特點
【1】requestAnimationFrame會把每一幀中的所有DOM操作集中起來,在一次重繪或回流中就完成,并且重繪或回流的時間間隔緊緊跟隨瀏覽器的刷新頻率
【2】在隱藏或不可見的元素中,requestAnimationFrame將不會進行重繪或回流,這當然就意味著更少的CPU、GPU和內(nèi)存使用量
【3】requestAnimationFrame是由瀏覽器專門為動畫提供的API,在運行時瀏覽器會自動優(yōu)化方法的調(diào)用,并且如果頁面不是激活狀態(tài)下的話,動畫會自動暫停,有效節(jié)省了CPU開銷
使用
requestAnimationFrame的用法與settimeout很相似,只是不需要設置時間間隔而已。requestAnimationFrame使用一個回調(diào)函數(shù)作為參數(shù),這個回調(diào)函數(shù)會在瀏覽器重繪之前調(diào)用。它返回一個整數(shù),表示定時器的編號,這個值可以傳遞給cancelAnimationFrame用于取消這個函數(shù)的執(zhí)行
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">requestID = requestAnimationFrame(callback); </pre>
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">//控制臺輸出1和0
var timer = requestAnimationFrame(function(){
console.log(0);
});
console.log(timer);//1</pre>
cancelAnimationFrame方法用于取消定時器
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">//控制臺什么都不輸出
var timer = requestAnimationFrame(function(){
console.log(0);
});
cancelAnimationFrame(timer);</pre>
也可以直接使用返回值進行取消
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">var timer = requestAnimationFrame(function(){
console.log(0);
});
cancelAnimationFrame(1);</pre>
兼容
IE9-瀏覽器不支持該方法,可以使用setTimeout來兼容
【簡單兼容】
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">if (!window.requestAnimationFrame) {
requestAnimationFrame = function(fn) {
setTimeout(fn, 17);
};
}</pre>
【嚴格兼容】
[](javascript:void(0); "復制代碼")
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">if(!window.requestAnimationFrame){ var lastTime = 0;
window.requestAnimationFrame = function(callback){ var currTime = new Date().getTime(); var timeToCall = Math.max(0,16.7-(currTime - lastTime)); var id = window.setTimeout(function(){
callback(currTime + timeToCall);
},timeToCall);
lastTime = currTime + timeToCall; return id;
}
}</pre>
](javascript:void(0); "復制代碼")
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;">if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}</pre>
應用
現(xiàn)在分別使用setInterval、setTimeout和requestAnimationFrame這三個方法制作一個簡單的進制度效果
【1】setInterval
[](javascript:void(0); "復制代碼")
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;"><div id="myDiv" style="background-color: lightblue;width: 0;height: 20px;line-height: 20px;">0%</div>
<button id="btn">run</button>
<script>
var timer;
btn.onclick = function(){
clearInterval(timer);
myDiv.style.width = '0';
timer = setInterval(function(){ if(parseInt(myDiv.style.width) < 500){
myDiv.style.width = parseInt(myDiv.style.width) + 5 + 'px';
myDiv.innerHTML = parseInt(myDiv.style.width)/5 + '%';
}else{
clearInterval(timer);
}
},16);
} </script></pre>
](javascript:void(0); "復制代碼")
<iframe src="https://demo.xiaohuochai.site/js/requestAnimationFrame/r1.html" frameborder="0" width="320" height="240" style="line-height: 1.5; width: 701px; height: 60px;"></iframe>
【2】setTimeout
[](javascript:void(0); "復制代碼")
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;"><div id="myDiv" style="background-color: lightblue;width: 0;height: 20px;line-height: 20px;">0%</div>
<button id="btn">run</button>
<script>
var timer;
btn.onclick = function(){
clearTimeout(timer);
myDiv.style.width = '0';
timer = setTimeout(function fn(){ if(parseInt(myDiv.style.width) < 500){
myDiv.style.width = parseInt(myDiv.style.width) + 5 + 'px';
myDiv.innerHTML = parseInt(myDiv.style.width)/5 + '%';
timer = setTimeout(fn,16);
}else{
clearTimeout(timer);
}
},16);
} </script></pre>
](javascript:void(0); "復制代碼")
<iframe src="https://demo.xiaohuochai.site/js/requestAnimationFrame/r2.html" frameborder="0" width="320" height="240" style="line-height: 1.5; width: 701px; height: 60px;"></iframe>
【3】requestAnimationFrame
[](javascript:void(0); "復制代碼")
<pre style="margin: 0px; white-space: pre-wrap; word-wrap: break-word; padding: 0px; list-style-type: none; list-style-image: none; font-family: "Courier New" !important; font-size: 12px !important;"><div id="myDiv" style="background-color: lightblue;width: 0;height: 20px;line-height: 20px;">0%</div>
<button id="btn">run</button>
<script>
var timer;
btn.onclick = function(){
myDiv.style.width = '0';
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){ if(parseInt(myDiv.style.width) < 500){
myDiv.style.width = parseInt(myDiv.style.width) + 5 + 'px';
myDiv.innerHTML = parseInt(myDiv.style.width)/5 + '%';
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
}
});
} </script></pre>