SASS/SCSS 中从变量值获取变量名

发布于 2025-01-05 01:46:09 字数 688 浏览 1 评论 0原文

我想使用定义的值在 @each 循环中访问变量,如下例所示:

$car:true;
$people:false;
$job:false;

    @mixin options($someval){
        @each $prefix in car,people,job{ 
            @if $#{$prefix} == true{
                    //some CSS...
            }       
        }
    }

变量将是一种定义是否打印 Css 规则的“信号量”。

我最大的疑问是如何检查动态定义的变量名称?

我尝试过 $#{$prefix} 但它不起作用。

编辑 - - - - - - - - - - - - - - 我想获得这个 CSS

car-something: 34px;

其中“car”一词取自 $prefix 并在第一轮 @each 循环中 $#{$prefix } 变成 $car

问题出在 $#{$prefix} ...它不起作用:P 我收到错误

I'd like to access variable within an @each loop using defined value like in the following example:

$car:true;
$people:false;
$job:false;

    @mixin options($someval){
        @each $prefix in car,people,job{ 
            @if $#{$prefix} == true{
                    //some CSS...
            }       
        }
    }

Variable would be a sort of "semaphores" that define whether print or not Css rules.

My big doubt is how can I check over dynamically defined variables name ?

I've tried with $#{$prefix} but it doesn't work.

EDIT ---------------------------
I'd like to obtain this CSS

car-something: 34px;

Where the word "car" is taken from $prefix and in the first round of @each loop $#{$prefix} becomes $car

The problem is on $#{$prefix} ... it doesn't work :P i get an error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2025-01-12 01:46:09

这是一个老问题,但由于它没有有效的答案,所以这里是

您需要将地图传递给 @each

$car:true;
$people:false;
$job:false;

@mixin options($someval){
    @each $key, $val in (car: $car, people: $people, job: $job) { 
        @if $val == true{
            #{$key}-something: $someval
        }       
    }
}

test {
    @include options(34px)
}

This is an old question but since it has no valid answer, here it is

You need to pass a map to @each

$car:true;
$people:false;
$job:false;

@mixin options($someval){
    @each $key, $val in (car: $car, people: $people, job: $job) { 
        @if $val == true{
            #{$key}-something: $someval
        }       
    }
}

test {
    @include options(34px)
}
樱花坊 2025-01-12 01:46:09

不要尝试插入变量名,而是将列表传递给 mixin。

$car: true;
$people: false;
$job: false;

$thing-list: $car, $people, $job;

@mixin thingamajig($thing-list) {
  @each $thing in $thing-list {
    @if $thing {
      // Some CSS
    }
  }
}

Instead of trying to interpolate a variable name, pass a list to the mixin.

$car: true;
$people: false;
$job: false;

$thing-list: $car, $people, $job;

@mixin thingamajig($thing-list) {
  @each $thing in $thing-list {
    @if $thing {
      // Some CSS
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文