简体   繁体   English

CSS3选择器:+ vs〜和〜?相反

[英]CSS3 Selectors: + vs ~ and the opposite of ~?

After seeing the different selectors available (as of CSS3), the difference between the + and ~ seem to be nearly the same. 看到可用的不同选择器之后(从CSS3开始), +~之间的差异似乎几乎相同。 And it also appears there is no selector with opposite functionality to ~ . 而且似乎还没有选择器具有与~相反的功能。

Taken from www.w3schools.com : 取自www.w3schools.com

  • div + p : Selects all <p> elements that are placed immediately after <div> elements. div + p :选择紧接<div>元素之后的所有<p> <div>元素。
  • p ~ ul : Selects every <ul> element that are preceded by a <p> element. p ~ ul :选择每个<p>元素之后的<ul> <p>元素。

The effect of p ~ ul can be rewritten as: p ~ ul可以重写为:

Selects all <ul> elements that are placed after a <p> element. 选择放置在 <p>元素之后的所有<ul>元素。

Because preceded by a <p> means the <p> is before the <ul> . 因为 <p> 之前表示<p><ul>之前。

This leaves the only difference between these operators being that + only select elements immediately after (I assume all consecutive occurrences), whereas ~ selects elements anywhere after (but still with the same parent). 这使得作为这些运营商之间的唯一区别+只能选择后立即 (我承担全部连续出现)的元素,而~选择任何地点后 (但仍具有相同父)元素。

  1. Is my understanding of the difference correct? 我对差异的理解正确吗?

Originally I thought the two operators were opposites due to the somewhat confusing english. 最初,我认为这两位操作员是相反的,因为英语有些令人困惑。 So, my follow-up questions is this: 因此,我的后续问题是:

  1. How can I select every element placed before (or immediately before) another element? 如何选择放置在另一个元素之前(或之前)的每个元素?


I'm working with this case scenario: 我正在处理这种情况:

I have 2 div's side-by-side in the same parent div. 我在同一父div中并排有2个div。

 <div id="container"> <div id="column_left"> </div> <div id="column_right"> </div> </div> 

When I hover either div, both should gradually become more opaque using CSS transitions. 当我将任何一个div悬停时,都应该使用CSS过渡逐渐变得更加不透明。 When I'm not hovering either, they should become more transparent. 当我也不悬停时,它们应该变得更加透明。

So, to select the right column when the left is hovered I would using the selector: 因此,要在悬停左侧时选择右侧列,可以使用选择器:

 #column_left:hover+column_right { opacity: 0.9; transition: opacity 300ms; -webkit-transition: opacity 300ms; } 

Now, how can I select the left column when the right is hovered? 现在,当鼠标悬停在右侧时,如何选择左侧列?

Here is my CSS so-far: 这是我到目前为止的CSS:

 div { border: 1px solid black; background: #262626; } #left { float: left; width: 200px; height: 200px; margin: 0; transition: background 300ms; -webkit-transition: background 300ms; } #right { display: inline-block; width: 200px; height: 200px; transition: background 300ms; -webkit-transition: background 300ms; } #left:hover,#left:hover~#right { background: #909090; transition: background 300ms; -webkit-transition: background 300ms; } 
 <div id=left> </div> <div id=right> </div> 

Is my understanding of the difference correct? 我对差异的理解正确吗?

Yes. 是。 Selectors L3 defines those two types of sibling combinators (emphasis mine): 选择器L3定义了这两种同级组合器(重点是我的):

  • Adjacent sibling combinator 相邻的同级组合器

    The adjacent sibling combinator is made of the "plus sign" (U+002B, +) character that separates two sequences of simple selectors. 相邻的同级组合器由分隔两个简单选择器序列的“加号”(U + 002B,+)字符组成。 The elements represented by the two sequences share the same parent in the document tree and the element represented by the first sequence immediately precedes the element represented by the second one. 这两个序列表示的元素在文档树中共享相同的父对象,而第一个序列表示的元素紧接在第二个序列表示的元素之前

  • General sibling combinator 通用同级组合器

    The general sibling combinator is made of the "tilde" (U+007E, ~) character that separates two sequences of simple selectors. 通用同级组合器由分隔两个简单选择器序列的“波浪号”(U + 007E,〜)字符组成。 The elements represented by the two sequences share the same parent in the document tree and the element represented by the first sequence precedes (not necessarily immediately) the element represented by the second one. 这两个序列所表示的元素在文档树中共享相同的父对象,而第一个序列表示的元素在第二个所表示的元素之前(不一定紧接)

How can I select every element placed before (or immediately before) another element? 如何选择放置在另一个元素之前(或之前)的每个元素?

As explained in Is there a previous sibling selector? 前面有同级选择器中所述。 , it's not possible to do that with Selectors L3. ,选择器L3不可能做到这一点。 Selectors L4 may introduce some way to do it, but probably it will only be available in JS (eg through querySelector ) but not in CSS stylesheets because of performance reasons. 选择器L4可能引入了一些方法,但是由于性能原因,它可能仅在JS中可用(例如,通过querySelector ),而在CSS样式表中不可用。

A solution for your specific use case 针对您特定用例的解决方案

When I hover either div, both should gradually become more opaque using CSS transitions. 当我将任何一个div悬停时,都应该使用CSS过渡逐渐变得更加不透明。 When I'm not hovering either, they should become more transparent. 当我也不悬停时,它们应该变得更加透明。

 .wrap { float:left; overflow:hidden } div { border: 1px solid black; } #left { float: left; width: 200px; height: 200px; margin: 0; background-color: red; transition: background 300ms; -webkit-transition: background 300ms; } #right { float:left; width: 200px; height: 200px; background-color: blue; transition: background 300ms; -webkit-transition: background 300ms; } .wrap:hover > #right, .wrap:hover > #left { background: #909090; transition: background 300ms; -webkit-transition: background 300ms; } 
 <div class="wrap"> <div id=left> </div> <div id=right> </div> </div> 

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

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