【问题标题】:SASS - Why is this mixin not being included?SASS - 为什么不包括这个 mixin?
【发布时间】:2013-12-18 00:59:51
【问题描述】:

我是 SASS 新手,如果这看起来很明显,请耐心等待。

我有一些测试 SASS 代码:

$background : "yellow";
$color : "green";

%my-placeholder { 

  background: $background;
  color: $color;

}

@mixin my-mixin($background,$color) {

  %my-placeholder { 

    background: $background;
    color: $color;

  }

}

.my-class {

  /* Why does this 'my-mixin()' NOT get included to the class? */
  @include my-mixin($background,$color);
  color: blue;

}

它是变量、占位符和mixin的组合,输出:

.my-class {
  /* Why does this 'my-mixin()' NOT get included to the class? */
  color: blue;
}

如您所见,mixin 的 @include 调用覆盖占位符,并没有在 .my-class 选择中使用。这是为什么呢?

我有一个 SASS 大师,你可以在其中看到这个直播: http://sassmeister.com/gist/8015459

这是正常行为吗?为了使用此功能,我是否缺少依赖项?

谢谢!

【问题讨论】:

标签: css sass


【解决方案1】:

它被包括在内,但你只是没有调用它。 % 是一个占位符选择器,因此您需要在调用 mixin 后使用 @extend %my-placeholder。这仍然会以您可能没想到的结果而告终。

.my-class {
  color: blue;
}
.my-class .my-class {
  background: "yellow";
  color: "green";
}

我认为使用简单的 mixin 可能会更好,因为您需要切换变量。

@mixin my-mixin( $background: #ccc, $color: #000 ){
  background: $background;
  color: $color;
}

.my-class {
  @include my-mixin( red, green );
}

此外,您似乎认为在选择器中定义的变量会使其成为本地变量,但事实并非如此。在我学习的时候,它确实让我陷入了困境。

$color: red;

.foo {
  $color: blue;
  color: $color; // $color is blue...
}

.bar {
  color: $color; // $color is still blue, not red...
}

【讨论】:

  • 实际上,在未来版本的 Sass 中,选择器中定义的变量将具有不同的范围。在 Sass 3.3 (beta) 下编译您的最后一段示例代码,您将收到关于覆盖/使用全局变量的弃用警告。不过,范围行为的变化要到 3.4 才会发生。
  • 我喜欢这样的声音,尽管我很少发现自己这样做。
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2012-10-24
  • 2014-05-04
相关资源
最近更新 更多