【问题标题】:sass incremental @loop 0.25,0.50,0.75,1sass增量@loop 0.25,0.50,0.75,1
【发布时间】:2017-04-27 06:19:04
【问题描述】:

我想要的是一个从 1 到 100 的增量列表:(+0.25)

.width-0_0em  {width: 0em; }
.width-0_25em {width: 0.25em; }
.width-0_50em {width: 0.5em; }
.width-0_75em {width: 0.75em; }
.width-1em    {width: 1em; }
.width-1_25em {width: 1.25em; }
.width-1_50em {width: 1.5em; }
.width-1_75em {width: 1.75em; }
.width-2em    {width: 2em; }

这是我的代码:

$max: 1 * 100;
$step: 1 ;

$zero: 0;
$twentyfive: 1 * 25 ;
$fifteen: 1 * 50 ;
$seventyyfive: 1 * 75 ;

@for $i from $step through ceil($max/$step) {
     $value: ($i - 1) * $step + 0;


      .width-#{$value}_#{$zero}em {
        width: #{$value}em;
      }
      .width-#{$value}_#{$twentyfive}em {
        width: $i * 0.25em;
      }
      .width-#{$value}_#{$fifteen}em {
        width: $i * 0.5em;
      }
      .width-#{$value}_#{$seventyyfive}em {
        width: $i * 0.75em;
      }
      .width-#{$value}em {
        width:($i - 1) * $step + 1em;
      }
  }
}

有人可以帮我处理这段代码吗?我不知道我做错了什么......任何小提琴或类似的例子都会被欣赏

【问题讨论】:

    标签: html loops variables sass


    【解决方案1】:

    不知道为什么你想要这么多类名,但你的答案很接近。您可以使用此稍作修改的代码 sn-p 来获得您想要的结果。

    @for $i from 0 to 100 {
      $step: 25;
      .width-#{$i}em {
        width: #{$i}em;
      }
      .width-#{$i}_#{$step}em {
        width: #{$i + ($step/100)}em;
      }
      .width-#{$i}_#{$step * 2}em {
        width: #{$i + ($step * 2/100)}em;
      }
      .width-#{$i}_#{$step * 3}em {
        width: #{$i + ($step * 3/100)}em;
      }
    }
    

    希望对你有帮助

    【讨论】:

    • 是的!这正是我想要的解决方案,谢谢!
    【解决方案2】:

    你可以使用nth-child而不是每次都命名一个类

    HTML

    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    

    SASS

    @mixin inc(){
      $step:0.25;
      $max:100;
      @for $i from 1 through ($max/$step){
        $w:$i*$step;
        div:nth-child(#{$i}){
          width:$w+'em';
        }
      }
    }
    @include inc();
    

    将步骤乘以当前的第 N 个孩子,然后就可以了。 FIDDLE

    【讨论】:

    • 很好,但它没有回答我的问题..(或者我不明白)我不希望宽度增量像:0.25、0.50、0.75、1、1.25、1.50 ...to100 孩子的想法很好!
    • 我之前误读了您的问题。现在应该可以了,检查 Fiddle
    【解决方案3】:

    我真的很接近解决方案,我只需要写一个“。”当我有“,”时

      $max: 1 * 100;
        $step: 1 ;
    
        $twentyfive: 1 * 25 ;
        $fifteen: 1 * 50 ;
        $seventyyfive: 1 * 75 ;
    
        @for $i from $step through ceil($max/$step) {
             $value: ($i - 1) * $step + 0;
    
              .width-#{$value}em {
                width:($i - 1) * $step + 0em;
              }
              .width-#{$value}_#{$twentyfive}em {
                width: #{$value},#{$twentyfive}em;
              }
              .width-#{$value}_#{$fifteen}em {
                width: #{$value},#{$fifteen}em;
              }
              .width-#{$value}_#{$seventyyfive}em {
                width: #{$value},#{$seventyyfive}em;
              }
    
      }
    

    【讨论】:

    • 为什么要给每个类起一个值名?
    • 是的,在那之后我想我会有一些“模块化”类,比如 width-3em 或 width-5em
    猜你喜欢
    • 2023-03-27
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2020-12-21
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多