【问题标题】:Scss, nth-child & :not() selectorScss, nth-child & :not() 选择器
【发布时间】:2017-10-26 10:35:45
【问题描述】:

我对 scss 有点陌生,我的行容器中的行有这种基本样式。

.rows-container{
    border-bottom:2px ridge grey;
}
.row{
    height:30px;width:100%;

    &:hover div{
        background-color:lightgrey;
        }
    &:nth-child(odd){
        background: white;
        }
    &:nth-child(even){
        background: #e0e0e0;
        }
}

使用以下 html 非常简单:我遗漏了一些不重要的 html。

<div class="rows-container">
    <div class="row></div> //white
    <div class="row></div> //grey
    <div class="row></div> //white
    <div class="row></div> //grey etc...
</div>

但现在我添加了子行

<div class="rows-container">
    {{each rows}}
        <div class="row"></div>           //parent row

        {{each childs}}
        <div class="subitem"></div>   //several rows from the same table which have a parent_id connected to the current row.
        {{#each}}

    {{#each}}
</div>

我打算在点击时切换子项。但是当没有子项可见时(子项有自己的颜色),'.rows'奇数/偶数会被弄乱。现在我认为这是因为第 n 个子奇数/偶数是在容器中的所有行/子项上计算的,而不仅仅是 .row(s)。 有没有办法对 .rows 设置奇数/偶数样式,但从奇数/偶数旋转中排除 .subitems?我在想也许有一种方法可以在 scss 中使用 :not() ,但到目前为止我还没有成功。

【问题讨论】:

  • 使子项成为行的子项?
  • 如果我真的必须这样做,我想我可以,但它会破坏行的整体响应能力。现在我在想 jquery...我可以为 css 相关的东西创建一个 .js 文件
  • CSS 没有nth-of-class 选择器,因此您无法选择所有其他类。
  • 我会接受这个答案,我会想办法np
  • @MaartenKuijper 你可以制作.rows &lt;section&gt; 和儿童&lt;div&gt;。作为不同的标签,您可以通过以下方式进行更多控制:section:nth-of-type(even)。现在&lt;div&gt; 不会被包含在选择中,只有&lt;section&gt;

标签: css sass


【解决方案1】:

使用:nth-of-type

所以你的代码应该是:

.row{
    height:30px;width:100%;

    &:hover div{
        background-color:lightgrey;
        }
    &:nth-of-type(odd){
        background: white;
        }
    &:nth-of-type(even){
        background: #e0e0e0;
        }
}

参考号:https://css-tricks.com/almanac/selectors/n/nth-of-type/

.subitem:nth-of-type(even) {
  background: lightslategrey;
}

.subitem:nth-of-type(odd) {
  background: red;
}
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>
<div class="subitem">subitem</div>
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>

【讨论】:

  • Tnx,我现在在 .rows 上使用 nth-of-type(odd/even) 并将子项包装在
    中,现在看来已修复!
  • @MaartenKuijper Cool =D
【解决方案2】:

正如我已经评论过的:如果您将 .row 设置为 &lt;section&gt; 并保持兄弟姐妹(它们处于同一级别(彼此相邻)不嵌套在 .row 内) 作为&lt;div&gt;,您可以使用以下方法维护.row 上的奇数/偶数序列:

section:nth-of-type(奇数)

使用:nth-of-type() 选择器可以让您通过使用不同的标签来进行更多控制,当战略性地使用这些标签时,您可以专门针对标签。

演示

section:nth-of-type(odd) {
  background: rgba(0, 0, 0, .4)
}

section:nth-of-type(even) {
  background: rgba(200, 0, 0, .4)
}
<div class="rows-container">
  <section class="row">ROW1</section>
  <div>DIV</div>
  <section class="row">ROW2</section>
  <div>DIV</div>
  <div>DIV All divs are not children of any .row</div>
  <div>DIV All divs are siblings of the .row</div>
  <section class="row">ROW3</section>
  <div>DIV</div>
  <section class="row">ROW4</section>
  <div>DIV</div>
  <div>DIV</div>
  <section class="row">ROW5</section>
  <div>DIV</div>
  <section class="row">ROW6</section>
  <div>DIV</div>
  <div>DIV</div>
  <div>DIV</div>
</div>

【讨论】:

  • Tnx,我已经使用了这两个功能,但只是有点不同,.row:nth-of-type(odd/even),并将所有 .subitem 包装在一个部分中,例如:
    。现在我也很容易创建一个点击函数来切换连接到行的右侧部分
  • 确实,您也可以在子项目上做一些交替颜色,并根据父部分更改它们的颜色以进行对比。就像白色部分打开黑色子项一样,反之亦然。
猜你喜欢
  • 2013-04-04
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多