Lenny-Hu / note

blog
5 stars 1 forks source link

Sass技巧 #45

Open Lenny-Hu opened 5 years ago

Lenny-Hu commented 5 years ago

Sass函数功能汇总

来源:https://www.jianshu.com/p/ef2ca6fbf944

1. 字符串函数

2. 数字函数

3. 列表函数

4. Introspection函数

5. 三元条件函数 if($condition,$if-true,$if-false); 判断$condition,如果条件成立,则返回$if-true的结果,如果条件不成立,则返回$if-false的结果。

6.Maps的函数

7.颜色函数

8.自定义函数 考虑到Sass自定义函数用到地方比较多,并且后续需要单独拿出来和Mixin、extend进行比较,所以另开篇幅了 【Sass自定义函数】:https://www.jianshu.com/p/7f879ce0cbb9

Lenny-Hu commented 5 years ago

sass 遍历对象数组

// 1
$stars: (
  (size: 40px, left: 22px, top: 97px),
  (size: 32px, left: 42px, top: 70px),
  (size: 31px, left: 464px, top: 273px),
  (size: 28px, left: 240px, top: 402px),
  (size: 25px, left: 289px, top: 557px)
);

@for $i from 1 through length($stars) {
  $item: nth($stars, $i);

  &:nth-child(#{$i}) {
    width: map-get($item, size);
    height: map-get($item, size);
    left: map-get($item, left);
    top: map-get($item, top);
  }
}

// 2
@each $i, $v in (
                1: (n: 'fan', w: 41, h: 61),
                2: (n: 'gyro', w: 52, h: 52),
                3: (n: 'pillow', w: 64, h: 53),
                4: (n: 'gift', w: 50, h: 53)
            ) {
                &:nth-child(#{$i}) dt {
                    @include bg-image('exchange/#{map-get($v, n)}', px2rem(map-get($v, w)), px2rem(map-get($v, h)));
                    background-position: center;
                }
            }