【问题标题】:possible to define an array in Sass?可以在 Sass 中定义一个数组吗?
【发布时间】:2009-12-14 21:35:44
【问题描述】:

想知道是否可以在 Sass 中使用数组,因为我发现自己在重复以下类型的事情:

.donkey
  h2
    background-color= !donkey

.giraffe
  h2
    background-color= !giraffe

.iguana
  h2
    background-color= !iguana

【问题讨论】:

    标签: haml sass


    【解决方案1】:

    绝对的。

    $animals: "donkey", "giraffe", "iguana"
    @foreach $animal in $animals
      .#{$animal}
        h2
          background-color: !#{$animal}
    

    【讨论】:

    • 贾斯汀是对的。我使用Sass lists 来获得乐趣和利润,并且可以保证它们的有用性。在阅读了 Sass 列表之后,您还应该看看 Sass 的列表函数,找到 here。它们几乎没有帮助,但在某些情况下它们确实可以让您的生活更轻松!
    【解决方案2】:

    不,这是不可能的。最好的方法是定义一个 mixin:

    +animal-h2(!name, !color)
      .#{name} h2
        background-color= !color
    

    那么你可以每个样式只有一行,而不是三行:

    +animal-h2("donkey", !donkey)
    +animal-h2("giraffe", !giraffe)
    +animal-h2("iguana", !iguana)
    

    【讨论】:

      【解决方案3】:

      nex3 的回答是正确的。为了让它与 SASS on Rails 3.1 一起使用,我需要在 css.scss 文件中包含以下内容:

      $donkey: #CC4499;
      
      @mixin animal-h2($name, $color) {
        .#{$name} h2 {
          background-color: $color;
        }
      }
      
      @include animal-h2("donkey", $donkey);
      @include animal-h2("horse", #000);
      

      哪个输出:

      .donkey h2 {
          background-color: #CC4499;
      }
      
      .horse h2 {
          background-color: black;
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 2016-06-01
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 2011-12-15
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多