Skip to content

Instantly share code, notes, and snippets.

@Visionchen
Created May 11, 2022 08:19
Show Gist options
  • Select an option

  • Save Visionchen/7dd78afe33c8cfded482b823362d62d7 to your computer and use it in GitHub Desktop.

Select an option

Save Visionchen/7dd78afe33c8cfded482b823362d62d7 to your computer and use it in GitHub Desktop.
studing
### 重排 重绘
1. 最小化重绘和重排,比如样式集中改变,使用添加新样式类名 .class 或 cssText 。
2. 批量操作 DOM,比如读取某元素 offsetWidth 属性存到一个临时变量,再去使用,而不是频繁使用这个计算属性;又比如利用 document.createDocumentFragment() 来添加要被添加的节点,处理完之后再插入到实际 DOM 中。
3. 使用 **absolute** 或 **fixed** 使元素脱离文档流,这在制作复杂的动画时对性能的影响比较明显。
4. 开启 GPU 加速,利用 css 属性 transform 、will-change 等,比如改变元素位置,我们使用 translate 会比使用绝对定位改变其 left 、top 等来的高效,因为它不会触发重排或重绘,transform 使浏览器为元素创建⼀个 GPU 图层,这使得动画元素在一个独立的层中进行渲染。当元素的内容没有发生改变,就没有必要进行重绘。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment