Sass中这些装酷耍帅的函数(function),你一定要会

2024-01-16 06:08

本文主要是介绍Sass中这些装酷耍帅的函数(function),你一定要会,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

e05d7f00f644539d8e734482e6268dc8.png

来源 | https://www.fly63.com/

玩玩Sass 自带的一些函数,有字符串函数(String Functions)、数字函数(Number Functions)、列表函数(List Functions)、颜色函数(Color Functions)、Introspection 函数(Introspection Functions)、三元函数(Miscellaneous Function)

1. 字符串函数

1.1 quote()

quote($string)给$string前后添加引号。
 
//Scss:
p:after{content: quote(hello +' '+ sass); //中间有空格(其他特殊符号)需要拼字符串;quote(hello sass); 直接这样会报错;
}
p:before{content: quote('This\'s' + ' ' + 'bug');//如果$string本身就带有引号,则会统一换成双引号`""`;
}//编译为:
p:after { content: "hello sass"; }
p:before { content: "This's bug"; }

1.2 unquote()

unquote($string) 删除 $string 前后的引号。
 
//Scss:
p:after{content: unquote("This's bug"); //中间的引号未被删除;
}
p:before{content: unquote(Thissbug);//如$string本身就不带引号,则返回$string本身;
}//编译为:
p:after { content: This's bug; }
p:before { content: sass; }

其实吧!这俩玩意在项目中我还真没用到过!

1.3 str-length()

str-length($string) 返回 $string 的长度。
 
//SCSS:
p:before { content: str-length('hello sass!'); }//编译为:
p:before { content: 11; }

1.4 to-upper-case()

to-upper-case($string) 将$string小写字母转成大写字母。
 
//SCSS:
p:before { content: to-upper-case('hello sass!'); }//编译为:
p:before { content: "HELLO SASS!"; }

1.5 to-lower-case()

to-lower-case($string) 将$string大写字母转成小写字母。
 
//SCSS:
p:before { content: to-lower-case('HELLO SASS!'); }//编译为:
p:before { content: "hello sass!"; }

2、数字函数

2.1 percentage()

percentage($number) 将一个不带单位的数值转成百分比。
 
//SCSS:
.box{ width: percentage(.5)}
.box2{ width: percentage(.1rem / .3rem)}

经测试,两个相同的单位,除了用除法 '/' 其他+-%均会报错,且用除法 '/' 也只能在两个相同类型的单位之间进行运算;*

 
//编译为:
.box { width: 50%; }
.box2 { width: 33.33333%; }

2.2 round()

round($number) 将$number 四舍五入为整数,$number可带单位。
 
//SCSS:
.xs-row{ width: round(33.33333333333333px)}//编译为:
.xs-row { width: 33px; }

2.3 ceil()

ceil($number) 大于 $number ,向上取整。
 
//SCSS
.fs14{ font-size: ceil(13.1px)}
.fs16{ font-size: ceil(15.9px)}//编译为:
.fs14 { font-size: 14px; }
.fs16 { font-size: 16px; }

2.4 floor()

与 ceil()相反,floor($number) 去除 $number 小数,向下取整。
 
//SCSS:
.fs14{ font-size: floor(14.1px) }
.fs16{ font-size: floor(16.9px) }//编译为:
.fs14 { font-size: 14px; }
.fs16 { font-size: 16px; }

2.5 abs()

abs($number),返回 $number 的绝对值。
 
//SCSS:
.fs16{ font-size: abs(-1.6rem) }
.fs18{ font-size: abs(1.8rem) }//编译为:
.fs16{ font-size: 1.6rem; }
.fs18{ font-size: 1.8rem; }

2.6 min() max()

min($numbers…),返回 $number... 的最小值。
max($numbers…),返回 $number... 的最大值。
 
//SCSS:
div{ width: min(2rem, 10rem) }
div{ width: max(2rem, 10rem) }
div{ width: max(2px, 10rem) } //非相同的单位,报错//编译为:
div { width: 2rem; }
div { width: 10rem; }
Incompatible units: 'rem' and 'px'

2.7 random()

random([$limit]),返回一个随机数。
 
//SCSS:
div {height: random(); //直接调用width: random(666); //传个参
}
//编译为:
div {height: 0.3649;width: 403;
}

3、列表函数

常用

3.1 length() nth()

length($list),返回 $list 的长度值;
nth($list, $n),返回 $list 中指定的某个 $n,且 $n必须是大于0的整数;

JavaScript的Array()的索引是从0开始的,厄...有点扯远了,用过 css3 的 :nth-child(n)都应该知道啦,索引下标也是从1开始的,So.....

 
//SCSS:
$list: google, baidu, sogo;
@for $i from 1 through length($list){ //取$list的length并循环输出;.icon-#{nth($list, $i)}{ //$list中的某个索引值;content: '#{nth($list, $i)}'}
}//编译为:
.icon-google { content: "google"; }
.icon-baidu { content: "baidu"; }
.icon-sogo { content: "sogo"; }

3.2 join()

join($list1, $list2, [$separator]) 将两个列表给连接在起来,组成一个列表;
$separator 默认值是 auto,另外还有 comma 和 space 两个值,其中 comma 值指定列表中的列表项值之间使用逗号 , 隔开,space 值指定列表中的列表项值之间使用 空 格 分隔。
 
join((blue, red), (#abc, #def), space) => blue red #abc #def //space
join(10px, 20px, comma) => 10px, 20px //comma

Examples:

 
//SCSS:
$list1: google, baidu, sogo;
$list2: facebook, instagram, twitter;
$list3: join($list1, $list2); //讲真啦,很少用到join(),反正我是很少用到;
@for $i from 1 through length($list3){.icon-#{nth($list3, $i)}{content: '#{nth($list3, $i)}'}
}//编译为:
.icon-google { content: "google"; }
.icon-baidu { content: "baidu"; }
.icon-sogo { content: "sogo"; }
.icon-facebook { content: "facebook"; }
.icon-instagram { content: "instagram"; }
.icon-twitter { content: "twitter"; }

3.3 index()

index($list, $value),返回 $list 中的 $value所在的位置。
 
index(1px solid red, solid) => 2
index(1px solid red, dashed) => null
index((width: 10px, height: 20px), (height 20px)) => 2

3.4 list-separator()

list-separator($list),返回 $list 中的分隔符。
 
list-separator(1px 2px 3px) => space
list-separator(1px, 2px, 3px) => comma
list-separator('foo') => space

4、Introspection 函数

4.1 type-of()

type_of($value) 返回 $value 的类型。
 
type-of(abc)   => string
type-of("abc") => string
type-of(true)   => bool
type-of(#fff)   => color
type-of(green)   => color

4.2 unit()

unit($number) 返回 $number 所使用的单位。
 
unit(100) => ""
unit(100px) => "px"
unit(3em) => "em"
unit(10px * 5em) => "em*px"
unit(10px * 5em / 30cm / 1rem) => "em*px/cm*rem"

4.3 unitless()

unitless($number) 返回 $number 是否带有单位;如果不带单位返回值为 true,带单位返回值为 false。
 
unitless(100) => true
unitless(100px) => false

5、 Miscellaneous 函数

Miscellaneous 函数称为三元条件函数,主要因为他和 JavaScript 中的三元判断非常的相似。他有两个值,当条件成立返回一种值,当条件不成立时返回另一种值
 
if($condition, $if-true, $if-false)

当 $condition 条件为真,则返回 $if-true 值,否则返回 $if-false值。

 
if(true, 1px, 2px) => 1px
if(false, 1px, 2px) => 2px

6、 颜色函数

暂时还没用到过。

6.1 RGB函数()

rgb($red, $green, $blue),根据$red、$green、$blue三个值创建一个颜色;
rgba($red, $green, $blue, $alpha),将一个颜色根据透明度转换成 rgba 颜色。
 
rgba(#102030, 0.5) => rgba(16, 32, 48, 0.5)
rgba(blue, 0.2)    => rgba(0, 0, 255, 0.2)

7、Reference API

Sass::Script::Functions — Sass Documentation

结语

既然你都看到这里了,我就说说吧,这些个函数其实也就在自己写插件的时候有用,其他时候可能会有些大材小用。

学习更多技能

请点击下方公众号

8735ed3b1c8096257441aa689f97a6ab.gif

46a501c8f5b2f2b12428defdc5c6cb8c.png

4a559660392e0c6899e269c64d4f0fef.png

这篇关于Sass中这些装酷耍帅的函数(function),你一定要会的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/611545

相关文章

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

OpenCV结构分析与形状描述符(11)椭圆拟合函数fitEllipse()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 围绕一组2D点拟合一个椭圆。 该函数计算出一个椭圆,该椭圆在最小二乘意义上最好地拟合一组2D点。它返回一个内切椭圆的旋转矩形。使用了由[90]描述的第一个算法。开发者应该注意,由于数据点靠近包含的 Mat 元素的边界,返回的椭圆/旋转矩形数据

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to

AutoGen Function Call 函数调用解析(一)

目录 一、AutoGen Function Call 1.1 register_for_llm 注册调用 1.2 register_for_execution 注册执行 1.3 三种注册方法 1.3.1 函数定义和注册分开 1.3.2 定义函数时注册 1.3.3  register_function 函数注册 二、实例 本文主要对 AutoGen Function Call

✨机器学习笔记(二)—— 线性回归、代价函数、梯度下降

1️⃣线性回归(linear regression) f w , b ( x ) = w x + b f_{w,b}(x) = wx + b fw,b​(x)=wx+b 🎈A linear regression model predicting house prices: 如图是机器学习通过监督学习运用线性回归模型来预测房价的例子,当房屋大小为1250 f e e t 2 feet^

JavaSE(十三)——函数式编程(Lambda表达式、方法引用、Stream流)

函数式编程 函数式编程 是 Java 8 引入的一个重要特性,它允许开发者以函数作为一等公民(first-class citizens)的方式编程,即函数可以作为参数传递给其他函数,也可以作为返回值。 这极大地提高了代码的可读性、可维护性和复用性。函数式编程的核心概念包括高阶函数、Lambda 表达式、函数式接口、流(Streams)和 Optional 类等。 函数式编程的核心是Lambda

PHP APC缓存函数使用教程

APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。(Linux APC扩展安装) 系统缓存 它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存 3600s(一小时)。但是这样仍会浪费大量C