Stylus
- 用法更像javascript,列如:变量声明,for循环等
- 运算符非常丰富
- Stylus有简略写法,但是对初学者来讲可能会不太适应
- 含有nib插件,可以简化Css写法
Sass
- 在指令前添加@,便于区分
- 与javascript写法接近
Less:
- 与javascript不同处很多,列如:条件判断使用when()等,学习成本较高
- 内置函数比较少
目录
-
变量
1.声明: 惰性加载(Less)/Sass命名问题/属性变量(Stylus/Less)
2.插值
3.类型: 颜色/数值/字符串/数组/对象/null
4.作用域: 全局变量(Sass/!global) -
语法
1.循环
2.判断: 三目运算(Stylus)
3.运算符: 除号/加号 -
函数: 样式优先(Less/!important)/匹配模式(Less)
1.生成类
2.生成样式
3.返回值 -
嵌套
1.父级: 嵌套层级(Stylus)/根目录(Sass)/属性嵌套(Sass)
2.冒泡 -
继承
1.嵌套继承(Stylus/Less)
2.占位符(Stylus/Sass) -
Stylus扩展
1.Stylus简写
2.判断/循环后置
3.nib插件 -
资料来源网址
1.Stylus: https://stylus.bootcss.com
2.Sass: https://www.sasscss.com
3.Less: https://less.bootcss.com
变量
1.声明
/* Stylus */
$color = red; // $可以省略
.color {
color: $color;
}
// 或
color = red;
.color {
color: color;
}
/* Sass */
$color: red; // $是Sass的变量标识符
.color {
color: $color;
}
/* Less*/
@color: red; // @是Less的变量标识符
.color{
color: @color;
}
- 惰性加载(Less): 可以先使用在声明
当出现多个相同变量时,取有此变量最近作用域的最终值.color { color: @color; } @color: red;
@color: yellow; .color{ @color: red; color: @color; @color: blue; } /* 生成 */ .color{ color: blue; }
- Sass命名: -与_在Sass命名中等值
$color-value: red; .color { color: $color_value; } /* 生成 */ .color { color: red; }
- 属性变量(Less/Stylus): 可以将属性直接当成变量使用
/* Stylus */ .number { width: 100px; height: @width; } /* Less */ .number { width: 100px; height: $width; } /* 生成 */ .number { width: 100px; height: 100px; }
2.插值(用于字符串的拼接): 在Stylus和Sass中会删除字符串的引号
/* Stylus */
$color = red;
.{$color} {
color: red;
}
/* Sass */
$color: red;
.#{$color} {
color: red;
}
/* Less */
@color: red;
.@{color} {
color: red;
}
/* 生成 */
.red {
color: red;
}
3.类型: Stylus判断类型函数为typeof();Sass为type-of();Less暂无,只有判断某些固定类型的函数
- 颜色:
/* Stylus */ $color = #111111; typeof($color) // 返回 'rgba' /* Sass */ $color: #111111; type-of($color) // 返回 color /* Less */ @width: #111111; iscolor(@color) // 返回 true
- 数值:
/* Stylus */ $number = 10px; typeof($number) // 返回 'unit' /* Sass */ $number: 10px; type-of($number) // 返回 number /* Less */ @number: 10px; isnumber(@number) // 返回 true
- 字符串: 字符串分为带有引号和未带引号两种
去除引号/* Stylus */ $string = string; typeof($string) // 返回 'ident' $string = "string"; typeof($string) // 返回 'ident' /* Sass */ $string: string; type-of($string) // 返回 string $string: "string"; type-of($string) // 返回 string /* Less */ @string: "string"; isstring(@string) // 返回 true @string: string; isstring(@string) // 返回 false
/* Stylus */ $string = "string"; unquote($string) /* Sass */ $string: "string"; unquote($string) /* Less */ @string: "string"; e(@string) // 返回 string
- 数组: 使用空格或逗号隔开; Stylus和Less无法判断数组和对象的类型
下标: Stylus可以使用array[index]来显示数组的某个属性,并且Stylus下标从0开始,Less和Sass从1开始/* Stylus */ $list = "1",2,3,4; // 或 $list = "1" 2 3 4; typeof($list) // 返回 'string'; 返回值为数组第一个元素的类型,判断对象也是一样的结果 /* Sass */ $list: 1,2,3,4; // 或 $list: 1 2 3 4; typeof($list) // 返回 list /* Less */ @list: 1,2,3,4; // 或 @list: 1 2 3 4;
/* Stylus */ $content = 1 2 3; $content[0] // 返回 1 /* Sass */ $content: 2 3 4; index($content,2) // 下标值: index(list,number) // 返回 1 /* Less */ @content: 2 3 4; extract(@content,1); // 提取值: extract(list,index) // 返回 2
- 对象: Less使用’分号‘分离,Stylus和Sass使用’逗号‘分离;Stylus和Less可以使用object[prop]写法
/* Stylus */ $object = { name: "name", value: "value" } $object[name] // 返回 "name" /* Sass */ $object : ( name: "name", value: "value" ) typeof($object) // 返回 'object' /* Less */ @object : { name: "name"; value: "value" } $object[name] // 返回 "name"
- Null: Less暂无此类型,会默认为字符串
/* Stylus */ $content = null; .stylus { content: typeof($content); } // 返回 'null' /* Sass */ $content: null; .stylus { content: type-of($content); } // 返回 null
4.作用域: 与javascript的作用域一致
.color{
$color = red;
color: $color;
}
// 或
$color = red;
.color{
color: $color;
}
/* 生成 */
.color{
color: red;
}
- 全局变量(Sass): 在变量后添加!global可以设置为全局变量
.var { $color: red !global; } .color { color: $color; } /* 生成 */ .color { color: red; }
语法
1.循环
/* 循环对象 */
/* Stylus */
$color = {
red: red,
yellow: yellow
};
for $key,$value in $color {
.{$key}{
color: $value;
}
}
/* Sass */
$color: (red: red, yellow: yellow);
@each $key,$value in $color {
.#{$key} {
color: $value;
}
}
/* Less */
@color: {
red: red;
yellow: yellow
}
each(@color, {
.@{key} {
color: @value;
}
})
// 默认值@value,@key,@index
/* 生成 */
.red{
color: red;
}
.yellow{
color: yellow;
}
/* 循环数字数组 */
/* Stylus */
for $index in range(1,2) {
.ellipsis-{$index} {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: $index;
text-overflow: ellipsis;
word-break: break-all;
}
}
// 生成数组: range(start,end,step)
// 相同效果: range(1,2) == (1..2)
/* Sass */
@for $index from 1 through 2 {
.ellipsis-#{$index} {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: $index;
text-overflow: ellipsis;
word-break: break-all;
}
}
// @for $index from 1 through 2
// @for $index from 1 to 2
// through 包含2;to 不包含2
/* Less */
each(range(2), {
.ellipsis-@{index} {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: @index;
text-overflow: ellipsis;
word-break: break-all;
}
})
/* 生成 */
.ellipsis-1 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
text-overflow: ellipsis;
word-break: break-all;
}
.ellipsis-2 {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
text-overflow: ellipsis;
word-break: break-all;
}
- Less中多次循环
@padding:left,right; @offset:10,20; each(@padding,{ each(@offset,.(@val){ .padding-@{value}-@{val}{ padding-@{value}:@val*1px; } }) }) // 重新声明: each(list,.(@v,@k,@i){}) /* 生成 */ .padding-left-10 { padding-left: 10rpx; } .padding-left-20 { padding-left: 20rpx; } .padding-right-10 { padding-right: 10rpx; } .padding-right-20 { padding-right: 20rpx; }
2.判断: Stylus和Sass使用if/else判断,Less使用when进行条件匹配
- Less运算符: 或:or;非:not;与:and;等于:=(与Stylus和Sass的==不同),链接:https://less.bootcss.com/features/
- Sass运算符: 在Less的基础上添加了部分javascript的运算符,链接:https://sass.bootcss.com/documentation/operators
- Stylus运算符: 比Sass运算符更加丰富:https://stylus.bootcss.com/docs/operators.html
/* Stylus */ $padding = { pd: top, pc: null, pm: null } $offset = 10,15; for $key,$value in $padding { for $item in $offset { if($key==pc) { .{$key}{$item}{ padding-top: $item * 1px; padding-bottom: $item * 1px; } } else if($key==pm) { .{$key}{$item}{ padding-left: $item * 1px; padding-right: $item * 1px; } } else { .{$key}{$item}{ padding{$value}: $item * 1px; } } } } // *1px用于添加单位 /* Sass */ $padding:(pt,-top),(pc,null),(pm:null); $offset:10,15; @each $key,$value in $padding { @each $item in $offset { @if($key==pc){ .#{$key}#{$item}{ padding-top:$item + px; padding-bottom:$item + px; } } @else if($key==pm){ .#{$key}#{$item}{ padding-left:$item + px; padding-right:$item + px; } } @else{ .#{$key}#{$item}{ padding#{$value}: $item + px; } } } } /* Less */ @padding: { pt: '-top'; pc: ''; pm: ''; } @offset: 10, 20, 30; each(@padding, { @prop: e(@value); each(@offset, .(@val) { .@{key}@{val} when(@key=pc) { padding-top:~'@{val}px'; padding-bottom:~'@{val}px'; } .@{key}@{val} when(@key=pm) { padding-left:~'@{val}px'; padding-right:~'@{val}px'; } .@{key}@{val} when not(@key=pc) and not(@key=pm) { padding@{prop}:~'@{val}px'; } }) }) // ~'@{val}px'与ES6中的`${val}px`效果一致 /* 生成 */ .pt10{ padding-top: 10rpx; } .pt20{ padding-top: 20rpx; } .pt30{ padding-top: 30rpx; } .pc10{ padding-top: 10rpx; padding-bottom: 10rpx; } .pc20{ padding-top: 20rpx; padding-bottom: 20rpx; } .pc30{ padding-top: 30rpx; padding-bottom: 30rpx; } .pm10{ padding-left: 10rpx; padding-right: 10rpx; } .pm20{ padding-left: 20rpx; padding-right: 20rpx; } .pm30{ padding-left: 30rpx; padding-right: 30rpx; }
- 三目运算(Stylus)
/* Stylus */ $content = false; .content { content: $content?10:20; } /* Less */ @content:false; .content { content: if(@content,10,20); } /* 生成 */ .content { content: 20; }
- 除号/加号: Stylus和Sass在进行除法时需要添加()
/* Stylus */ .content { content: (10/20px); } /* Sass */ .content { content: (10/20px); } /* Less */ .content { content: 10/20px; } /* 生成 */ .content { content: 0.5px; } /* Sass */ .content{ content: 10+px; } // Sass可以实现字符串拼接,在Less和Stylus中可以使用10*1px实现 // Less可以实现拼接,但是拼接点会有空格生成 /* Less */ .mixin() { box-shadow+: inset 0 0 10px #555; } .myclass { .mixin(); box-shadow+: 0 0 20px black; } /* 生成 */ .myclass { box-shadow: inset 0 0 10px #555, 0 0 20px black; }
函数
1.生成类
/* Stylus */
setContent($color=green, $font, args...) {
.content {
color: $color;
font-size: $font;
box-shadow: args;
}
}
setContent(red, $font:20, 1px 1px 1px 1px)
/* Sass */
@mixin setContent($color:green,$font:10,$args...) {
.content {
color: $color;
font-size: $font;
box-shadow: $args;
}
}
@include setContent(red,20,(1px 1px 1px 1px));
/* Less */
.setContent(@color:green,@font,@args...) {
.content{
color:@color;
font-size: @font;
box-shadow: @args;
}
}
.setContent(red,20,1px 1px 1px 1px);
/* 生成 */
.content {
color: red;
font-size: 20;
-webkit-box-shadow: 1px 1px 1px 1px;
box-shadow: 1px 1px 1px 1px;
}
2.生成样式
/* Stylus */
setContent($color=green, $font, args...) {
color: $color;
font-size: $font;
box-shadow: args;
}
.content {
setContent(red, $font: 20, 1px 1px 1px 1px)
}
// 或
setContent() {
color: arguments[0]; // arguments为参数列表
font-size: arguments[1];
box-shadow: arguments[2];
}
.content {
setContent: red,20,(1px 1px 1px 1px);
}
// 执行函数: { 函数名: 参数 }
/* Sass */
@mixin setContent($color:green, $font:10, $args...) {
color: $color;
font-size: $font;
box-shadow: $args;
}
.content {
@include setContent(red, 20, (1px 1px 1px 1px));
}
/* Less */
.setContent(@color:green,@font,@args...) {
color:@color;
font-size: @font;
box-shadow: @args
}
.content{
.setContent(red,20,1px 1px 1px 1px);
}
/* 生成 */
.content {
color: red;
font-size: 20;
-webkit-box-shadow: 1px 1px 1px 1px;
box-shadow: 1px 1px 1px 1px;
}
3.返回值
/* Stylus */
add($a, $b) {
return $a+$b // return可以省略
}
.padding {
padding: add(15px, 5);
}
/* Sass */
@function add($a, $b) {
@return $a+$b
}
.padding {
padding: add(15px, 5);
}
/* Less */
.add(@a, @b) {
@c: @a+@b
}
.padding {
padding: .add(15px, 5)[@c]
}
// 这里可以将返回值看成一个对象
/* 生成 */
.padding {
padding: 20px;
}
- 样式优先级(Less)
.color() { .content { color:red; font-size:10px; } } .color() !important /* 生成 */ .content{ color:red !important; font-size:10px !important; }
- 匹配模式(Less): 函数名相同,通过参数来匹配对应函数
/* 单个参数值不同 */ .mixin(dark; @color) { color: darken(@color, 10%); } .mixin(light; @color) { color: lighten(@color, 10%); } .mixin(@_; @color) { // @_表示所有情况都执行 display: block; } @switch: light; .class { .mixin(@switch; #888); } /* 生成 */ .class { color: #a2a2a2; display: block; } /* 参数数量不同 */ .mixin(@a) { color: @a; } .mixin(@a; @b) { color: fade(@a; @b); } .mixin(red) /* 生成 */ color: red;
嵌套
body{
font-size: 20px;
.content{
color: red;
}
}
/* 生成 */
body{
font-size: 20px;
}
body .content{
color: red;
}
1.父级
.content{
content: 10px;
&:hover{
content: 20px;
}
}
/* 生成 */
.content{
content: 10px;
}
.content:hover{
content: 20px;
}
- 嵌套层级(Stylus)
.body { .content { .red { ^[0]:hover { // ^[0] == ~/ == /.body color: red; } ^[-2]:hover { // ^[-2] == ../../ color: red; } ^[0..1]:hover { color: red; } } } } // ^[N]表示从第N层嵌套开始;-N时执行顺序相反;^[N..M]表示从第N层到第M层 // ~/表示最外层;../表示上一层;/表示根目录 /* 生成 */ .body:hover { color: #f00; } .body:hover { color: #f00; } .body .content:hover { color: #f00; }
- 根目录(Sass)
.body { .content { @at-root .red { color:red; } } } /* 生成 */ .red{ color:red; }
- 属性嵌套(Sass)
.content{ font:{ size:10px; weight:100; } } /* 生成 */ .content { font-size: 10px; font-weight: 100; }
2.冒泡: 自动编译到文件最外层
.body {
@media (min-width: 1000px) {
content: 2
}
}
/* 生成 */
@media (min-width: 1000px) {
.body {
content: 2;
}
}
继承
/* Stylus/Sass */
.red {
color: red;
}
.content {
@extend .red;
content: 10;
}
/* Less */
.red {
color: red;
}
.content:extend(.red) {
content: 10;
}
// 或
.red {
color: red;
}
.content {
.red;
content: 10;
}
/* 生成 */
.red,.content {
color: #f00;
}
.content {
content: 10;
}
- 占位符(Stylus/Sass): Stylus中使用$替换.或#;Sass中使用%:使用占位符可以不将样式输出到css文件
/* Stylus */ $red { color: red; } .content { @extend $red; content: 10; } /* Stylus */ %red { color: red; } .content { @extend %red; content: 10; } /* 生成 */ .content { color: #f00; } .content { content: 10; }
- 嵌套继承(Stylus/Less)
/* Stylus */ .body{ .red{ color:red; } } .content{ @extend .body .red; font-size:20; } /* Less */ .body{ .red{ color:red; } } .content{ .body .red; font-size: 20; } // 或 .content:extend(.body .red){ font-size: 20; } /* 生成 */ .body .red,.content { color: #f00; } .content { font-size: 20; }
Stylus扩展
var = 10
.content
content var
// Stylus简化写法
// 变量省去$
// 样式省去 '缩进'替换'花括号'和'冒号'/'换行'替换'分号'
- Sass
$var: 10 .content content: $var // Scss的初始版;'缩进'替换'花括号'/'换行'替换'分号' /* 生成 */ .content { content: 10; }
- 判断/循环后置
first-even(nums) return n if n % 2 == 0 for n in nums first-even(1 3 5 5 6 3 2) // 返回 6
- nib: Stylus的扩展插件
链接:http://stylus.github.io/nib/// nib的positions.stylus源码 // 关于position的简略写法 -pos(type, args) i = 0 position: unquote(type) for j in (1..4) if length(args) > i {args[i]}: args[i + 1] is a 'unit' ? args[i += 1] : 0 i += 1 fixed() -pos('fixed', arguments) absolute() -pos('absolute', arguments) relative() -pos('relative', arguments) .content{ fixed: right; } /" 生成 "/ .content { position: fixed; right: 0; }