简体   繁体   English

何时在DOM环境中进行重排?

[英]When does reflow happen in a DOM environment?

What kinds of activities will trigger reflow of web page with DOM? 什么类型的活动会触发带有DOM的网页重排?

It seems there are different points of view. 似乎有不同的观点。 According to http://www.nczonline.net/blog/2009/02/03/speed-up-your-javascript-part-4/ , it happens 根据http://www.nczonline.net/blog/2009/02/03/speed-up-your-javascript-part-4/ ,它发生了

  • When you add or remove a DOM node. 添加或删除DOM节点时。
  • When you apply a style dynamically (such as element.style.width="10px"). 动态应用样式时(例如element.style.width =“10px”)。
  • When you retrieve a measurement that must be calculated, such as accessing offsetWidth, clientHeight, or any computed CSS value (via getComputedStyle() in DOM-compliant browsers or currentStyle in IE). 当您检索必须计算的度量时,例如访问offsetWidth,clientHeight或任何计算的CSS值(通过DOM兼容浏览器中的getComputedStyle()或IE中的currentStyle)。

However, according to http://dev.opera.com/articles/view/efficient-javascript/?page=3 , taking measurement triggers reflow only when there is already reflow action queued. 但是,根据http://dev.opera.com/articles/view/efficient-javascript/?page=3 ,只有当已经排队的重排操作时,测量才会触发重排。

Does anybody have any more ideas? 有没有人有更多的想法?

Both articles are correct. 两篇文章都是正确的。 One can safely assume that whenever you're doing something that could reasonably require the dimensions of elements in the DOM be calculated that you will trigger reflow. 人们可以放心地假设,无论何时你做了一些可能合理要求计算DOM中元素尺寸的东西,你就会触发回流。

In addition, as far as I can tell, both articles say the same thing. 另外,据我所知,两篇文章都说同样的话。

The first article says reflow happens when: 第一篇文章称回流发生在:

When you retrieve a measurement that must be calculated , such as accessing offsetWidth , clientHeight , or any computed CSS value (via getComputedStyle() in DOM-compliant browsers or currentStyle in IE), while DOM changes are queued up to be made. 当您检索必须计算的度量时 ,例如访问offsetWidthclientHeight或任何计算的CSS值(通过DOM兼容的浏览器中的getComputedStyle()或IE中的currentStyle),同时DOM更改排队等待。

The second article states: 第二篇文章指出:

As stated earlier, the browser may cache several changes for you, and reflow only once when those changes have all been made. 如前所述,浏览器可能会为您缓存多个更改,并且只有在完成所有更改后才重排一次。 However, note that taking measurements of the element will force it to reflow , so that the measurements will be correct. 但是,请注意, 对元素进行测量会强制它进行回流 ,因此测量结果是正确的。 The changes may or may not not be visibly repainted, but the reflow itself still has to happen behind the scenes. 这些变化可能会或可能不会明显重新粉刷,但回流本身仍然必须在幕后进行。

This effect is created when measurements are taken using properties like offsetWidth , or using methods like getComputedStyle . 使用offsetWidth等属性或使用getComputedStyle等方法进行测量时,会生成此效果。 Even if the numbers are not used, simply using either of these while the browser is still caching changes, will be enough to trigger the hidden reflow. 即使没有使用这些数字,只需在浏览器缓存更改时使用其中任何一个,就足以触发隐藏的重排。 If these measurements are taken repeatedly, you should consider taking them just once, and storing the result, which can then be used later. 如果重复进行这些测量,您应该考虑只进行一次,并存储结果,然后可以使用。

I take this to mean the same thing they said earlier. 我认为这与他们之前所说的相同。 Opera will try its hardest to cache values and avoid reflow for you, but you shouldn't rely on its ability to do so. Opera会尽最大努力缓存值并避免重排,但你不应该依赖它的能力。

For all intents and purposes just believe what they both say when they say that all three types of interactions can cause reflow. 对于所有意图和目的,当他们说所有三种类型的相互作用都可能导致回流时,只要相信他们所说的话。

Cheers. 干杯。

Look at the "Rendering triggered by Property Read Access" section of Understanding Internet Explorer Rendering Behaviour , where the following code in IE will cause rendering activity. 查看了解Internet Explorer渲染行为的“由属性读取访问触发的渲染”部分,其中IE中的以下代码将导致渲染活动。

function askforHeight () {
  $("#lower").height();  
}
document.body.style.display = 'none';
document.body.style.display = 'block';

This often solves those incomprehensible layout bugs. 这通常可以解决那些难以理解的布局错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM