前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >触发浏览器回流的属性方法一览表

触发浏览器回流的属性方法一览表

作者头像
Tiffany_c4df
发布2019-09-04 15:35:09
1.5K0
发布2019-09-04 15:35:09
举报
文章被收录于专栏:前端小课堂前端小课堂

下列的所有属性、方法,在读取或执行的同时,将会导致浏览器同步地计算样式和布局。这种行为又叫做回流,也是常见的性能瓶颈。

元素类

元素测量

elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight elem.getClientRects(), elem.getBoundingClientRect()

滚动相关

elem.scrollBy(), elem.scrollTo() elem.scrollIntoView(), elem.scrollIntoViewIfNeeded() elem.scrollWidth, elem.scrollHeight elem.scrollLeft, elem.scrollTop 除了读取,设置也会触发

聚焦

elem.focus() 会触发两次强制布局

其他

elem.computedRole, elem.computedName elem.innerText getComputedStyle

window.getComputedStyle() 调用通常会导致样式的重新计算,并且,当满足下列条件时,会触发强制布局:

元素属于一颗影子树中 出现下列任意一个媒体查询时: 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

所获取的属性是下列之一时:

height, width top, right, bottom, left margin [-top, -right, -bottom, -left, 或简写形式] 仅在数值是定值时 padding [-top, -right, -bottom, -left, 或简写形式] 仅在数值是定值时 transform, transform-origin, perspective-origin translate, rotate, scale grid, grid-template, grid-template-columns, grid-template-rows perspective-origin window

window.scrollX, window.scrollY window.innerHeight, window.innerWidth window.getMatchedCSSRules() 仅会导致样式重新计算

表单:

inputElem.focus() inputElem.select(), textareaElem.select()

鼠标事件:

mouseEvt.layerX, mouseEvt.layerY, mouseEvt.offsetX, mouseEvt.offsetY document

doc.scrollingElement 仅会导致样式重新计算

Range:

range.getClientRects(), range.getBoundingClientRect()

SVG

可以参见 Tony Gentilcore’s 2011 Layout Triggering List

contenteditable

很多行为都会触发,包括复制粘贴图像进去

附录

在文档发生改变或布局、样式失效时会导致回流的消耗。通常,这是因为DOM发生了改变(类的修改,节点的增加、删除,甚至是添加一个伪类如 :focus);

如果需要强制布局,样式首先会被重新计算。所以强制布局会导致这两种操作的发生。它们所消耗的性能取决于当时的内容或者情况,但通常来说两者所消耗的性能都是相似的; 一些简单的解决办法:

  • 避免在 for 循环中强制布局以及更改DOM
  • 使用开发工具分析产生影响的代码
  • 批量读写DOM(使用FastDOM或者其他的虚拟DOM实现)

上述数据在各个浏览器中的表现:

Chromium 源码

CSS Triggers

CSS Triggers 提供了一个很好的资源,里面记载了关于设置或者改变一个CSS数值时,浏览器内需要做什么操作的信息。

更多关于强制布局的信息

  • Avoiding layout thrashing — Web Fundamentals
  • Fixing Layout thrashing in the real world | Matt Andrews
  • Timeline demo: Diagnosing forced synchronous layouts - Google Chrome
  • Preventing ‘layout thrashing’ | Wilson Page
  • wilsonpage/fastdom
  • Rendering: repaint, reflow/relayout, restyle / Stoyan
  • We spent a week making Trello boards load extremely fast. Here’s how we did it. - Fog Creek Blog
  • Minimizing browser reflow | PageSpeed Insights | Google Developers
  • Optimizing Web Content in UIWebViews and Websites on iOS
  • Accelerated Rendering in Chrome
  • web performance for the curious
  • Jank Free
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 元素类
    • 元素测量
      • 滚动相关
        • 聚焦
          • 其他
            • 表单:
              • 鼠标事件:
                • Range:
                  • SVG
                    • contenteditable
                    • 附录
                    • Chromium 源码
                    • CSS Triggers
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档