圆角border-radius

 @mixin rounded($radius){
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}

盒模型阴影box-shadow

下面是一个使用多参数的例子:用CSS3创建一个阴影的mixin,需要传递水平和垂的偏移量,模糊的范围,还有颜色,4个参数:

 @mixin shadow($x, $y, $blur, $color){
    -webkit-box-shadow: $x $y $blur $color;
       -moz-box-shadow: $x $y $blur $color;
            box-shadow: $x $y $blur $color;
}

我们把这个mixin添加到之前的div.module的例子中,让这个阴影以垂直向下1px,2px的模糊范围,颜色为50%透明度的黑色呈现:

 div.module{
padding: 20px;
background: #eee;
@include rounded(10px);
@include shadow(0, 1px, 2px, rgba(0,0,0,.5));
}

CSS3渐变的语法让人非常厌烦。在各浏览器中的写法都不一样,不容易记忆,并且书写规范进化的进程非常快,强迫作者要不断更新他们的样式表。基于以上原因,Sass(特别是mixin)利用CSS3渐变做了一个可随时更新的小功能。当规范变更时,我们只需要在mixin中更新一次语法规范即可。为了保证渐变在大多数浏览器中可以显示,而且在不支持渐变的浏览器中显示纯色,我们需要全面的属性堆栈

 header nav[role="navigation"] ul li.active a {
    padding: 3px 8px;
    color: #fff;
    -webkit-border-radius: 4px;
       -moz-border-radius: 4px;
            border-radius: 4px;
    /* Fallback for sad browsers */
    background-color: #d42a78;
    /* Mozilla Firefox */
    background-image: -moz-linear-gradient(#ff70b1, #d42a78);
    /* Opera */
    background-image: -o-linear-gradient(#ff70b1, #d42a78);
    /* WebKit (Safari/Chrome 10) */
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #ff70b1), color-stop(1, #d42a78));
    /* WebKit (Chrome 11+) */
    background-image: -webkit-linear-gradient(#ff70b1, #d42a78);
    /* IE10 */
    background-image: -ms-linear-gradient(#ff70b1, #d42a78);
    /* W3C */
    background-image: linear-gradient(#ff70b1, #d42a78);
}

每一个创建由上到下渐变的私有前缀属性,都有相同的从开始的十六进制色值到结束的十六进制色值。用Sass的mixin,我们可以通过传递渐变颜色的变量给mixin来很简单的调用这些。谁能每次写渐变的时候都记得这些样式规则啊?下面做一些改变让我们更轻松一点吧。首先我们建一个叫linear-gradient的mixin,在整个样式中把十六进制的色值提取出来,通过$from和$to两个变量将色值传递到样式代码中:

 @mixin linear-gradient($from, $to) {
    /* Fallback for sad browsers */
    background-color: $to;
    /* Mozilla Firefox */
    background-image:-moz-linear-gradient($from, $to);
    /* Opera */
    background-image:-o-linear-gradient($from, $to);
    /* WebKit (Safari 4+, Chrome 1+) */
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, $from), color-stop(1, $to));
    /* WebKit (Chrome 11+) */
    background-image: -webkit-linear-gradient($from, $to);
    /* IE10 */
    background-image: -ms-linear-gradient($from, $to);
    /* W3C */
    background-image: linear-gradient($from, $to);
}

注意,我使用了变量$to的颜色来指定当浏览器不支持CSS渐变样式时,background-color的背景颜色。非常感谢我们只用写这么折磨人的样式一次。现在,当我们想要创建一个简单的渐变的时候,我们就可以选择两个颜色传给mixin,剩下的Sass就帮我们做了。

 &.active a{
padding: 3px 8px;
color: #fff;
@include rounded(4px);
@include linear-gradient(#ff70b1, #d42a78);
}

创建一个mixin库

 @mixin rounded($radius) {
    -webkit-border-radius: $radius;
       -moz-border-radius: $radius;
            border-radius: $radius;
}
@mixin shadow($x, $y, $blur, $color) {
  -webkit-box-shadow: $x $y $blur $color;
     -moz-box-shadow: $x $y $blur $color;
          box-shadow: $x $y $blur $color;
}
@mixin shadow-inset($x, $y, $blur, $color) {
    -webkit-box-shadow: inset $x $y $blur $color;
       -moz-box-shadow: inset $x $y $blur $color;
            box-shadow: inset $x $y $blur $color;
}
@mixin transition($property) {
    -webkit-transition: $property .2s ease;
       -moz-transition: $property .2s ease;
         -o-transition: $property .2s ease;
            transition: $property .2s ease;
}
@mixin box-sizing {
-webkit-box-sizing: border-box;
   -moz-box-sizing: border-box;
        box-sizing: border-box;
}
@mixin linear-gradient($from, $to) {
    /* Fallback for sad browsers */
    background-color: $to;
    /* Mozilla Firefox */
    background-image:-moz-linear-gradient($from, $to);
    /* Opera */
    background-image:-o-linear-gradient($from, $to);
    /* WebKit (Chrome 11+) */
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, $from), color-stop(1, $to));
    /* WebKit (Safari 5.1+, Chrome 10+) */
    background-image: -webkit-linear-gradient($from, $to);
    /* IE10 */
    background-image: -ms-linear-gradient($from, $to);
    /* W3C */
    background-image: linear-gradient($from, $to);
}

sass中mixin常用的CSS3的更多相关文章

  1. sass的mixin,extend,placeholder,function

    1. mixin 就是定义了一个函数,可以传参,并且设定默认值,css选择器可以通过@include来引用,mixin混入的代码,就是原样复制,不会合并,会造成冗余 例如: @mixin br6($b ...

  2. Sass中常用的函数

    字符串函数 To-upper-case() 函数将字符串小写字母转换成大写字母 To-lower-case() 函数 与 To-upper-case() 刚好相反,将字符串转换成小写字母 数字函数 S ...

  3. 工作中经常用到github上优秀、实用、轻量级、无依赖的插件和库

    原文收录在我的 GitHub博客 (https://github.com/jawil/blog) ,喜欢的可以关注最新动态,大家一起多交流学习,共同进步,以学习者的身份写博客,记录点滴. 按照格式推荐 ...

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

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

  5. Sass中的Map 详解

    Sass中的Map长什么样 Sass 的 map 常常被称为数据地图,也有人称其为数组,因为他总是以 key:value 成对的出现, Sass 的 map 长得与 JSON 极其相似. json: ...

  6. sass中 混合宏 VS 继承 VS 占位符 各自的使用时机和特点

    初学者都常常纠结于这个问题“什么时候用混合宏,什么时候用继承,什么时候使用占位符?”其实他们各有各的优点与缺点,先来看看他们使用效果: a) Sass 中的混合宏使用 举例代码见 2-24 行 编译出 ...

  7. css编译工具Sass中混合宏,继承,占位符分别在什么时候使用

    //SCSS中混合宏使用 @mixin mt($var){ margin-top: $var; } .block { @include mt(5px); span { display:block; @ ...

  8. autoprefixer安装或者里sass的$mixin处理浏览器前缀

    Autoprefixer是一个后处理程序,不象Sass以及Stylus之类的预处理器.它适用于普通的CSS,可以实现css3代码自动补全.也可以轻松跟Sass,LESS及Stylus集成,在CSS编译 ...

  9. 几个常用的CSS3样式代码以及不兼容的解决办法

    原文:几个常用的CSS3样式代码以及不兼容的解决办法 border-radius实现圆角效果 CSS3代码: -webkit-border-radius:10px; -moz-border-radiu ...

随机推荐

  1. MYSQL 日期函数【转】

    MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...

  2. HDU 5773 The All-purpose Zero 求LIS

    求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以 ...

  3. jquery之ajax之$.get方法的使用

    jquery对ajax进行了封装,非常方便. 自己用$.get()方法写了个小demo,包括客户端和服务端. 客户端: <!DOCTYPE html> <html> <h ...

  4. PHP IMAP收QQ邮件,SMTP存入另外QQ邮箱

    作用,将qq1收到邮件,用qq2的账号.以qq0的为发件人身份放到qq2的邮箱. 什么样做这样一个功能,一个朋友要求的,她不告诉我为什么,好吧 <?php define('USER','xxx@ ...

  5. HTTP Status 404 - No result defined for action com.csdhsm.struts.action.LoginAction and result error

    智商拙计的问题,没有找到为类LoginAction和error找到定义,然后重新去struts.xml去看,我类个去,我居然把result写成了ERROR <result name=" ...

  6. windows下安装openssh服务并实现远程登录

    需要准备的工具: winscp 点击下载        openssh 点击下载  步骤: 在远程计算机安装 1.首先安装openssh,双击并安装 2.指定用户的home directory为C:\ ...

  7. CSU 1160 A(Contest #3)

    Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的 ...

  8. android webview实战

    webSettings = wvShowProduce.getSettings();//设置WebView属性,能够执行Javascript脚本webSettings.setJavaScriptEna ...

  9. 阿里公共DNS 正式发布了

    喜大普奔!集阿里巴巴集团众多优秀工程师开发维护的公共DNS---AliDNS终于上线啦!作为国内最大的互联网基础服务提供商,阿里巴巴在继承多年优秀技术的基础上,通过提供性能优异的公共DNS服务,为广大 ...

  10. 更新安装xcode7插件

    mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-inscurl -fsSL https://raw.github ...