作用
- 是一个css的预编译工具
- 依旧要转换为css在浏览器中运行
安装插件
- easy sass
基本知识
- 扩展名:
sass scss
- scss 保存后会生成
.css / .min.css文件
- 空格要注意
语法
变量
// .scss
$color:blue;
.box {
background: $color;
}
计算 + - * /
$width: 100px;
.footer {
width: $width*2;
heigth: $width/2;
}
分支语句
if else
$isShowTab: false;
@if ($isShowTab == true) {
.banner {
color: red;
}
}
@else {
.banner {
color: blue;
}
}
$isRed: true;
div {
width: 100px;
@if(isRed == true) {
background: red;
}
@else {
background: blue;
}
}
循环
for
from 1 to 5 => 1,2,3,4
from 1 through 5 => 1,2,3,4,5
- ``
@for $item from 1 to 5 {
li:nth-child(#{$item}){
position: absoulte;
left: ($item - 1) * 100px;
top:($item -1) * 100px;
}
}
each
@each $item in $colors item代表colors中的元素
$index: index($colors,$item) 下标
$colors: red, green, yellow;
@each $item in $colors {
$index: index($colors,$item)
li:nth-child(
background: $item;
}
}
函数
@function 函数名(变量) {
@return 输出px;
}
$b: 1px;
$c: 2;
@function change($a) {
@return #{$a / $b * $c}px;
}
数组
$colors: red, green, yellow;
混入css
@minxin meimei {
transition: all 1s;
-webkit-transition: all 1s;
-moz-transition: all 1s;
}
.box {
@include meimei
}
.box1 {
@include meimei
}
@minxin meimei($a, $b) {
transition: $a $b;
-webkit-transition: $a $b;
-moz-transition: $a $b;
}
.box {
@include meimei
}
.box1 {
@include meimei
}
@minxin meimei($a:all, $b:1s) {
transition: $a $b;
-webkit-transition: $a $b;
-moz-transition: $a $b;
}
.box {
@include meimei
}
.box1 {
@include meimei
}
嵌套
//原始css
div {
width: 100px;
height: 100px;
}
div p {
width: 50px;
height: 50px;
}
div p span {
width:10px;
heigth:10px;
}
//scss嵌套写法
div {
width: 100px;
height: 100px;
p {
width: 50px;
height: 50px;
span {
width: 10px;
height: 10px;
}
}
}
ul > li {
background: red;
}
ul {
> li {
background: red;
}
}
ul li:hover {
background: yellow;
}
ul {
li {
&:hover {
background: yellow;
}
}
}
继承
//css
.base,.btn_primary,.btn_defualt {
width: 100px;
height: 100px;
outline: none;
}
.btn_primary {
background-color: green;
}
.btn_defualt {
background-color: red;
}
//scss
.base {
width: 100px;
height: 100px;
outline: none;
}
.btn_primary {
@extend .base;
background-color: green;
}
.btn_defualt {
@extend .base;
background-color: red;
}
导入公共文件
// ./xxx.scss
$color: blue;
$bg: red;
@import './xxx.scss'
.box {
color: $color;
background-color: $bg;
}
:export {}
// XXX.scss
$menuText:#bfcbd9;
$menuActiveText:#409EFF;
$subMenuActiveText:#f4f4f5;
$menuBg:#304156;
$menuHover:#263445;
$subMenuBg:#1f2d3d;
$subMenuHover:#001528;
$backWhite:#ffffff;
$sideBarWidth: 210px;
:export {
menuText: $menuText;
menuActiveText: $menuActiveText;
subMenuActiveText: $subMenuActiveText;
menuBg: $menuBg;
menuHover: $menuHover;
subMenuBg: $subMenuBg;
subMenuHover: $subMenuHover;
sideBarWidth: $sideBarWidth;
backWhite: $backWhite;
}
// .vue
@import "@/styles/XXX.scss";
.main-container {
min-height: 100%;
transition: margin-left .28s;
margin-left: $sideBarWidth;
position: relative;
}