css - SASS 选择器附加函数,带有多个带空格的选择器
问题描述
但是,我的目标是@at-root body.home #{&}
在嵌套选择器中使用,因为 myimg
和选择器只video
需要相同的属性,而不需要.@at-root body.home #{&}
img
video
我尝试使用该selector-append
功能,它可以完成这项工作,但问题是它会去除空格,而不是:
body.home .slider__container.--media .slider .slide img,body.home .slider__container.--media .slider .slide video
如何保留嵌套选择器但在多个选择器中引用 body.home?我想我可以使用一个类(哈哈)但我很好奇!
我最终得到
body.home.slider__container.--media .slider .slide img,body.home.slider__container.--media .slider .slide video
.slider__container {
&.--media {
.slider {
.slide {
img, video {
max-height: calc(100% - (30px * 4) - (28px * 2));
max-width: calc(100% - 60px);
@at-root #{selector-append('body.home', &)} {
max-height: calc(100% - (65px * 4) - (28px * 2));
transition: transform 400ms, filter 400ms;
transform: scale(0.8);
}
@include media(tablet-p) {
max-height: calc(100% - (20px * 4) - (28px * 2));
max-width: calc(100% - 40px);
@at-root #{selector-append('body.home', &)} {
max-width: calc(100% - 100px);
}
}
@include media(phone) {
max-height: calc(100% - (20px * 4) - (28px * 2));
max-width: calc(100% - 40px);
@at-root #{selector-append('body.home', &)} {
max-height: calc(100% - (65px * 4) - (24px * 2));
max-width: calc(100% - 50px);
transform: scale(0.7);
}
}
}
}
}
}
}
解决方案
前段时间我编写了一个“父选择器”SCSS mixin,我相信这就是你所需要的。一探究竟:
<div class="a">
hello there
<div class="b">
<div class="c">
I'M THE C
<div class="d">
<div class="e">
<div class="f">
<div class="g"></div>
</div>
</div>
</div>
</div>
</div>
</div>
SCSS
@mixin parent($el) {
$index: str-index(#{$el}, ' ');
$this-parent: str-slice(#{$el}, 0, $index);
@at-root #{$this-parent}{
@content;
}
}
.a{
$main-parent: &;
background: lightgreen;
.b{
.c{
background: green;
.d{
.e{
.f{
.g{
@include parent(&){
background: red;
}
/* targetting specific elements with just at root */
@at-root #{$main-parent} .b .c {
background: salmon;
}
}
}
}
}
}
}
}
推荐阅读
- c# - 在统一 2d 中更新敌人健康栏
- c# - 在asp-route(参数)中使用javascript变量?
- c# - C# Ninject 不从 JSON 反序列化注入
- algorithm - 使用不等的相关概率进行无替换抽样
- python - 熊猫日期时间格式 VS 谷歌表格日期格式
- javascript - 我得到 TypeError is not a function in nodeJS
- python-3.x - 显示一个不会冻结应用程序的python倒数计时器
- haskell - RebindableSyntax 不能按预期工作
- java - onBindViewHolder 重复结果
- c# - ASP.Net MVC 更新实体遇到“无法更新实体”。