切换显示是否更好:无;分别在几个相关的项目上,或者将它们包装在另一个 div 中并切换它?
假设我有三个 div 元素,分别具有类 a
、b
和 c
。我想使用 css 在 none
和 block
之间切换这些元素的显示,具体取决于父元素是否具有 current
类> 或悬停
。这些元素总是一起切换,而不是单独切换。
我可以a)
单独切换元素上的显示,或者我可以b)
将它们包装在第四个div中,否则该div将不起作用,并切换该元素上的显示。
因此,例如,它是这样的:
<li>
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</li>
li > .a,
li > .b,
li > .c {
display: none;
}
li:hover > .a,
li.current > .a,
li:hover > .b,
li.current > .b,
li:hover > .c,
li.current > .c {
display: block;
}
与此:
<li>
<div class="wrap">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
</li>
li > .wrap {
display: none;
}
li:hover > .wrap,
li.current > .wrap {
display: block;
}
CSS 中存在明显的代码清洁度差异,但代价是一些额外的 html 结构,但我认为网络赞成使用包装器。
我想知道这两种选择之间是否还有其他好处或成本,例如浏览器性能。
Let's say I have three div elements with classes a
, b
, and c
, respectively. I want to use css to toggle the display of these elements between none
and block
, depending on whether or not a parent element has a class of current
or is hovered
. The elements always toggle together, never individually.
I can a)
toggle the display on the elements individually, or I can b)
wrap them in a fourth div that is otherwise non-functional and toggle display on that element instead.
So, for instance, it's this:
<li>
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</li>
li > .a,
li > .b,
li > .c {
display: none;
}
li:hover > .a,
li.current > .a,
li:hover > .b,
li.current > .b,
li:hover > .c,
li.current > .c {
display: block;
}
versus this:
<li>
<div class="wrap">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
</li>
li > .wrap {
display: none;
}
li:hover > .wrap,
li.current > .wrap {
display: block;
}
There's an obvious code-cleanliness
difference in the css, at the expense of some additional html structure, but I think the net is in favor of using the wrapper.
I'm wondering if there are any other benefits or costs, such as browser performance, one way or the other between the two options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,你也不必这样做:)。你正在使用CSS!!请考虑以下事项:
现在,您可以保留
a
、b
、c
的样式元素。通过添加简单的 CSS 样式,您可以快速轻松地满足您的两组需求。此外,它是抽象的,因此您可以在需要时在更多地方使用它。无需添加 DOM 元素,也无需对a
、b
和c
当前的样式代码进行任何实际更改。此外,您的交换机甚至不必是li
。有关 CSS 类的其他信息
元素可能属于多个类。通过利用它,您可以将伪功能与 CSS 内部的外观分开。当使用多个类时,浏览器将结合两者的规则来完成其样式。
请考虑以下事项:
body
元素将具有任何 html 定位和整个文档覆盖。article
类将具有您可能需要的任何呈现样式。twocolumns
将具有您需要的任何布局参数。您可以提供单独或组合的样式,如下所示:
学习以这种方式做事有很多好处。它允许您更轻松地抽象功能、布局和表示元素,从而使您可以“移植”到您可能拥有的其他 Web 项目。它使您可以更有效地找到跨浏览器的工作方式,以获得更统一的外观和感觉。它还减少了意外调整可能与您的问题无关的规则的可能性。最后,它减少了对无关 DOM 元素的依赖,使您能够保持性能。
希望这有帮助。
模糊逻辑
Actually, you don't have to do either :). You're using CSS!! Consider the following:
Now, you can keep your styling elements for
a
,b
,c
. By adding a simple CSS style, you can cover both of your sets of needs quickly and easily. Additionally, its abstracted, so you could use it in more places, when needed. No addition of DOM elements necessary and no real changes to your current styling code fora
,b
, andc
. Furthermore, your switch doesn't even have to be anli
.Additional Information on CSS classes
Elements may be of more than one class. By utilizing this, you can separate pseudo-functionality from look inside your CSS. When utilizing multiple classes, the browser will combine the rules of both to complete its style.
Consider the following:
body
element will have whatever html positioning, and overall document overrides.article
class would have whatever presentation styles you might need.twocolumns
would have whatever layout parameters your require.You may provide individual or combination styles, like so:
Learning to do things in this way has a number of benefits. It allows you to abstract functionality, layout and presentation elements easier allowing you to "port" either to other web projects you might have. It allows you to find what works cross-browser more effectively for more unified look and feel. It also reduces the tendency to accidentally tweak rules that might not be related to your issue. Finally, it reduces your dependency on extraneous DOM elements allowing you to keep your performance.
Hope this helps.
FuzzicalLogic
我更喜欢更干净的 HTML、更混乱的 CSS 选项,因为:
至于浏览器性能,我想说的是,必须解析更多的 CSS 行,这比为额外的 div 元素创建一个新的 DOM 对象要快得多,并且所有的钩子都与这需要的 javascript 事件相关。
I would favour the cleaner HTML, messier CSS option, because:
As for browser performance, I would say having to parse a few more lines of CSS will be far quicker than creating a new DOM object for the extra div element, with all the hooks into javascript events that this entails.
每当您对 DOM 进行更改时,您始终需要将更改次数保持在最低限度。对 DOM 的更改会启动浏览器进行重排 - 遍历页面上的所有元素并“重绘”它们。这种情况发生得很快,但很快你就会看到抖动(最明显的是动画)。
如果您可以选择仅进行 1 次回流而不是 3 次,那么通常您应该选择该选项。当您谈论仅发生一次(例如单击)的几十次回流时,您看不到多少节省。但是,当您开始使用动画,或者在隐藏其他内容的同时对某些内容进行动画处理时,节省的费用就会变得更加明显。
更新:
这个答案假设您正在使用 javascript,因为问题指出“将它们包装在另一个 div 中并切换它”。如果您不使用 javascript 动态添加或删除 css 类,那么您不会切换任何内容(而只是简单地设置它们),而且这个答案也不适用。
Whenever you make changes to the DOM, you always need to keep the number of changes to a minimum. Changes to the DOM initiates the browser to reflow - to run through all the elements on the page and "redraw" them. This happens pretty fast, but it doesn't take much before you see jitters (most notable with animations).
If you have the option to incur only 1 reflow instead 3, as a rule you should take that option. When you are talking about only a few dozen reflows that only happen once (a click, for example), you don't see much savings. But when you start getting into animations, or animating something while hiding others the savings become much more noticable.
Update:
This answer is assuming you are using javascript, because the question states "wrap them in another div and toggle it". If you are not using javascript to dynamically add or remove css classes, then you are not toggling anything (but rather simply setting them), and also this answer doesn't apply.