简体   繁体   English

DOM元素太多

[英]Too Many DOM Elements

I am building a one page webapp and it's starting to get pretty big. 我正在构建一个单页webapp,它开始变得非常大。 There are several components to the app, each one meticulously styled. 应用程序有几个组件,每个组件都经过精心设计。 On average the app has a DOM element count of 1200+. 平均而言,该应用的DOM元素数量为1200+。 I have been warned by my YSlow scan that this is too many, and that I should have no more than 700 DOM elements. 我的YSlow扫描警告我这太多了,我应该有不超过700个DOM元素。

I am usually quite strict and efficient with my markup and I doubt I would be able to trim much off. 我的标记通常非常严格和高效,我怀疑我能够修剪得多。 I tend to use a lot of DOM elements to get the styling exactly right and working cross browser. 我倾向于使用大量的DOM元素来使样式完全正确并且跨浏览器工作。

How can I dramatically cut the number of DOM elements? 如何大幅减少DOM元素的数量? Will I have to load more of the content on demand (ajax) instead on all on page load? 我是否必须按需加载更多内容(ajax)而不是全部加载页面? Does a large amount of DOM elements have a big impact on performance? 大量DOM元素对性能有很大影响吗?

I would love to hear people's experience with this and any solutions you may have... 我很想听听人们对此的体验以及您可能拥有的任何解决方案......

The number of dom elements would only enter into the picture if you're doing a lot of DOM and/or CSS manipulation on the page via Javascript. 如果你通过Javascript在页面上进行大量的DOM和/或CSS操作,那么dom元素的数量只会进入图片。 Scanning for an ID in a page with 50,000 elements is always going to be slower than a page with only 500. Changing a CSS style which is inherited by most of the page will most likely lead to more redrawing/reflowing than it would on a simpler page, etc... 扫描具有50,000个元素的页面中的ID总是比仅有500个页面的页面慢。更改大部分页面继承的CSS样式很可能导致比更简单的页面更多重绘/回流页面等...

The only way to cut element count is to simplify the page. 减少元素数量的唯一方法是简化页面。

We've built a single page web app. 我们构建了一个单页面的Web应用程序。 Initially Yslow worried me as we had 2,000+ DOM objects in the page. 最初Yslow担心我,因为我们在页面中有2,000多个DOM对象。

After some work we got all the other Yslow items to green. 经过一些工作,我们将所有其他Yslow项目变为绿色。 And we ended up living with it(around 1,800 right now) as the app is very fast in various browsers. 我们最终生活在它(现在大约1,800),因为该应用程序在各种浏览器中非常快。

But we don't support IE6 and IE7, and it could be different for these browsers. 但是我们不支持IE6和IE7,这些浏览器可能会有所不同。

How can I dramatically cut the number of DOM elements? 如何大幅减少DOM元素的数量?

By using only those elements that are necessary. 仅使用必要的元素。 If you want an more elaborate advice, post your code. 如果您想要更精细的建议,请发布您的代码。

Will I have to load more of the content on demand (ajax) instead on all on page load? 我是否必须按需加载更多内容(ajax)而不是全部加载页面?

If you want your page to perform better on start-up, you can do that. 如果您希望页面在启动时表现更好,则可以执行此操作。

Does a large amount of DOM elements have a big impact on performance? 大量DOM元素对性能有很大影响吗?

Not necessarily. 不必要。

您可以在用户单击按钮时按需呈现元素,也可以使用Twitter等延迟加载。

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

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