mengtuifrontend / Blog

芦叶满汀洲,寒沙带浅流。二十年重过南楼。柳下系船犹未稳,能几日,又中秋。 黄鹤断矶头,故人今在否?旧江山浑是新愁。欲买桂花同载酒,终不似,少年游。
18 stars 5 forks source link

什么引起重排 #31

Open ajccom opened 4 years ago

ajccom commented 4 years ago

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Element

Box metrics
Scroll stuff
Focus
Also…

getComputedStyle

window.getComputedStyle() will typically force style recalc

window.getComputedStyle() will force layout, as well, if any of the following is true:

  1. The element is in a shadow tree
  2. There are media queries (viewport-related ones). Specifically, one of the following: (source)
    • min-width, min-height, max-width, max-height, width, height
    • aspect-ratio, min-aspect-ratio, max-aspect-ratio
    • device-pixel-ratio, resolution, orientation , min-device-pixel-ratio, max-device-pixel-ratio
  3. The property requested is one of the following: (source)
    • height, width
    • top, right, bottom, left
    • margin [-top, -right, -bottom, -left, or shorthand] only if the margin is fixed.
    • padding [-top, -right, -bottom, -left, or shorthand] only if the padding is fixed.
    • transform, transform-origin, perspective-origin
    • translate, rotate, scale
    • grid, grid-template, grid-template-columns, grid-template-rows
    • perspective-origin
    • These items were previously in the list but appear to not be any longer (as of Feb 2018): motion-path, motion-offset, motion-rotation, x, y, rx, ry

window

Forms

Mouse events

document

Range

SVG

contenteditable

*Appendix

Timeline trace of The Guardian. Outbrain is forcing layout repeatedly, probably in a loop.
Cross-browser
Browsing the Chromium source:

CSS Triggers

CSS Triggers is a related resource and all about what operations are required to happen in the browser lifecycle as a result of setting/changing a given CSS value. It's a great resource. The above list, however, are all about what forces the purple/green/darkgreen circles synchronously from JavaScript.

More on forced layout


Updated slightly Feb 2018. Codesearch links and a few changes to relevant element properties.

来源:https://gist.githubusercontent.com/paulirish/5d52fb081b3570c81e3a/raw/05d5c8aeda72a56ddb1156a08741f138fc11bc09/what-forces-layout.md

NancyFan commented 4 years ago

什么导致重排【译】

在javascript调用以下属性或方法时,将会触发浏览器同步计算样式和布局。这被称为重排(reflow/layout thrashing),也是常见的性能瓶颈。

Element元素

盒子距离相关的度量

滚动内容

焦点

其它

getComputedStyle

window.getComputedStyle() 通常会导致样式的重新计算。

此外,当满足下面任何一种情况时,window.getComputedStyle()都会导致重排:

  1. 元素在shadow tree(影子树)中

  2. 使用媒体查询(viewport相关的一种)。特别是查询下面的任何一个媒体属性时:

    • min-width, min-height, max-width, max-height, width, height
    • aspect-ratio, min-aspect-ratio, max-aspect-ratio
    • device-pixel-ratio, resolution, orientation , min-device-pixel-ratio, max-device-pixel-ratio
  3. 获取下列属性:

    • height, width
    • top, right, bottom, left
    • margin [-top, -right, -bottom, -left,或简写]仅当margin值固定.
    • padding [-top, -right, -bottom, -left,或简写]仅当padding值固定.
    • transform, transform-origin, perspective-origin
    • translate, rotate, scale
    • grid, grid-template, grid-template-columns, grid-template-rows
    • perspective-origin
    • 以及之前被列入进来但是似乎之后不再使用的属性: motion-path, motion-offset, motion-rotation, x, y, rx, ry

window窗口

Forms表单

Mouse events鼠标事件

document文档流

Range对象

SVG

contenteditable

附录

各浏览器中的表现
Chromium源码推荐
CSS Triggers网站

CSS Triggers是一个很好的资源网站,它提供了关于设置或者改变一个CSS值时,会导致浏览器生命周期中进行了什么操作的信息。上面列表中都是关于JavaScript中什么操作引起浏览器同步触发紫色(重排)/绿色(重绘)/深绿色(混合)的。

更多关于重排的文章