Jquery hide() 和 show() 在 google chrome 中运行太慢
我有一个 Web 应用程序在 Chrome 中无法正常运行。在 Firefox 中完美运行。我有一个包含大量列表项的页面,确切地说是 316 个。每个列表项都包含大量 HTML。我的问题是当我想隐藏或显示这些列表项时。
我在 jsFiddle 上有一个测试页面来显示我遇到的问题。我将 HTML 页面精简为一个无序列表来保存所有 316 个列表项。我有两个按钮,单击时只需调用 jQuery 隐藏或显示。同样,它在 Firefox、Opera 甚至 IE 中运行得很快,在 Safari 中也运行得很好,但在 Google Chrome 中可能需要 30 秒以上,这会弹出对话框窗口,询问您是否要终止页面,因为脚本运行时间太长。
的链接
这是 jsFiddle http://jsfiddle.net/oumichaelm/UZCZc/3/ 嵌入/结果/
感谢您的任何意见。 杰姆
I have a web application that doesn't run correctly in chrome. Works perfectly in Firefox. I have a page with a large numbered of list items, 316 to be exact. Each list item contains a large amount of HTML. My problem is when I want to hide or show these list items.
I have a test page on jsFiddle to show the problem I'm having. I stripped down the HTML page to one unordered list to hold all 316 list items. I have two buttons that simply call jQuery hide or show when clicked. Again this runs fast in Firefox, Opera, even IE, pretty well in Safari but in Google Chrome it can take over 30 seconds which brings up the dialog window asking if you want to kill the page because a script is running to long.
Here is the link to jsFiddle
http://jsfiddle.net/oumichaelm/UZCZc/3/embedded/result/
thanks for any input.
jmm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来这与 jQuery 无关,只是 Chrome 隐藏具有大量子元素的父元素的问题。
这只是使用基本的 JavaScript 来隐藏文档准备好的元素:
并且在文档准备好后仍然需要很长时间。
http://jsfiddle.net/petersendidit/UZCZc/10/embedded/result/
为此打开了一个 Chrome 错误:http://code.google。 com/p/chromium/issues/detail?id=71305
Looks like this has nothing to do with jQuery and just is a problem with Chrome hiding an parent element that has a HUGE number of children elements.
This just uses basic javascript to hide the element on document ready:
And it still takes forever after the document is ready.
http://jsfiddle.net/petersendidit/UZCZc/10/embedded/result/
Opened a Chrome bug for this: http://code.google.com/p/chromium/issues/detail?id=71305
隐藏时,从 DOM 中删除元素比使用
hide()
更快。当你
append()
将它返回到 DOM 时,它仍然很慢。一种可能的解决方法是在单击显示按钮时显示前 10 个左右的项目,然后使用
setInterval
逐步显示它们。编辑:发现另一个黑客:
您必须将容器设置为
overflow:hidden
:隐藏时,通过设置
margin-top将该元素移动到最顶部
为一个大负数。显示时,重置其
margin-top
。并且它立即显示和隐藏。
When hiding, removing the element from the DOM is faster than using
hide()
.It is still slow when you
append()
it back to the DOM.A possible workaround is to show the first 10 or so items when the show button is clicked, and then
setInterval
to progressively show them.Edit: Found another hack:
You have to set the container to
overflow: hidden
:When hiding, move that element up to far top, by setting
margin-top
to a big negative number.When showing, reset its
margin-top
.And it shows and hides instantly.
感谢上面的回答,它效果很好并且加快了进程。
然而它并不总是有效 - 当我需要的元素位于列表顶部时效果很好。但是,如果我从列表中间选择某些内容,它不会显示全部内容。
我相信我知道它为什么行为不端。
当一长串元素的值设置为隐藏/显示时,隐藏元素将从流中删除,并按照删除的顺序放置在页面底部。
这使得删除元素的速度非常快。
然而,尝试使它们再次可见对于渲染来说是一件痛苦的事情,因为 chrome 必须记住这些项目所属的顺序,并且似乎要重新计算关联的值。
与大多数其他浏览器不同,该组件的位置不会丢失,因此不会在这种不必要的排序中浪费时间。上面的答案非常有效,因为这避免了 Chrome 的无序问题。
Thanks for the answer above, it works great and speeds up the process.
It doesn't however always work - Works nice when the elements I need are at the top of the list. However, it doesn't show them all if I pick something from the middle of the list.
I believe I know why it misbehaves.
When the value of a long list of elements is set to hide/show the hidden elements are removed from the flow and placed on the bottom of the page in the order they were removed.
This makes removing the elements remarkably fast.
However, trying to make them visible again is a pain on the rendering as chrome has to remember the order in which these items belonged in and seemingly to recompute the values associated.
Other than most other browsers the spot for the component isn't lost so no time is wasted in this unnecessary sorting. The above answer works remarkably well as this avoids Chrome's disordering problem.