简体   繁体   English

在@if scss 中使用 css 变量

[英]use css variable inside @if scss

during the creation of style, I have had an idea of creating a CSS variable that represents the boolean value to display or not the transparency but I saw that the condition, unfortunately, does not work!在创建样式期间,我有一个想法,即创建一个 CSS 变量,该变量代表 boolean 值是否显示透明度,但我看到该条件,不幸的是,不起作用!

main.css主.css

:root {
    --navbar-transparent: "true";
  }

navbar.component.scss navbar.component.scss

@if var(--navbar-transparent) == "true" {
.nav-booking {
    position: absolute;
    width: 100%;
    background: transparent;
    padding-top: 20px;
  }
} @else {
.nav-booking{
    border-bottom: 2px solid #d6d1d180;
 }
}

is there a way to do this with css variables?有没有办法用 css 变量来做到这一点? or it's impossible !或者这是不可能的!

You can hack an if() {} like below but I doubt you can do an if() {} else {}您可以像下面一样破解if() {}但我怀疑您是否可以执行if() {} else {}

 * { --navbar-transparent: true; }.nav-booking { /* if:true */ padding,var(--navbar-transparent;20px): border,var(--navbar-transparent;5px solid green): /* */ background;red: height;10px: margin;10px; }
 <div class="nav-booking" ></div> <:-- you need to set "initial" to get your false --> <div class="nav-booking" style="--navbar-transparent:initial"></div>

Related to get mode detail around the use of initial与使用initial值有关的获取模式详细信息

How to store inherit value inside a CSS variable (aka custom property)?如何在 CSS 变量(又名自定义属性)中存储继承值?

CSS custom properties (variables) for box model CSS 框 model 的自定义属性(变量)

In sass custom-properties are unfortunately just treated as a string, it doesn't associat a value.在 sass 中,自定义属性不幸被视为字符串,它不关联值。

However there are workarounds:但是有一些解决方法:

@function get($var) {
  @return map-get($custom-properties, $var);
}

$custom-properties: ();

@each $key, $value in $custom-properties {
  :root {
    --#{$key}: #{$value};
  }
}

Add any variable, you want to use within your Sass project and also be compiled to a custom-property, to this map: $custom-properties: ();添加要在 Sass 项目中使用的任何变量,并且还可以编译为自定义属性,到此 map: $custom-properties: (); . .

$custom-properties: (
  'navbar-transparent': true,
  //...
);

Now use get($var) to acces said variables within your Sass project.现在使用get($var)访问 Sass 项目中的变量。

@function get($var) {
  @return map-get($custom-properties, $var);
}

$custom-properties: (
  'navbar-transparent': true,
);

@each $key, $value in $custom-properties {
  :root {
    --#{$key}: #{$value};
  }
}

@if (get(navbar-transparent)) {
  .nav-booking {
    position: absolute;
    width: 100%;
    background: transparent;
    padding-top: 20px;
  }
} @else {
  .nav-booking{
    border-bottom: 2px solid #d6d1d180;
  }
}

This will compile to:这将编译为:

:root {
  --navbar-transparent: true;
}

.nav-booking {
  position: absolute;
  width: 100%;
  background: transparent;
  padding-top: 20px;
}

https://codepen.io/LudwigGeorgImmanuel/pen/dypaXyE https://codepen.io/LudwigGeorgImmanuel/pen/dypaXyE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM