【问题标题】:Can you help me Interpreting `&` in Sass code?你能帮我解释一下 Sass 代码中的 `&` 吗?
【发布时间】:2018-11-03 05:15:29
【问题描述】:

这是 DOM 应用 Sass。

<i className={`contract-icon ic-${this.props.value}`} /> 

这是 Sass 文件。

.contract-icon {
    &.ic-rise_fall {
        &:before {
            content: url('../images/trading_app/contracts/rise_fall.svg');
        }
        &--invert:before { # What's this?
            content: url('../images/trading_app/contracts/invert/rise_fall.svg');
        }
    }
    .other-class {
        padding-right: 8px;
    }
    ...

根据我的研究,

  • &amp; 如果当前元素应用了伴随类,则与此选择器组合/连接。

    • (我的猜测)所以我认为我们可以像这样构建 DOM:&lt;div class="contract-icon&gt; &lt;div class="ic-rise_fall"&gt; -> 但这是不对的。为什么是错的?它与.contract-icon 类有何不同?
  • (额外问题)--invert:before 是什么?

【问题讨论】:

标签: javascript css dom sass


【解决方案1】:

通过这一行,您实际上将两个类应用于i,它们是contract-iconic-{something}(在示例中是ic-rise_fall)。

正如您所说,&amp; 用于组合类名。意思是:-

&.ic-rise_fall {
    &--invert:before { # What's this?
        content: url('../images/trading_app/contracts/invert/rise_fall.svg');
    }
}

这将被转换成

&.ic-rise_fall--invert:before {
    content: url('../images/trading_app/contracts/invert/rise_fall.svg');
}

在 CSS 中。

所以基本上,代码

.contract-icon {
    &.ic-rise_fall {
        &:before {
            content: url('../images/trading_app/contracts/rise_fall.svg');
        }
        &--invert:before { # What's this?
            content: url('../images/trading_app/contracts/invert/rise_fall.svg');
        }
    }
    .other-class {
        padding-right: 8px;
    }
}

会被转换成

.contract-icon.ic-rise_fall:before {
    content: url('../images/trading_app/contracts/rise_fall.svg');
}
.contract-icon.ic-rise_fall--invert:before {
    content: url('../images/trading_app/contracts/invert/rise_fall.svg');
}
.contract-icon .other-class {
    padding-right: 8px;
}

像这样。

这个ic-{something} 是作为props 从父级传递的,所以它可以是ic-rise_fallic-rise_fall--invert。所以我们只是在这里处理使用 SASS 的可能性。

<span class='contract-icon ic-rise_fall'>
   <i class='other-class'></i>
</span>

将此视为您应用other-classic-rise_fall 的情况

【讨论】:

  • 你能不能也回答我的额外问题
  • .contract-icon 呢?
  • 没有。我刚刚将.contract-iconchild 更改为.other-class
  • 刚刚也编辑了我的答案。找到您的答案,并随时消除您的疑虑。
  • 那么为什么要把&amp;放在ic-rise-_fall之前呢?它的工作原理与other-class though 相同。
【解决方案2】:

& is to reference the parent selector.

很容易理解它的作用。你可以说这就像追加父选择器。或者,这样想。

假设你想对.container应用多种样式,它的标准css是:

  1. .container.success,
  2. .container.success.but-stupid,
  3. .container.successful,
  4. .container &gt; div.error,
  5. .container ~ div.stupid,
  6. .container-----_happy-sass

在 SASS 中,你可以这样做,

.container { /* this is 1 */
    &.success { 
        &.but-stupid { /* this is 2 */ }
        &ful { /* this is 3 */  }
    }

    & > div.error { /* this is 4 */ }

    & ~ div.stupid { /* this is 5 */ }

    &-----_happy-sass { /* this is 6 */ }
}

或者,在你上面的例子中,

.contract-icon {
    &.ic-rise_fall {  /* EQUALS TO .contract-icon.ic-rise_fall */
        &:before { /* EQUALS TO .contract-icon.ic-rise_fall:before; */
            /*declarations*/
        }
        &--invert:before { /* EQUALS TO .contract-icon.ic-rise_fall--invert */
            /*declarations*/
        }
    }

    .other-class { /* EQUALS TO .contract-icon .other-class */
        /*declarations*/
    }
}

【讨论】:

  • .other-class in contact-icon 怎么样?
  • @JohnBaek 等于.contract-icon.other-class。请检查更新的答案。
  • 不,它等于.contract-icon .other-class
  • 感谢您的贡献@choz。非常抱歉,我只能选择一个答案。您的解释很清楚,我相信它会对 Stackoverflow 社区有所帮助。我投票赞成你的答案。
  • @JohnBaek 当然,我只是希望这会有所帮助。
【解决方案3】:

你可以说“&”是父级。例如,

这是带有 & 的 SASS。

.contract-icon {
    &.ic-rise_fall {
        &:before {
            content: url('../images/trading_app/contracts/rise_fall.svg');
        }
        &--invert:before { # What's this?
            content: ''
    }
  }
}

这是&的编译CSS。它将选择相同级别的选择器

.contract-icon.ic-rise_fall:before {
  content: url("../images/trading_app/contracts/rise_fall.svg");
}
.contract-icon.ic-rise_fall--invert:before {
  content: "";
}

这是没有 & 的 SASS。

   .contract-icon {
      .ic-rise_fall {
            &:before {
                content: url('../images/trading_app/contracts/rise_fall.svg');
            }
      }
    }

没有&的编译CSS。它将选择子元素

 .contract-icon .ic-rise_fall:before {
  content: url("../images/trading_app/contracts/rise_fall.svg");
}

直接附加父选择器有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2021-10-27
    • 2021-12-22
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多