如何根据根选择器有条件地设置scss变量值

时间:2019-03-31 09:15:39

标签: css sass scss-mixins

我想根据body标签的选择器更改变量的值。

例如:

$body-bg: green;
$text-color: red;
$border-style: grey;

body{
  background:$body-bg;
  color: $text-color;
}

并且如果身体具有“黑暗”类别,则应有条件地将变量值更改为以下形式:

$body-bg: black;
$text-color: white;

不幸的是,不同文件中有很多覆盖。所以我不能在所有地方都覆盖。因此,在我的情况下,唯一的方法是根据选择器覆盖变量的值。

1 个答案:

答案 0 :(得分:0)

$_contrast-low :     $_color-black !default;

$_contrast-high :    $_color-white !default;

// Find the best contrast color compared to a color
@function get-contrast-text($base-color) {

  $color-brightness: round((red($base-color) * 299) + (green($base-color) * 587) + (blue($base-color) * 114) / 1000);
  $light-color: round((red(#ffffff) * 299) + (green(#ffffff) * 587) + (blue(#ffffff) * 114) / 1000);

  @if abs($color-brightness) < ($light-color/2){
    @return $_contrast-high;
  }

  @return $_contrast-low;
}

您可以使用:

$my-color: #fff;
.hello {
    background : $my-color;
    color:  get-contrast-text($my-color);
}
相关问题