当使用具有潜在大型 JS 库、视图模板、验证、ajax、动画等的非常动态的 UI(想想单页应用程序)时,有哪些策略可以帮助最小化或减少浏览器在重排上花费的时间?
例如,我们知道有很多方法可以完成 DIV 大小的更改,但是否有应该避免的技术(从回流的角度来看)以及浏览器之间的结果有何不同?
这是一个具体的例子:
举一个简单的例子,在调整窗口大小时控制 DIV 大小的 3 种不同方法,应该使用哪些方法来最小化回流?
http://jsfiddle.net/xDaevax/v7ex7m6v/
//Method 1: Pure Javascript
function resize(width, height) {
var target = document.getElementById("method1");
target.setAttribute("style","width:" + width + "px");
target.setAttribute("style", "height:" + height + "px");
console.log("here");
} // end function
window.onresize = function() {
var height = (window.innerHeight / 4);
var width = (window.innerWidth / 4);
console.log(height);
resize(height, width);
}
//Method #3 Jquery animate
$(function() {
$(window).on("resize", function(e, data) {
$("#method3").animate({height: window.innerHeight / 4, width: window.innerWidth / 4}, 600)
});
});