博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SCSS] Reuse Styles with the SCSS @mixin Directive
阅读量:4669 次
发布时间:2019-06-09

本文共 4107 字,大约阅读时间需要 13 分钟。

Copy/pasting the same code is redundant and updating copy/pasted code slows development velocity. Mixins are reusable chunks of code that are included, similar to calling a function, instead of copy/pasted.

Mixins have some nice features:

- Arguments just like functions.
- Arguments can have default values and optional values.
- Named arguments allow us to use optional and default arguments when the mixin is included.
- Variable arguments allow us to have a dynamic number of arguments when the mixin is included.
- The @content directive allow us to add additional styles when the mixin is included.

In this lesson we learn how to DRY up the code with the SCSS @mixin directive and make copy/paste a thing of the past.

 

Define a mixin:

@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {

Here as you can see, we use named parameters. The benifits when we use named parameters is:

  1. we can pass default value: '$base-color: #a83b24'
  2. we can use optional parameter: '$border: null'
  3. when we use mixin, the parameters order doesn't matter
@include make-character($border: 5px solid brown, $mix-color: pink)

 

If you don't know how many paramter the mixin will take, you can do:

@mixin make-transitions($transitions...) { transition: $transitions; }

It can take as many as paramters you pass in:

@include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s);

 

@content directive

@content directive refers to whatever you pass in when you using mixin:

.wolverine {  @include make-character($border: 5px solid brown, $mix-color: pink) {    @include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s);    &:hover {      margin-top: 5rem;      border-radius: 50%;      border: 10px solid green;      transform: rotate3d(10, 0, 0, 360deg);    }  };}

So now, @content referts to all the highlighted part.

@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {  $light-color: lighten($base-color, 20%);  $dark-color: darken($base-color, 35%);  $cbc: complement($base-color);  $clc: complement($light-color);  $cdc: complement($dark-color);  background-image: linear-gradient($light-color, $base-color, $dark-color);  border: $border;  &:hover { background-image: linear-gradient($clc, $cbc, $cdc); }  &:hover &-text { color: transparentize(mix($base-color, $mix-color, 25%), .2); }  &-text {    color: mix($base-color, $mix-color, 75%);  }  img { @content; }}

In this context, 'img' will get all the highlighted styles.


.character {
text-align: center; width: 15rem; display: inline-block; margin: 0.5rem; p { font-size: 1.5rem; padding-bottom: 0.5rem; } img {
margin-top: 1rem; border-radius: 25%; }}@mixin make-transitions($transitions...) {
transition: $transitions; }@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {
$light-color: lighten($base-color, 20%); $dark-color: darken($base-color, 35%); $cbc: complement($base-color); $clc: complement($light-color); $cdc: complement($dark-color); background-image: linear-gradient($light-color, $base-color, $dark-color); border: $border; &:hover { background-image: linear-gradient($clc, $cbc, $cdc); } &:hover &-text {
color: transparentize(mix($base-color, $mix-color, 25%), .2); } &-text {
color: mix($base-color, $mix-color, 75%); } img {
@content; }}@mixin media($min-width) {
@media screen and (min-width: $min-width) { @content; }}.wolverine {
@include make-character($border: 5px solid brown, $mix-color: pink) { @include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s); &:hover { margin-top: 5rem; border-radius: 50%; border: 10px solid green; transform: rotate3d(10, 0, 0, 360deg); } };}.rogue {
@include make-character(#0ab36d, #FFFE8A, 5px solid green); }.firestar {
@include make-character(#DB233B, #e3fd00); }.nightcrawler {
@include make-character(#1d6098, #ffcef9) { @include media(800px) { content: url("../images/bamf.jpg"); } };}

 

 

转载于:https://www.cnblogs.com/Answer1215/p/6760028.html

你可能感兴趣的文章
.NET设计模式(1):1.1 单例模式(Singleton Pattern)
查看>>
创建模态对话框和非模态对话框
查看>>
08-图8 How Long Does It Take
查看>>
二维数组中最大连通子数组
查看>>
java 正则表达式-忽略大小写与多行匹配
查看>>
mac 上亚马逊密钥登录
查看>>
css选择器中:first-child与:first-of-type的区别
查看>>
nopcommerce 二次开发
查看>>
NHibernate入门实例
查看>>
IBM_DS5020磁盘阵列做raid、热备并把盘阵挂在服务器上的步骤
查看>>
svg制作风车旋转
查看>>
《软件工程》课堂作业:返回一个整数数组中最大字数组的和
查看>>
ACM 美素数 (没AC)
查看>>
Sqlserver学习研究
查看>>
VTK图形模型主要对象
查看>>
c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
查看>>
动静态广播的区别
查看>>
前缀式计算(前缀表达式)
查看>>
UOJ 7 NOI2014 购票
查看>>
java学习之—链表(3)
查看>>