How can I visualize that reading element.offsetWidth causes a recalc/reflow

爷,独闯天下 提交于 2019-12-08 01:54:57

问题


It's written everywhere that reading element.offsetWidth causes a recalculation (or even a reflow?) of the element's dimensions.

However, I'm struggling with making this effect visible.

In chrome I would expect I would be able to make it visible with 3 simple steps:

  1. open the Chrome dev tools
  2. Go to the element tab and select an element that you want to cause a recalc/reflow on
  3. Go to the timeline tab and start recording
  4. Go to the console and type $0.offsetWidth

Now if I go to the timeline tab I would assume to see a reflow drawn. However, I see nothing. So I must have gotten something wrong.


回答1:


After some more research, I think I can answer my own question. It's not that reading offsetWidth directly causes a reflow. It's more that it causes the browser to immediately perform all actions that are stacked in the rendering queue. That in turn can mean multiple reflows if you organize your reading and writing to the DOM in the wrong order.



来源:https://stackoverflow.com/questions/18084135/how-can-i-visualize-that-reading-element-offsetwidth-causes-a-recalc-reflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!