Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重绘和回流 #5

Open
sihai00 opened this issue Jul 19, 2019 · 0 comments
Open

重绘和回流 #5

sihai00 opened this issue Jul 19, 2019 · 0 comments
Assignees
Labels

Comments

@sihai00
Copy link
Owner

sihai00 commented Jul 19, 2019

定义

重绘:不影响布局的改变(color、background-color等等)
回流:影响布局的改变(border、padding、margin等等)

回流必定会发生重绘,重绘不一定会引发回流

浏览器优化

浏览器通过维护队列,一定时间(1000 / 60 = 16.6ms)内执行更新,清空队列。为了获取最新的值,以下属性会让浏览器的强制提前更新,导致渲染,也就触发回流。

  • offsetTop、offsetLeft、offsetWidth、offsetHeight
  • scrollTop、scrollLeft、scrollWidth、scrollHeight
  • clientTop、clientLeft、clientWidth、clientHeight
  • width、height
  • getComputedStyle()
  • getBoundingClientRect()

减少重绘和回流

对于css

  1. 避免table
  2. 多条css修改变为单个class修改
  3. 动画脱离文档流(position:absolute或fixed)
  4. css3硬件加速

对于js

  1. 避免频繁操作dom
  2. 合并多次dom操作
  3. 缓存dom值

参考

介绍下重绘和回流(Repaint & Reflow),以及如何进行优化

@sihai00 sihai00 added the 性能 label Jul 19, 2019
@sihai00 sihai00 self-assigned this Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant