简体   繁体   English

可见性:隐藏和显示之间的性能差异:无

[英]Performance differences between visibility:hidden and display:none

I want to simplify things in my jQuery Backbone.js web application.我想简化我的 jQuery Backbone.js Web 应用程序中的事情。 One such simplification is the behavior of my menu and dialog widgets.一种这样的简化是我的菜单和对话框小部件的行为。

Previously I created the div boxes of my menus at start and hid them using display: none; opacity:0;以前我在开始时创建了菜单的 div 框并使用display: none; opacity:0;隐藏它们display: none; opacity:0; display: none; opacity:0; . . When I needed a menu, I changed its style to display:block then used the jQuery ui position utility to position the div box (since elements with display:none cannot be positioned) and when it was done, finally changed its style to opacity:1 .当我需要菜单时,我将其样式更改为display:block然后使用 jQuery ui position 实用程序来定位 div 框(因为无法定位带有display:none元素),完成后,最终将其样式更改为opacity:1 .

Now I want to just hide them with visibility:hidden , and when I need one, I use the position utility and then change the style to visibility:visible .现在我只想用visibility:visible visibility:hidden隐藏它们,当我需要一个时,我使用位置实用程序,然后将样式更改为visibility:visible When I begin using this new approach, I will have around 10 div boxes throughout the web application session that are hidden but occupy space, in contrast to the previous div boxes hidden with display:none .当我开始使用这种新方法时,我将在整个 Web 应用程序会话中拥有大约 10 个隐藏但占用空间的 div 框,这与之前使用display:none隐藏的 div 框形成对比。

What are the implications of my new approach?我的新方法有什么影响? Does it effect browser performance in any regard?它会在任何方面影响浏览器性能吗?

display:none; elements are not in the render tree all , so they will perform better at face value.元素不在渲染树 all 中,因此它们的表面价值会更好。

I doubt you will have any real visible performance problems from this though.我怀疑您是否会因此而遇到任何真正可见的性能问题。 If you need opacity: 0 or visibility: hidden because of their functionality , then just use them.如果您需要opacity: 0visibility: hidden因为它们的功能,那么就使用它们。 If you don't need the functionality, then use display: none;如果您不需要该功能,请使用display: none;

If you are toggling between visible and invisible states via javascript then visibility:hidden should be the better performer.如果您通过 javascript 在可见和不可见状态之间切换,那么可见性:隐藏应该是更好的表现。 Seeing as it always takes up the same amount of space in both visible and hidden states it won't cause a reflow of the elements below it every time you make it appear of disappear.因为它在可见和隐藏状态下总是占用相同的空间,所以每次让它看起来消失时,它不会导致它下面的元素回流。 For display:none you are removing it from the flow of the document and then when you set it to display:block you are rerendering it and pushing everything below that element down, essentially laying all that stuff out again.对于 display:none ,您将它从文档流中删除,然后当您将其设置为 display:block 时,您正在重新渲染它并将该元素下方的所有内容向下推,基本上将所有内容重新放置。

But if you are doing something like toggling visible states on button presses then you really should be using what suits your needs rather than what performs better, as the performance differences are negligible in such cases.但是,如果您正在执行诸如在按下按钮时切换可见状态之类的操作,那么您确实应该使用适合您需求的方式而不是性能更好的方式,因为在这种情况下性能差异可以忽略不计。 When you are animating with the dom at around 20 times per second THEN you can worry about the performance of visibility:hidden vs display:none.当您以每秒 20 次左右的速度使用 dom 进行动画处理时,您可能会担心可见性的性能:隐藏与显示:无。

visibility: hidden does not cause a re-flow on the document, while display: none does. visibility: hidden不会导致文档重排,而display: none

display: none : The HTML engine will completely ignore the element and its children. display: none :HTML 引擎将完全忽略该元素及其子元素。 The engine will not ignore elements marked with visibility: hidden , it will do all the calculations to the element and its children, the exception is that the element will not be rendered to the viewport.引擎不会忽略标记为visibility: hidden元素,它将对元素及其子元素进行所有计算,唯一的例外是元素不会呈现到视口。

If the values for position and dimensions properties are needed then visibility: hidden have to be used and you have to handle the white space in the viewport, usually by wrapping that element inside another one with 0 width and height and 'overflow: hidden'.如果需要位置和尺寸属性的值,则必须使用visibility: hidden ,并且您必须处理视口中的空白区域,通常是将该元素包装在另一个宽度和高度为 0 且“溢出:隐藏”的元素中。

display:none will remove the element from the document's normal flow and set the values for position/height/width to 0 on the element and its children. display:none将从文档的正常流中删除元素,并将元素及其子元素的位置/高度/宽度值设置为 0。 When the elements display property is changed to other value than none , it triggers a complete document re-flow, which can be a problem for big documents - and sometimes not-so-big documents being rendered on hardware with limited capabilities.当元素display属性更改为none以外的其他值时,它会触发完整的文档重流,这对于大文档来说可能是一个问题 - 有时在功能有限的硬件上呈现不太大的文档。

display: none is the natural and logical solution to use when hiding elements on the viewport, visibility: hidden should be used as a fallback, where/when needed. display: none是在视口上隐藏元素时使用的自然和合乎逻辑的解决方案, visibility: hidden应该在需要的地方/时间用作后备。

EDIT: As pointed by @Juan, display: none is the choice to go when what you need is to add many elements to the DOM tree.编辑:正如@Juan 所指出的,当您需要向 DOM 树添加许多元素时,可以选择display: none visibility: hidden will trigger a re-flow for each element added to the tree, while display: none will not. visibility: hidden将触发添加到树中的每个元素的重新流,而display: none则不会。

I'm not aware of any performance difference between display:none and visibility:hidden - even if there is, for as little as 10 elements it will be completely negligible.我不知道display:nonevisibility:hidden之间有任何性能差异 - 即使有,对于少至 10 个元素,它也完全可以忽略不计。 Your main concern should be, as you say, whether you want the elements to remain within the document flow, in which case visibility is a better option as it maintains the box model of the element.正如您所说,您的主要关注点应该是您是否希望元素保留在文档流中,在这种情况下, visibility是更好的选择,因为它维护了元素的框模型。

Well, the main performance difference between display: block and visibility: hidden is that if you have a list of, say, 100000 elements, the visibility: hidden won't save you from DOM hanging because it doesn't remove elements from DOM.好吧, display: blockvisibility: hidden之间的主要性能区别在于,如果您有一个包含100000 个元素的列表,那么visibility: hidden不会使您免于DOM 挂起,因为它不会从DOM 中删除元素。 visibility: hidden acts like opacity: 0 + pointer-events: none . visibility: hidden行为就像opacity: 0 + pointer-events: none display: none acts like Element.remove() . display: none作用类似于Element.remove()

Live example: https://jsfiddle.net/u2dou58r/10/实例: https : //jsfiddle.net/u2dou58r/10/

根据个人经验,在一个简单的静态页面上尝试了这两种方法,其中一个表单位于“隐藏”按钮下方, visibility: hidden表现完美,而display: none导致可点击按钮在点击时轻微跳跃,就好像它试图显示隐藏按钮一样一毫秒。

Well, visibility:none still uses the space of the div.好吧,可见性:无仍然使用 div 的空间。 So you could maybe skip the positioning part because its place is already allocated (and by that get a better performance).所以你可以跳过定位部分,因为它的位置已经分配(这样可以获得更好的性能)。

But I somehow guess that you need your display:none approach to allocate space correctly when the "show" event is triggered.但是我以某种方式猜测您需要使用 display:none 方法在触发“显示”事件时正确分配空间。

I think this could be somehow related to this question: CSS Properties: Display vs. Visibility我认为这可能与这个问题有某种关系: CSS Properties: Display vs. Visibility

I'll just quote the interesting part:我只引用有趣的部分:

the element is NEVER removed from the DOM hierarchy.该元素永远不会从 DOM 层次结构中删除。 All block level display 'styles' are completely 'hidden' when using display:none, whereas with visibility:hidden;使用 display:none 时,所有块级显示“样式”都完全“隐藏”,而使用可见性:隐藏; the element itself is hidden but it still occupies a visual space in the DOM.元素本身是隐藏的,但它仍然占据 DOM 中的视觉空间。

So there should be no real difference in regard to browser performance, because both versions are still in the DOM hierarchy.所以在浏览器性能方面应该没有真正的区别,因为两个版本仍然在 DOM 层次结构中。 These properties only affect how an element is displayed in regards to the DOM.这些属性只影响一个元素是如何显示的问候的DOM。

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

相关问题 可见性:隐藏与显示:无与不透明度:0 - visibility:hidden vs display:none vs opacity:0 更改Toggle(); 从显示:无到可见性:隐藏 - Change Toggle(); from Display:none to Visibility:hidden 样式 hidden、block 和 none 之间的区别 - Differences between style hidden, block and none 如果未启用JavaScript,则显示后备广告:无/可见性:隐藏 - Fallback for display:none / visibility:hidden in case javascript is disabled 具有可见性的JQuery Infinite Pulse Function:隐藏而不是显示:无 - JQuery Infinite Pulse Function with visibility:hidden instead of display:none 隐藏元素而不使用 display:none 或 visibility:hidden - Hiding an element without using display:none or visibility:hidden jQuery的hide()和.css('display','none')之间的区别? - Differences between jQuery's hide() and .css('display', 'none')? 浏览器性能显示/可见性 - Browser Performance display/visibility jQuery为什么显示/隐藏使用display:none而不是visible:hidden? - Why does jQuery show/hide use display:none instead of visibility:hidden? 避免隐藏的元素占用空间/无法使visible:true最初显示为:css样式中没有 - avoid hidden element occupying space/not possible to make visibility:true which is initially made display:none in css style
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM