• SASS常用内置函数


    1,math

    引入@use "sass:math";

    常用函数:

    • ceil($number) - 向上取整
    • floor($number) - 向下取整
    • round($number) - 四舍五入
    • max($number...) - 比较若干数值并取最大值
    • min($number...) - 比较若干数值并取最小值
    • random() - 取0~1之间的随机数

    示例

    1. @use "sass:math";
    2. @debug ceil(3.1); // 5
    3. @debug max(5px, 2px, 3px); // 5px
    4. @debug random(); // 0.6581354

    2,string

    引入@use "sass:string";

    常用函数:

    • [str-]length($string) - 字符串长度(局部函数,全局函数前缀加str-)
    • [str-]index($string, $substring) - 返回指定字符在字符串的索引位置(索引从1开始
    • [str-]slice($string, $start, $end) - 截取字符串
    • [str-]insert($string, $insert, $index) - 向指定位置插入字符串
    • to-upper-case($string) - 转换成大写字母
    • to-lower-case($string) - 转换成小写字母
    • quote($string) - 将字符串添加引号
    • unquote($string) - 去除字符串的引号
    • unique-id() - 随机生成一个当前环境下唯一的字符串

    示例:

    1. @use "sass:string";
    2. @debug str-length("abc"); // 3
    3. @debug str-slice("abcd", 2, 3); // "bc"
    4. @debug quote(abc); // "abc"

    3,list

    引入@use "sass:list";

    常用函数

    • append($list, $val, [$spilt]) - 追加元素,$split参数为分隔符(space-空格,comma-逗号,slash-斜杠
    • index($list, $val) - 元素的索引位置,从1开始
    • length($list) - 数组长度
    • nth($list, $index) - 获取指定索引位置的值
    • set-nth($list, $index, $val) - 设置指定位置的值

    示例

    1. @use "sass:list";
    2. $list1: 10px, 12px, 14px;
    3. @debug append($list1, 16px); // 10px, 12px, 14px, 16px
    4. @debug nth($list1, 2); // 12px

    4,map

    引入@use "sass:map";

    常用函数

    • [map-]get($map, $key, [$keys...]) - 通过key值获取value;支持传入多个key值获取深层的值
    • [map.]set($map, $key, [$keys...], $value) - 设置key的值;支持传多个key设置深层的值
    • [map-]keys($map) - 获取所有键的列表
    • [map-]values($map) - 获取所有值的列表
    • [map-]has-key($map, $key, [$keys...]) - 判断是否包含某个键;支持深层key
    • [map-]remove($map, $keys...) - 移除一个或多个key
    • [map.]deep-remove($map, $key, $keys...) - 移除深层的key
    • [map-]merge($map1, $map2) - 浅层次合并两个map,相同的属性取$map2的值
    • [map.]deep-merge($map1, $map2) - 深层次合并两个map

    示例

    1. @use "sass:map";
    2. // 定义map变量
    3. $man: (
    4. name: "zhangsan",
    5. age: 18,
    6. like: (
    7. face: round,
    8. height: 18px
    9. )
    10. )
    11. @debug map-get($man, "name"); // "zhangsan"
    12. @debug map-get($man, "like", "height"); // 18px
    13. @debug map-has-key($man, "like"); // true
    14. @debug map-merge((name: "a", age: 12), (name:"b", sex: 'x')); // (name: "b", age: 12, sex: 'x')

    5,color

    引入@use "sass:color";

    常用函数

    • mix($color1, $color2, $weight) - color1和color2颜色占比混合$weight表示color1颜色的占比,从0% ~100%
    • opacity($color) - 获取颜色的透明度
    • opacity($color, $amont) - 设置颜色的透明度,$amont的值在0~1之间

    示例:

    1. @use "sass:color";
    2. @debug mix(#409EFF, #FFFFFF, 10%); // #ECF5FF 蓝色取10%,更接近白色
    3. @debug mix(#409EFF, #FFFFFF, 90%); // #53A8FF 蓝色取90%,更接近蓝色
    4. @debug opacity(#F023FF, 0.5); // 透明度为0.5

    6,meta

    引入@use "sass:meta";

    常用函数

    • type-of($value) - 变量的类型,返回值例如string、number、list、map、color、function、null、bool(布尔类型boolean)、arglist(参数argument List)、calculation等
    • function-exists($name, [$module]) - 判断某个模块或当前样式表是否存在指定的函数
    • mixin-exists($name, [$module])- 判断某个模块或当前样式表是否存在指定的mixin
    • variable-exists($name) - 判断当期作用域中是否存在指定的变量
    • global-variable-exists($name, [$module]) - 判断某个模块或当前样式表是否存某个在全局变量或者全局函数

    示例:

    1. @use "sass:meta";
    2. $h: 10px;
    3. $q: 10px, 20px, 30px;
    4. @debug type-of(12px); // number
    5. @debug type-of($q); // list
    6. @debug variable-exists($h); // true
    7. @debug global-variable-exists($like); // false

    7,selector

    引入@use "sass:selector";

    常用函数

    • [selector-]append($selectors...) - 把多个选择器联合成一个
    • [selector-]nest($selectors...) - 将多个选择器按顺序进行嵌套
    • [selector-]replace($selector, $source, $target) - 将选择器中的指定选择器替换
    • [selector-]selectors($selector) - 将选择器拆分成列表

    示例:

    1. @use "sass:selector";
    2. @debug selector-append('span', '.active'); // span.active
    3. @debug selector-nest('ul', 'li', '&:hover'); // ul li li:hover
    4. @debug selector-replace('a.disabled', 'a', 'span'); // span.disabled
    5. @debug selector-selectors('.contain.list:before'); // .contain .list :before
  • 相关阅读:
    【OpenCV-Python】教程:3-9 轮廓(4)更多函数
    django setting.py中的SECRET_KEY
    数据结构——堆排序
    开源基础软件大时代,与国产深度学习框架一起乘风破浪
    丝滑的在RT-Smart用户态运行LVGL
    led台灯哪个牌子质量好?2022最新的台灯牌子排名
    C++ 之二叉搜索树
    爱上开源之golang入门至实战第四章-映射(Map)
    【FreeRTOS】14 Tickless低功耗模式
    玩转数组高级技法,成为JS高手
  • 原文地址:https://blog.csdn.net/wjs0406/article/details/132561224