本篇文章将使用对比的手法讲解Less和Scss的语法,让你快速上手Less和Scss
前端最常用的预处理器 有 scss
和 less
两种。
dart-sass
已经更名为 sass
补充sass
与scss
可以理解为一个东西,唯一的区别是sass
使用锁进表示层级关系,scss
使用花括号{}
表示层级关系
lessc 2-variable.less > 2-variable.css
node-sass --output-style expanded 2-variable.scss > 2-variable-scss.css
import
导入@import "./6-import-variable";
css.box {
height: 200px;
width: 200px;
background: linear-gradient(180deg, #ebecf1, #ebecf1 50%, transparent 50%) top left no-repeat,
linear-gradient(360deg, #ebecf1, #ebecf1 50%, transparent 50%) bottom left no-repeat;
background-size: 100% 1px, 100% 1px;
background-color: #ffffff;
}
scss写法
scss/**
0.5px 精美边框 函数
$directions 是一个数组 (T, B, R, L)
T代表top, B代表bottom, R代表right, L代表left 循序无关
$borderColor 颜色值
例子: @include myBorders((T, B));
*/
$color-gray-border: #ebecf1;
$color-white: #ffffff;
@mixin myBorders($directions, $borderColor: $color-gray-border, $bg-color: $color-white) {
$directionsMap: (
L: linear-gradient(90deg, $borderColor, $borderColor 50%, transparent 50%) bottom left no-repeat,
R: linear-gradient(270deg, $borderColor, $borderColor 50%, transparent 50%) bottom right no-repeat,
T: linear-gradient(180deg, $borderColor, $borderColor 50%, transparent 50%) top left no-repeat,
B: linear-gradient(360deg, $borderColor, $borderColor 50%, transparent 50%) bottom left no-repeat
);
$sizesMap: (
L: 1px 100%,
R: 1px 100%,
T: 100% 1px,
B: 100% 1px
);
$dirResult: ();
$sizResult: ();
@each $direction in $directions {
$dirResult: append($dirResult, map_get($directionsMap, $direction), comma);
$sizResult: append($sizResult, map_get($sizesMap, $direction), comma);
}
background: $dirResult;
background-size: $sizResult;
background-color: $bg-color;
}
.box {
height: 200px;
width: 200px;
@include myBorders((T, B));
}
less就没有那么灵活了
less/* 0.5px 精美边框 函数
T代表top, B代表bottom, R代表right, L代表left
borders-h 水平(上下)边框
borders-v 垂直(左右)边框
borders-h-v 上下左右边框
例:
.borders1 {
.borders-h()
}
*/
.borders-h(@borderColor: @color-line-gray, @bg-color: @color-main-white) {
background: linear-gradient(180deg, @borderColor, @borderColor 50%, transparent 50%) top left no-repeat, linear-gradient(360deg, @borderColor, @borderColor 50%, transparent 50%) bottom left no-repeat;
background-size: 100% 1px, 100% 1px;
background-color: @bg-color;
}
.borders-v(@borderColor: @color-line-gray, @bg-color: @color-main-white) {
background: linear-gradient(90deg, @borderColor, @borderColor 50%, transparent 50%) bottom left no-repeat, linear-gradient(270deg, @borderColor, @borderColor 50%, transparent 50%) bottom right no-repeat;
background-size: 1px 100%, 1px 100%;
background-color: @bg-color;
}
.borders-h-v(@borderColor: @color-line-gray, @bg-color: @color-main-white) {
background: linear-gradient(180deg, @borderColor, @borderColor 50%, transparent 50%) top left no-repeat, linear-gradient(360deg, @borderColor, @borderColor 50%, transparent 50%) bottom left no-repeat, linear-gradient(90deg, @borderColor, @borderColor 50%, transparent 50%) bottom left no-repeat, linear-gradient(270deg, @borderColor, @borderColor 50%, transparent 50%) bottom right no-repeat;
background-size: 100% 1px, 100% 1px, 1px 100%, 1px 100%;
background-color: @bg-color;
}
/* 0.5px边框 单条边框的情况
例:
.border-top {
.border(T) // T,B,L,R分别代表top,bottom,left,right
}
*/
.border-direction(@d, @borderColor) when (@d=B) {
background-size: 100% 1px;
background-position: bottom left;
background-image: linear-gradient(360deg, @borderColor, @borderColor 50%, transparent 50%);
}
.border-direction(@d, @borderColor) when (@d=T) {
background-size: 100% 1px;
background-position: top left;
background-image: linear-gradient(180deg, @borderColor, @borderColor 50%, transparent 50%);
}
.border-direction(@d, @borderColor) when (@d=L) {
background-size: 1px 100%;
background-position: bottom left;
background-image: linear-gradient(90deg, @borderColor, @borderColor 50%, transparent 50%);
}
.border-direction(@d, @borderColor) when (@d=R) {
background-size: 1px 100%;
background-position: top right;
background-image: linear-gradient(270deg, @borderColor, @borderColor 50%, transparent 50%);
}
.border(@d:B, @borderColor: @color-line-gray) {
background-repeat: no-repeat;
.border-direction(@d, @borderColor);
}
(别人封装好的工具库给你用)
PreCSS Stylus https://stylus.bootcss.com/
本文作者:郭敬文
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!