【一】 Scss 学习

1 . 变量声明

方便全局修改 值

$color-primary: #f9ed69; // yellow color
$color-secondary: #f08a5d; // orange
$color-tertiary: #b83b5e; // pink
$color-text-dark: #333; // dark
$color-text-light: #eee;

$width-button: 150px;

2. 嵌套编写 scss

减少选择器的编写

.navigation {
  list-style: none;
  float: left;
  
  li {
    display: inline-block;
    margin-left: 30px;
      
    // 相当于 .navigation li:first-child
    &:first-child {
      margin: 0;
    }
    // 相当于 .navigation li a:link
    a:link {
      @include style-link-text($color-text-dark);
    }
  }
}

3. @mixin 重用样式

减少重复样式的编写

// 这是一段关于浮动导致盒子塌陷的解决代码
@mixin clearfix {
  &::after {
    content:"";
    clear: both;
    display: table;
  }
}
// 此处类型函数 $color 像个形参样
@mixin style-link-text($color) {
  text-decoration: none;
  text-transform: uppercase;
  color: $color;
}

// 引用 @include 引入 @mixin 定义的内容
nav {
  margin: divide(60, 2) * 1px; // 30px
  background-color: $color-primary;
 
  @include clearfix;
  @include style-link-text(red);
}

4. 扩展(%)

避免重复使用同样的选择器

// 扩展
%btn-placeholder {
  padding: 10px;
  display: inline-block;
  text-align: center;
  border-radius: 100px;
  width: $width-button;
  
  @include style-link-text($color-text-light);
}

// 使用
.btn-main {
  &:link {
    @extend %btn-placeholder;
    background-color: $color-secondary;  
  }
  
  &:hover {
    background-color: darken($color-secondary, 15%);
  }
  
}

5. @function

函数吧,感觉一般用不是。和内置函数使用方式差不多

@function divide($a, $b) {
  @return $a / $b;
}

nav {
  // 调用函数  
  margin: divide(60, 2) * 1px; // 30px
  background-color: $color-primary;
 
  @include clearfix;
}

6. Scss 代码 转化成 Css 代码对比

浏览此链接可在线观看:https://codepen.io/qq123457/pen/ZErEEpW?editors=1100

Sass代码
* {
  margin: 0;
  padding: 0;
}

$color-primary: #f9ed69; // yellow color
$color-secondary: #f08a5d; // orange
$color-tertiary: #b83b5e; // pink
$color-text-dark: #333; // dark
$color-text-light: #eee;

  
$width-button: 150px;


@mixin clearfix {
  &::after {
    content:"";
    clear: both;
    display: table;
  }
}

@mixin style-link-text($color) {
  text-decoration: none;
  text-transform: uppercase;
  color: $color;
}

@function divide($a, $b) {
  @return $a / $b;
}

// 扩展
%btn-placeholder {
  padding: 10px;
  display: inline-block;
  text-align: center;
  border-radius: 100px;
  width: $width-button;
  
  @include style-link-text($color-text-light);
}

nav {
  margin: divide(60, 2) * 1px; // 30px
  background-color: $color-primary;
 
  @include clearfix;
}



.navigation {
  list-style: none;
  float: left;
  
  li {
    display: inline-block;
    margin-left: 30px;
    
    &:first-child {
      margin: 0;
    }
    
    a:link {
      @include style-link-text($color-text-dark);
    }
  }
}
.button {
  float: right;
}

.btn-main {
  &:link {
    @extend %btn-placeholder;
    background-color: $color-secondary;  
  }
  
  &:hover {
    background-color: darken($color-secondary, 15%);
  }
  
}

.btn-hot {
  &:link {
    @extend %btn-placeholder;
    background-color: $color-tertiary;  
  }
  
  &:hover {
    background-color: lighten($color-tertiary, 15%);
  }
  
}
Css代码
* {
  margin: 0;
  padding: 0;
}

.btn-hot:link, .btn-main:link {
  padding: 10px;
  display: inline-block;
  text-align: center;
  border-radius: 100px;
  width: 150px;
  text-decoration: none;
  text-transform: uppercase;
  color: #eee;
}

nav {
  margin: 30px;
  background-color: #f9ed69;
}
nav::after {
  content: "";
  clear: both;
  display: table;
}

.navigation {
  list-style: none;
  float: left;
}
.navigation li {
  display: inline-block;
  margin-left: 30px;
}
.navigation li:first-child {
  margin: 0;
}
.navigation li a:link {
  text-decoration: none;
  text-transform: uppercase;
  color: #333;
}

.button {
  float: right;
}

.btn-main:link {
  background-color: #f08a5d;
}
.btn-main:hover {
  background-color: #ea5717;
}

.btn-hot:link {
  background-color: #b83b5e;
}
.btn-hot:hover {
  background-color: #d16f8a;
}