简体   繁体   English

可见性是否影响DOM操作性能?

[英]Does visibility affect DOM manipulation performance?

IE7/Windows XP IE7 / Windows XP

I have a third party component in my page that does a lot of DOM manipulation to adjust itself each time the browser window is resized. 我的页面中有第三方组件,每次调整浏览器窗口大小时都会执行大量DOM操作以调整自身。

Unfortunately I have little control of what it does internally and I have optimized everything else (such as callbacks and event handlers) as much as I can. 不幸的是,我几乎无法控制它在内部的作用,而且我已经尽可能地优化了其他所有内容(例如回调和事件处理程序)。 I can't take the component off the flow by setting display:none because it fails measuring itself if I do so. 我不能通过设置display:none来使组件脱离流程,因为如果我这样做,它将无法自我测量。

In general, does setting visibility of the container to invisible during the resize help improve DOM rendering performance? 通常,在调整大小期间将容器的可见性设置为不可见有助于提高DOM呈现性能吗?

Caveat: I have not specifically tested this with IE7, but I am reasonably confident based on what I know about its DOM manipulation model. 警告:我没有用IE7对此进行过专门测试,但根据我对DOM操作模型的了解,我有理由相信。

Changing CSS properties (whether display: none or visibility: hidden or what-have-you) will not affect the performance of DOM manipulation in any version of any browser I've worked with. 更改CSS属性(无论是display: none还是visibility: hidden what-have-you)都不会影响我使用的任何浏览器版本中DOM操作的性能。 The primary way to improve the speed of DOM manipulation is to remove the node(s) you will be working with from the document tree, performing your manipulations, and adding them back in. This involves keeping track of their succeeding sibling nodes, if any (for use with insertBefore ), which can become complex if you're working with nodes which are scattered around the document. 提高DOM操作速度的主要方法是从文档树中删除您将要使用的节点,执行操作并重新添加它们。这包括跟踪其后续的兄弟节点,如果有的话(与insertBefore一起使用),如果您正在处理散布在文档周围的节点,这可能会变得复杂。

One technique I have seen when performing a large number of DOM manipulations all at once is to get a list of the body element's children, remove them, perform your manipulations (wherever they fall in the document tree), and then reappend the body's child nodes. 我在一次执行大量DOM操作时看到的一种技术是获取body元素的子元素列表,删除它们,执行操作(无论它们落在文档树中的哪个位置),然后重新获取body的子节点。 Depending on how long your DOM manipulations take (which is itself partially dependent on the speed of your visitor's computer!), this can produce a noticeable flicker. 根据您的DOM操作需要多长时间(这本身部分取决于访问者计算机的速度!),这会产生明显的闪烁。 This is why sites manipulating content via AJAX will usually replace any temporarily-removed content with a "spinner" or loading screen. 这就是为什么通过AJAX操作内容的网站通常会用“微调器”或加载屏幕替换任何临时删除的内容。

I'm not certain, but removing it from the active document's DOM then manipulating it does improve performance. 我不确定,但是从活动文档的DOM中删除它然后操作它确实提高了性能。 After all manipulating is done, attach it back to the document's DOM. 完成所有操作后,将其附加回文档的DOM。 Think of it sort of like video buffer swapping. 可以认为它有点像视频缓冲区交换。

I've just tested this using a button-dialog situation I'm having. 我刚刚使用按钮对话框测试了这个。 Basically, I copied one long dialog many times until I have a 10000-line file. 基本上,我多次复制一个长对话框,直到我有10000行文件。

html: HTML:

<div class="no-visible dialog">
    ....
</div>

css: CSS:

.no-visible {
    visibility: hidden;
    animation:....
    ....
}

Conslusion: Conslusion:

It slowed down the computer a lot with all the JS and CSS, and all my css animations were gone. 使用所有的JS和CSS,它大大减慢了计算机的速度 ,我所有的css动画都消失了。 Especially when a button is clicked and JS have to find to corresponding dialog to display, it lagged. 特别是当单击按钮并且JS必须找到要显示的相应对话框时,它会滞后。

Solution: 解:

Put the dialogs in another html (dialogs.html) and load the necessory when needed. 将对话框放在另一个html(dialogs.html)中,并在需要时加载必需品。

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

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