When to use MIXIN?

Better way to use MIXIN is when you deal with browser prefiex, for example:

@mixin transition($val...){
-webkit-transition: $val;
-moz-transition: $val;
transition: $val;
}

Because when you use mixin, actually you just copy the mixin to the another css class, it is not efficent way to wirte css.

But if use it with browser prefix, it can save your typing and you can pass in variable, make it more dynamic.

Notice: $val..., the reason to use ... is because somtime you will passin the value like:

color 0.3s ease-in, background-color 0.5s ease-out

You have , inside, it will casuse problem if you don't give ... after $val.

There is another way to use ...

@mixin button($radius, $color){
border-radius: $radius;
color: $color;
} $props = "4px, #ccc"; .btn-a {
@include button($props...)
}

Sass is smart enought to set $radius to 4px and color to #ccc. But you need to make sure the order is correct.

Default value:

.mtn-select{
@include field-border($top: 'top', $right: 'right', $bottom: '', $left:'left');
@include filed-validation;
@include filed-common;
margin-top: 0px;
& .md-select-icon{
margin-right: 18px;
} }
@mixin field-border($top:top, $right:right, $bottom:bottom, $left:left){
border-#{$top}: 1px solid rgba(0,0,0,0.12) !important;
border-#{$left}: 1px solid rgba(0,0,0,0.12) !important;
border-#{$right}: 1px solid rgba(0,0,0,0.12) !important;
border-#{$bottom}: 1px solid rgba(0,0,0,0.12) !important;
}

INCLUDE

We've identified a set of properties that commonly appear in our stylesheet (sometimes with minor tweaks). Let's streamline the process of using these values by adding the commented lines to a mixin called assemble, then calling that mixin in .factory and.highrise.

// background: #fff;
// border: 1px solid #ccc;
// padding: 10px; @mixin assemble{
background: #fff;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble
}
.highrise {
@include assemble
}

ARGUMENT

That's a good start, but we need the ability to change the background color. Alter the mixin to take an argument called $bg, which should then be set as the value of the background property. Pass#fff in for .factory and #797979 in for .highrise.

@mixin assemble {
background: #fff;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble;
}
.highrise {
@include assemble;
}

Answer:

@mixin assemble($bg) {
background: $bg;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble(#fff);
}
.highrise {
@include assemble(#797979);
}

DEFAULT ARGUMENT

The background color for declarations using assemble will most often be #fff. Add a default value to the $bg argument to reflect this discovery. Note: once the default value is set, remember to update your .factory @include.

@mixin assemble($bg) {
background: $bg;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble(#fff);
}
.highrise {
@include assemble(#797979);
}

Answer:

@mixin assemble($bg:#fff) {
background: $bg;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble;
}
.highrise {
@include assemble(#797979);
}

MULTIPLE ARGUMENTS

Some elements also need a nonstandard amount of padding. Create a second argument, $pad, with a default value of 10px.factory uses the default value, but .highrise should have 20pxof padding on all sides.

@mixin assemble($bg: #fff) {
background: $bg;
border: 1px solid #ccc;
padding: 10px;
} .factory {
@include assemble;
}
.highrise {
@include assemble(#797979);
}

Answer:

@mixin assemble($bg: #fff , $pad:10px) {
background: $bg;
border: 1px solid #ccc;
padding: $pad;
} .factory {
@include assemble;
}
.highrise {
@include assemble(#797979, 20px);
}

KEYWORD ARGUMENTS

Given the number of people collaborating on this stylesheet, we should make the mixin call as clear as possible. Add $bg: and $pad: to the .highrise @include arguments, creating keyword arguments.

@mixin assemble($bg: #fff, $pad: 10px) {
background: $bg;
border: 1px solid #ccc;
padding: $pad;
} .factory {
@include assemble;
}
.highrise {
@include assemble(#797979, 20px);
}

Answer:

@mixin assemble($bg: #fff, $pad: 10px) {
background: $bg;
border: 1px solid #ccc;
padding: $pad;
} .factory {
@include assemble;
}
.highrise {
@include assemble($bg: #797979, $pad: 20px);
}

INTERPOLATING ARGUMENTS

A change request just came in: the border should only apply to one side of these elements. Add a required argument $side to the mixin and interpolate that value to the border property (passingleft as $side should create border-left: 1px solid #ccc, for instance). .factory borders should be on the left side, .highrise borders on the right. As a reminder, arguments without default values need to come before arguments with default values.

@mixin assemble($bg: #fff, $pad: 10px) {
background: $bg;
border: 1px solid #ccc;
padding: $pad;
} .factory {
@include assemble;
}
.highrise {
@include assemble($bg: #797979, $pad: 20px);
}

Answer:

@mixin assemble($side, $bg: #fff, $pad: 10px) {
background: $bg;
border-#{$side}: 1px solid #ccc;
padding: $pad;
} .factory {
@include assemble(left);
}
.highrise {
@include assemble(right, $bg: #797979, $pad: 20px);
}

VARIABLE ARGUMENTS

We're attempting to pass a comma-separated box-shadow value into our mixin as an argument, but we keep getting an error about the number of arguments. Alter $shadow to be a variable argument and accept the value below.

@mixin modal($shadow) {
box-shadow: $shadow;
border: 1px solid #ccc;
} .modal {
@include modal(inset 0 0 5px #000, 0 2px 5px #000);
}

Answer:

@mixin modal($shadow...) {
box-shadow: $shadow;
border: 1px solid #ccc;
} .modal {
@include modal(inset 0 0 5px #000, 0 2px 5px #000);
}

[Sass] Level 3: Mixin -- Ex的更多相关文章

  1. sass揭秘之@mixin,%,@function scss基本使用及操作函数

    sass揭秘之@mixin,%,@function: 地址:https://www.w3cplus.com/preprocessor/sass-mixins-function-placeholder. ...

  2. sass揭秘之@mixin,%,@function

    因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 在阅读本文章之前,请先确认你已经阅读了上篇文章sass揭秘之变量,不然会给你 ...

  3. sass揭秘之@mixin,%,@function(转载)

    因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 在阅读本文章之前,请先确认你已经阅读了上篇文章sass揭秘之变量,不然会给你 ...

  4. Sass的混合-@mixin,@include

    1,无参数,有参数和带默认值参数的@mixin声明sass文件内容: //带参数,默认50@mixin opa($opa:50){ opacity: $opa / 100; filter:alpha( ...

  5. sass中常用mixin

    //设置字体大小 @mixin font($s:14px,$h:1.5,$f:microsoft yahei){ font:$s/#{$h} $f; } //参数(方向,大小,颜色),默认:向下,10 ...

  6. Sass中的mixin,function,extend

    Mixins: 用于相类似的css属性将会被使用多次,每次调用时仅仅有小的参数改变: Function 用于计算得出相关值: Extend 有一批属性完全匹配时,应该使用extend

  7. [Sass] Level 4: Extend -- Ex

    Better use @extend with % placeholder. Extend is useful when you want to reuse some of you class. Al ...

  8. sass(混合mixin的调用、@content)

    sass中使用@mixin声明混合,可以传递参数,参数名以$符号开始,多个参数以逗号分开,也可以给参数设置默认值.声明的@mixin通过@include来调用 1.无参数mixin scss.styl ...

  9. CSS3与页面布局学习总结(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)

    CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...

随机推荐

  1. luoguP4036 [JSOI2008]火星人 平衡树+hash

    这个操作十分的复杂 但是可以拿平衡树维护 直接二分答案然后用$hash$值判断即可 复杂度$O(10000 * log^2 n + n \log n)$ #include <cstdio> ...

  2. Android ContentObserver详解

    前言: 工作中,需要开启一个线程大量的查询某个数据库值发送了变化,导致的开销很大,后来在老大的指点下,利用了ContentObserver完美的解决了该问题,感到很兴奋,做完之后自己也对Content ...

  3. bzoj 2998 第k小字串

    这道题用后缀数组貌似会T. 后缀自动机做法: t==0:第k小的本质不同字串 首先把后缀自动机建出来,我们会得到一个DAG,并且只存在一个点入度为0(我们称之为根),可以证明字符串的任意一个本质不同的 ...

  4. Codeforces Round #350 (Div. 2) C. Cinema 水题

    C. Cinema 题目连接: http://www.codeforces.com/contest/670/problem/C Description Moscow is hosting a majo ...

  5. TensorFlow安装和HelloWorld

    TensorFlow安装 TensorFlow可以在各种操作系统上面安装.安装的时候要注意TensorFlow的类型,一种是普通的版本,仅支持CPU,安装简单.另外一种类型带GPU的,可以利用GPU来 ...

  6. .NET程序员提高效率的70多个开发工具

    工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...

  7. Python基础教程学习(三)

    如何定义类 class ClassName(base_class[es]): "optional documentation string" static_member_decla ...

  8. 第三方网站返回hybrid app H5页面缓存问题应对策略

    最近负责公司各产品线购买模块的开发,各项功能如期开发完成后测试那边反馈回来一个问题:IOS手机在点击支付宝购买后,跳转到支付宝网站时不输入支付密码,直接点返回,返回到我们自己的APP购买界面发现页面显 ...

  9. python接口自动化24-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

  10. Derby安装,创建数据库,在Java程序中使用Derby

    1,下载并安装Derby: 下载地址:http://db.apache.org/derby /derby_downloads.html,下载最新版本. 我用的是10.5.3.0. 解压缩到任意文件夹, ...