When tigger site updates the layout, it always follow this order:

Javascript trigger style changes, then layout changes then broswer do the paint and composite.

All those five steps should be finished in 60fps, or 16ms. Then users will have a smooth and good user experience.

For "layout", "style" and "composite", please check this site: https://csstriggers.com/

From the site, you can see that, 'transform' and 'opacity' has good preference, because they only trigger "composite", save lot of works for the broswer.

Also you can see that the method for "left", "right", they triggers all "layout", "paint", "composite"

Now let see "style" and "layout".

In general, "style" should happen before "layout", otherwise, broswer need to rerender "layout"->"style"->"layout" all over again, which is a waste for the perfermence.

To see which opreation will cause "layout" recalcuation, please checkout http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html, basiclly, be careful with those:

clientHeight, clientLeft, clientTop, clientWidth, focus(), getBoundingClientRect(), getClientRects(), innerText, offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth, outerText, scrollByLines(), scrollByPages(), scrollHeight, scrollIntoView(), scrollIntoViewIfNeeded(), scrollLeft, scrollTop, scrollWidth

Let's see an example how bad it can affect our site prefermence. Example site

In this bad example, you can see taht Recalculation for "style" -> "layout" -> "style" .... "layout", it repeat number of times.

Let's see the code causes this:

        function firstRun() {
divs.forEach(function(elem, index, arr) {
if (window.scrollY < 200) {
elem.style.opacity = 0.5;
}
})
}

As you can see in a forEach loop, every time you call "scollY" will cause a layout update, then we call "style.opacity" to trigger style update, but after style updated, layout will be updated again because the order, remember?

Let's see how to fix the problem:

        function thirdRun() {
var newWidth = container.offsetWidth;
divs.forEach(function(elem, index, arr) {
elem.style.width = newWidth + "px";
})
}

We can simply move 'layout' update code out of forEach loop. Now the timeline looks much better!

最新文章

  1. 双日历时间段选择控件—daterangepicker(汉化版)
  2. python查找空格和中文
  3. 状态压缩DP
  4. python 判断操作系统类型
  5. inline-block元素的一些坑
  6. Sublime Text 2 插件
  7. BFC / hasLayout
  8. (转)iOS项目的目录结构和开发流程
  9. Struts文件上传
  10. 网站开发进阶(四十四)input type=&quot;submit&quot; 和&quot;button&quot;的区别
  11. js+jquery创建元素
  12. [原创]..\OBJ\gpio.axf: error: L6002U: Could not open file ..\obj\gpio.o: No such file
  13. SpringCloud(8)---zuul权限校验、接口限流
  14. DotNetCore深入了解之二HttpContext类
  15. java-Collection集合、List集合、Vector集合和迭代器Iterator、ListIterator的使用
  16. gdb 使用
  17. 20155219 2016-2017-2 《Java程序设计》第10周学习总结
  18. UT源码_105032014033
  19. Lucene4.x创建索引与3.x的一些不同
  20. JQueryEasyUI easyui-combobox 单击文本区域显示下拉菜单

热门文章

  1. 修改linux内核的启动logo和禁用启动光标【转】
  2. Hdu-5983 2016ACM/ICPC亚洲区青岛站 B.Pocket Cube 模拟
  3. Ajax请求成功但是一直进入error的原因
  4. MATLAB 2018a 下载安装
  5. 5个对话框和FileStream:文件流
  6. wcf 学习程序
  7. Android studio 隐藏toolbar上的app title
  8. Centos7下git服务器及gogs部署
  9. mac 下安装 mariadb
  10. 【seo】title / robots / description / canonical