JavaScript系列教程

JavaScript学习笔记(三十五)-- SASS

2020-07-29  本文已影响0人  千锋HTML5学院

SASS

Sass世界上最成熟、稳定和强大的CSS扩展语言 | Sass中文网​www.sass.hk

安装 sass 环境

# 安装全局 sass 环境 $ npm  install sass -g

编译 sass

h1 {
 width: 100px;
 height: 200px;
}
h1 
    width: 100px
    height: 200px
h1 {
 width: 100px;
 height: 200px;
}
# 把 index.scss 编译,输出成 index.css
$ sass index.scss index.css

实时编译

# 实时监控 index.scss 文件,只要发生修改就自动编译,并放在 index.css 文件里面
$ sass --watch index.scss:index.css

实时监控目录

# 实时监控 sass 这个目录,只要有变化,就会实时响应在 css 文件夹下
$ sass --watch sass:css

sass 语法

变量

$c: red;
​
h1 {
 // 在使用 $c 这个变量
 color: $c;
}
h1 {
 // 这个 $w 变量只能在 h1 这个规则块中使用
 $w: 100px;
 width: $w;
}

嵌套

h1 {
 width: 100px;
 
 div {
 width: 200px;
    }
}
​
// 编译结果
h1 {
 width: 100px;
}
​
h1 div {
 width: 200px;
}
ul {
    width: 100px;
    
    li {
        width: 90px;
        
        div {
            width: 80px;
            
            p {
                width: 70px;
                
                span: {
                    color: red;
                }
            }
        }
    }
}

嵌套中的 &

div {
 width: 100px;
 height: 100px;
 
    :hover {
 width: 200px;
    }
}
​
// 我想的是 div 被鼠标悬停的时候 width 变成 200
// 但是编译结果却是
div {
 width: 100px;
 height: 100px;
}
div :hover {
  width: 200px;
}
div {
 width: 100px;
 height: 100px;
​
    &:hover {
 width: 200px;
    }
}
​
// 编译结果
div {
 width: 100px;
 height: 100px;
}
div:hover {
  width: 200px;
}

群组嵌套

div {
 width: 100px;
 
 .box1, .box2, .box3 {
 color: red;
    }
}
​
// 编译结果
div {
  width: 100px;
}
div .box1, div .box2, div .box3 {
  color: red;
}
h1, h2, h3 {
 width: 100px;
​
 .box {
 color: red;
    }
}
​
// 编译结果
h1, h2, h3 {
  width: 100px;
}
h1 .box, h2 .box, h3 .box {
  color: red;
}

嵌套属性

div {
 border: {
 style: solid;
 width: 10px;
 color: pink;
    }
}
​
// 编译结果
div {
 border-style: solid;
 border-width: 10px;
 border-color: pink;
}
div {
 border: 1px solid #333 {
 bottom: none;
    }
}
​
// 编译结果
div {
 border: 1px solid #333;
 border-bottom: none;
}

混入

// 定义一个混合器使用  @mixin 关键字
@mixin radius {
 -webkit-border-radius: 10px;
 -moz-border-radius: 10px;
 -ms-border-radius: 10px;
 -o-border-radius: 10px;
 border-radius: 10px;
}
// 使用混合器使用 @include 关键字
div {
 width: 100px;
 height: 100px;
​
 @include radius;
}
div {
 width: 100px;
 height: 100px;
 -webkit-border-radius: 10px;
 -moz-border-radius: 10px;
 -ms-border-radius: 10px;
 -o-border-radius: 10px;
 border-radius: 10px;
}

混合器传参

// 定义混合器
@mixin my_transition($pro, $dur, $delay, $tim) {
 -webkit-transition: $pro $dur $delay $tim;
 -moz-transition: $pro $dur $delay $tim;
 -ms-transition: $pro $dur $delay $tim;
 -o-transition: $pro $dur $delay $tim;
 transition: $pro $dur $delay $tim;
}
div {
 width: 100px;
 height: 100px;
​
 @include my_transition(all, 1s, 0s, linear);
}
div {
 width: 100px;
 height: 100px;
 -webkit-transition: all 1s 0s linear;
 -moz-transition: all 1s 0s linear;
 -ms-transition: all 1s 0s linear;
 -o-transition: all 1s 0s linear;
 transition: all 1s 0s linear;
}

参数默认值

// 设置一些带有默认值的参数
@mixin my_transition($dur: 1s, $pro: all, $delay: 0s, $tim: linear) {
 -webkit-transition: $dur $pro $delay $tim;
 -moz-transition: $dur $pro $delay $tim;
 -ms-transition: $dur $pro $delay $tim;
 -o-transition: $dur $pro $delay $tim;
 transition: $dur $pro $delay $tim;
}
div {
 width: 100px;
 height: 100px;
 
 // 使用的时候,只传递一个,剩下的使用默认值
 @include my_transition(2s);
}
div {
 width: 100px;
 height: 100px;
 -webkit-transition: 2s all 0s linear;
 -moz-transition: 2s all 0s linear;
 -ms-transition: 2s all 0s linear;
 -o-transition: 2s all 0s linear;
 transition: 2s all 0s linear;
}

继承

div {
 width: 100px;
 height: 100px;
 background-color: pink;
}
p {
 @extend div;
​
 font-size: 20px;
 color: red;
}
div, p {
 width: 100px;
 height: 100px;
 background-color: pink;
}
​
p {
 font-size: 20px;
 color: red;
}

注释

1、编译的时候不会被编译的注释

// 我是一个普通注释,在编译的时候,我就被过滤了

2、编译的时候会被编译的注释

/* 我在编译的时候,会被一起编译过去 */

3、强力注释

/*! 我是一个强力注释,不光编译的时候会被编译过去,将来压缩文件的时候也会存在 */

导入文件

// 我是 variable.scss
$w: 100px;
$h: 200px;
$c: pink;
​
// 我是 mixin.scss
@mixin my_transition($dur: 1s, $pro: all, $delay: 0s, $tim: linear) {
 -webkit-transition: $dur $pro $delay $tim;
 -moz-transition: $dur $pro $delay $tim;
 -ms-transition: $dur $pro $delay $tim;
 -o-transition: $dur $pro $delay $tim;
 transition: $dur $pro $delay $tim;
}
​
@mixin radius {
 -webkit-border-radius: 10px;
 -moz-border-radius: 10px;
 -ms-border-radius: 10px;
 -o-border-radius: 10px;
 border-radius: 10px;
}
// 我是 index.scss
@import './variable.scss';
@import './mixin.scss';
​
div {
 width: $w;
 height: $h;
 background-color: $c;
​
 @include radius;
}
​
h1 {
 @include my_transition;
}
div {
 width: 100px;
 height: 200px;
 background-color: pink;
 -webkit-border-radius: 10px;
 -moz-border-radius: 10px;
 -ms-border-radius: 10px;
 -o-border-radius: 10px;
 border-radius: 10px;
}
​
h1 {
 -webkit-transition: 1s all 0s linear;
 -moz-transition: 1s all 0s linear;
 -ms-transition: 1s all 0s linear;
 -o-transition: 1s all 0s linear;
 transition: 1s all 0s linear;
}

上一篇 下一篇

猜你喜欢

热点阅读