美文网首页
Sass预编译语法

Sass预编译语法

作者: 张先觉 | 来源:发表于2020-10-09 15:10 被阅读0次

1. 遍历@each,完成padding类、font-size计算类.

@each in遍历$变量定义数组的圆括号()
通过 #{} 插值语句,使用变量(简单说,就是将变量插到类名、属性值等里面)

@each $var in (14, 15, 16, 20) {
    .fs-#{$var} {
        font-size: #{$var * 0.02}rem;
    }
}

// Margin、Padding
$base-size: 1px;
$space-types:('m':margin,'p':padding);
$space-direction:('t':top,'l':left,'b':bottom,'r':right);
$space-size:(
    5:5,
    10:10,
    15:15,
    18:18,
    20:20,
);
@each $typeKey,$typeValue in $space-types {
    @each $sizeKey,$sizeValue in $space-size{
        // .m-1
        .#{$typeKey}-#{$sizeKey}{
            #{$typeValue}:$sizeValue * $base-size;
        }
        // .mx-1 .my-1 左右 上下
        .#{$typeKey}x-#{$sizeKey}{
            #{$typeValue}-right:$sizeValue * $base-size;
            #{$typeValue}-left:$sizeValue * $base-size;
        }
        .#{$typeKey}y-#{$sizeKey}{
            #{$typeValue}-top:$sizeValue * $base-size;
            #{$typeValue}-bottom:$sizeValue * $base-size;
        }
        @each $key,$value in $space-direction{
            // .mt-1
            .#{$typeKey}#{$key}-#{$sizeKey}{
                #{$typeValue}-#{$value}:$sizeValue * $base-size;
            }
        }     
    }
}

#附录——sass公共样式

/**
* @file 封装公共样式类
*/

// color background-color
@each $key, $value in $colors {
    .text-#{$key} {
        color: $value;
    }
    .bg-#{$key} {
        background-color: $value;
    }
}

// text-align
@each $key in (left, center, right) {
    .text-#{$key} {
        text-align: $key;
    }
}

// font-weight
@each $key in (600, 800) {
    .fw-#{$key} {
        font-weight: $key;
    }
}

// margin padding
$base-size: 1px;
$space-types: (
    "m": margin,
    "p": padding
);
$space-direction: (
    "t": top,
    "l": left,
    "b": bottom,
    "r": right
);
$space-size: (
    5: 5,
    10: 10,
    15: 15,
    18: 18,
    20: 20
);
@each $typeKey, $typeValue in $space-types {
    @each $sizeKey, $sizeValue in $space-size {
        // .m-1
        .#{$typeKey}-#{$sizeKey} {
            #{$typeValue}: $sizeValue * $base-size;
        }
        // .mx-1 .my-1 左右 上下
        .#{$typeKey}x-#{$sizeKey} {
            #{$typeValue}-right: $sizeValue * $base-size;
            #{$typeValue}-left: $sizeValue * $base-size;
        }
        .#{$typeKey}y-#{$sizeKey} {
            #{$typeValue}-top: $sizeValue * $base-size;
            #{$typeValue}-bottom: $sizeValue * $base-size;
        }
        @each $key, $value in $space-direction {
            // .mt-1
            .#{$typeKey}#{$key}-#{$sizeKey} {
                #{$typeValue}-#{$value}: $sizeValue * $base-size;
            }
        }
    }
}

// flex
// justify-content
$justify-content: (
    "flex-start": flex-start,
    "flex-end": flex-end,
    "center": center,
    "space-between": space-between,
    "space-around": space-around
);
@each $key, $value in $justify-content {
    .jc-#{$key} {
        justify-content: $value;
    }
}

相关文章

  • sass入门学习总结

    总论 sass 经cass编译器编译成css。sass 兼容了css语法。sass 带有变量,mixin,循环,简...

  • sass入门

    编译 使用 sass --watch监控文件的改变,并对其进行实时编译语法: sass --watch input...

  • JS的变量和函数提升

    1.js的运行和预编译过程 <1>.语法分析 查找基本语法有无错误; <2>、预解析/预编译 执行之前进行预解析;...

  • Sass预编译语法

    1. 遍历@each,完成padding类、font-size计算类. @each in遍历、$变量、定义数组的圆...

  • Sass系列(二):sass的基本用法

    sass的语法格式 sass有两种语法格式: sass语法格式(老版本sass语法格式) 文件后缀名为.sass ...

  • Sass基础人门-学习笔记

    一、Sass的语法格式及编译调试 1. Sass和scss的区别 本质上来说是同一个东西,只是语法上有细微的差异:...

  • CSS 预处理器-Sass基础

    上一节讲完了Sass安装与编译的入门支持,本节主要讲Sass的基础语法。 1.变量 在Sass中可以定义变量,变量...

  • sass语法总结

    写在前面的话 本文所有语法格式是基于SCSS格式的 scss 和 sass语法的相互转换 编译 命令行编译 GUI...

  • Sass入门篇

    卸载 Sassgem uninstall sass Sass 语法格式这里说的 Sass 语法是 Sass 的最初...

  • day05-JS运行和编译

    1.JS运行和编译 1.1语法分析:查找基本语法有没有错误 1.2 预解析:执行之前进行预解析 ...

网友评论

      本文标题:Sass预编译语法

      本文链接:https://www.haomeiwen.com/subject/hgplpktx.html