简体   繁体   English

获取元素的offsetHeight会产生副作用吗?

[英]Does getting the offsetHeight of an element have a side effect?

In the code for Bootstrap collapse, in the hide() method, I see the following line: 在Bootstrap collapse的代码中,在hide()方法中,我看到以下行:

this.$element[dimension](this.$element[dimension]())[0].offsetHeight

I don't understand what the point of the .offsetHeight at the end is unless it has a side effect, because it's not being assigned to anything. 我不明白.offsetHeight最后的意思是什么,除非它有副作用,因为它没有分配给任何东西。 Does it have a side effect? 它有副作用吗?

Some old browsers like old versions of IE had the problem of sometimes not reflowing (re-rendering the presentation) after you performed some actions. 一些老版本的IE浏览器在执行某些操作后遇到了有时无法回流(重新呈现演示文稿)的问题。

Mearly querying some properties like offsetHeight forces the DOM to recalculate and redraw the objects on the screen. Mearly查询像offsetHeight这样的属性offsetHeight强制DOM重新计算并重绘屏幕上的对象。

So, the side effect is forcing a reflow (redraw) of the screen. 因此,副作用是强制屏幕重排(重绘)。 Quirky, but an old trick for old browsers. 古怪,但旧浏览器的老技巧。

Here is a question where this is suggested as a solution for an old version of Google Chrome where it did not work properly without it. 这是一个问题,建议将其作为旧版Google Chrome 的解决方案 ,如果没有它,它将无法正常运行。

这是来自bootstrap团队的有用评论:

if (doAnimate) this.$backdrop[0].offsetWidth // force reflow

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

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