当前位置:网站首页>重绘与回流的关系
重绘与回流的关系
2022-07-29 05:10:00 【Try your best】
需知:浏览器使用流式布局模型 (Flow Based Layout)。
浏览器会把HTML解析成DOM,把CSS解析成CSSOM,DOM和CSSOM合并就产生了Render Tree。
有了RenderTree,我们就知道了所有节点的样式,然后计算他们在页面上的大小和位置,最后把节点绘制到页面上。
由于浏览器使用流式布局,对Render Tree的计算通常只需要遍历一次就可以完成,但table及其内部元素除外,他们可能需要多次计算,通常要花3倍于同等元素的时间,这也是为什么要避免使用table布局的原因之一。
一句话:回流必将引起重绘,重绘不一定会引起回流
当Render Tree中部分或全部元素的尺寸、结构、或某些属性发生改变时,浏览器重新渲染部分或全部文档的过程称为回流。
会导致回流的操作:
页面首次渲染
浏览器窗口大小发生改变
元素尺寸或位置发生改变
元素内容变化(文字数量或图片大小等等)
元素字体大小变化
添加或者删除可见的DOM元素
激活CSS伪类(例如::hover)
查询某些属性或调用某些方法
一些常用且会导致回流的属性和方法:
clientWidth、clientHeight、clientTop、clientLeft
offsetWidth、offsetHeight、offsetTop、offsetLeft
scrollWidth、scrollHeight、scrollTop、scrollLeft
scrollIntoView()、scrollIntoViewIfNeeded()
getComputedStyle()
getBoundingClientRect()
scrollTo()
重绘 (Repaint)
当页面中元素样式的改变并不影响它在文档流中的位置时(例如:color、background-color、visibility等),浏览器会将新样式赋予给元素并重新绘制它,这个过程称为重绘。
性能影响
回流比重绘的代价要更高。
有时即使仅仅回流一个单一的元素,它的父元素以及任何跟随它的元素也会产生回流。
现代浏览器会对频繁的回流或重绘操作进行优化:
浏览器会维护一个队列,把所有引起回流和重绘的操作放入队列中,如果队列中的任务数量或者时间间隔达到一个阈值的,浏览器就会将队列清空,进行一次批处理,这样可以把多次回流和重绘变成一次。
当你访问以下属性或方法时,浏览器会立刻清空队列:
clientWidth、clientHeight、clientTop、clientLeft
offsetWidth、offsetHeight、offsetTop、offsetLeft
scrollWidth、scrollHeight、scrollTop、scrollLeft
width、height getComputedStyle()
getBoundingClientRect()
因为队列中可能会有影响到这些属性或方法返回值的操作,即使你希望获取的信息与队列中操作引发的改变无关,浏览器也会强行清空队列,确保你拿到的值是最精确的。
如何避免
CSS
避免使用table布局。
尽可能在DOM树的最末端改变class。
避免设置多层内联样式。
将动画效果应用到position属性为absolute或fixed的元素上。
避免使用CSS表达式(例如:calc())。
JavaScript
避免频繁操作样式,最好一次性重写style属性,或者将样式列表定义为class并一次性更改class属性。
避免频繁操作DOM,创建一个documentFragment,在它上面应用所有DOM操作,最后再把它添加到文档中。
也可以先为元素设置display: none,操作结束后再把它显示出来。因为在display属性为none的元素上进行的DOM操作不会引发回流和重绘。
避免频繁读取会引发回流/重绘的属性,如果确实需要多次使用,就用一个变量缓存起来。
对具有复杂动画的元素使用绝对定位,使它脱离文档流,否则会引起父元素及后续元素频繁回流。
边栏推荐
猜你喜欢

365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions

力扣994:腐烂的橘子(BFS)

About local variables

Longest string without duplicate characters

Application of Huffman tree and Huffman coding in file compression

刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解

Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction

游戏行业弹性计算最佳实践

Cmu15-213 malloc lab experiment record

Occt learning 001 - Introduction
随机推荐
个人学习笔记
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
ClickHouse学习(八)物化视图
365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
167. Sum of two numbers II - enter an ordered array
Clickhouse learning (x) monitoring operation indicators
Clickhouse learning (IX) Clickhouse integrating MySQL
Day 3
Bubble sort c language
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
抽象类与接口
Day 1
Side effects and sequence points
C language n queen problem
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
【C语言系列】— 打印100~200之间的素数
Redirection and files
省市区三级联动(简单又完美)
C语言 一级指针
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing