css - Sass:使用 for 嵌套选择器

标签 css sass

我在 scss 中编写了以下代码来定位具有相同类的 sibling :

    .Foo {
        & ~ .Foo {
            bottom: 2 * $distance-from-bottom + $height;

            & ~ .Foo {
                bottom: 3 * $distance-from-bottom + (2 * $height);

                & ~ .Foo {
                    bottom: 4 * $distance-from-bottom + (3 * $height);

                    & ~ .Foo {
                        bottom: 5 * $distance-from-bottom + (4 * $height);
                    }
                }
            }
        }
    }

我想使用 scss @for 替换它。但是,我正在努力了解如何拥有这些嵌套选择器。任何想法 ?

最佳答案

您可以使用附加每个循环来实现:

$height: 100px;
$distance-from-bottom: 50px;
$var: #{"&"};

@for $i from 2 to 6 {

  .Foo {
    #{append($var, #{"~ &"})} {
      bottom: $i * $distance-from-bottom + ($height*($i - 1));
    }
  }

  $var: append($var, #{"~ &"});
}

结果:

.Foo ~ .Foo {
  bottom: 200px;
}

.Foo ~ .Foo ~ .Foo {
  bottom: 350px;
}

.Foo ~ .Foo ~ .Foo ~ .Foo {
  bottom: 500px;
}

.Foo ~ .Foo ~ .Foo ~ .Foo ~ .Foo {
  bottom: 650px;
}

关于css - Sass:使用 for 嵌套选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60166849/

相关文章:

css - 在 Rails 应用程序 list 中使用 @import 时,您如何知道哪一行包含 SCSS 错误?

css - 如何最好地处理 html5 中嵌套 h1 的样式?

javascript - 相对div内的CSS响应绝对div

javascript - Webpack css-loader 生成错误的图像

css - 如何让css不重复同一个类?

css - 自定义 Bootstrap 4 css

html - rowspan 不工作 html

html - Div 不会环绕 Firefox 中的内容

html - 模态行为异常

PHP 变量超出 HTML 链接 href 字符串?