Creating or referencing variables dynamically in Sass

后端 未结 6 1766
臣服心动
臣服心动 2020-11-22 01:31

I\'m trying to use string interpolation on my variable to reference another variable:

// Set up variable and mixin
$         


        
6条回答
  •  -上瘾入骨i
    2020-11-22 01:40

    Sass does not allow variables to be created or accessed dynamically. However, you can use lists for similar behavior.

    scss:

    $list: 20px 30px 40px;    
    @mixin get-from-list($index) {
      width: nth($list, $index);
    }
    
    $item-number: 2;
    #smth {
      @include get-from-list($item-number);
    }
    

    css generated:

    #smth {
      width: 30px; 
    }
    
    • http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#lists
    • http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#list-functions

提交回复
热议问题