注:配合查看:Sass
为什么使用Sass和Compass?
.color { color: $green; color: rgba($green, .9); }
.rounded { -webkit-border-radius: 5px; -moz-border-radius: 5px; -ms-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; }
改写:
.rounded { @include border-radius(5px); }
Compass,第一个基于Sass的框架
安装
使用
创建新的compass工程
compass create [project-name]

编译输出风格
使用相对资源路径
relative_assets = true
background-image: url('../img/img.jpg');
可以改为:
background-image: image-url('img.jpg');
注:使用的是image-url
模块化开发
借助@import
注:嵌套开发时,尽量不超过三层
@extend可扩展现有代码
只用于扩展的代码,可以使用占位符选择器
混合宏
@mixin bs($bs-type) { -webkit-box-sizing: $bs-type; -moz-box-sizing: $bs-type; box-sizing: $bs-type; }
调用混合宏使用@include命令
* { @include bs(border-box); }
还可以为混合宏定义默认值
@mixin bs($bs-type: border-box) { // do something }
颜色
compass的clearfix
%clearfix { @include clearfix; }
background-color: adjust-hue($color,180deg);
adjust-hue接收两个参数,一个颜色参数,一个0-360度的作用范围
background-color: fade-in($color, .3);
background-color: opacify($color, .3);
rgba($color, .9);
混合函数(mix)
background-color: mix($color1, $color2, 50%);
background-color: adjust-color($color, $hue: 40); 将取得的颜色色调调高40度
background-color: adjust-color($color, $hue: 20, $lightness: 40%); 将取得的颜色色调调高20度,亮度调高40%
background-color: adjust-color($color, $red: 20); 将取得的颜色中红色值调到20
background-color: adjust-color($color, $lightness: -20%);
background-color: scale-color($color, $lightness: -20%);
background-color: shade($color, 60%); background-color: tint($color, 60%);
Compass中的CSS3、Image Sprites等功能
文本阴影
$default-text-shadow-color: darken($color, 20%);
$default-text-shadow-blur: 0px;
$default-text-shadow-v-offset: 4px;
$default-text-shadow-h-offset: 4px;
需要使用多个阴影时,只需要使用逗号隔开每个阴影值
圆角
盒子阴影
$default-box-shadow-color: lighten($color, 50%);
$default-box-shadow-h-offset: 1px;
$default-box-shadow-v-offset: 1px;
$default-box-shadow-blur: 2px;
$default-box-shadow-spread: false;
$default-box-shadow-inset: false;
背景线性渐变语法
@include background(linear-gradient(to-direction, first-color first-color-stop, second-color second-color-stop);
例:
@include background(linear-gradient(to right, blue 0px, purple 40px, orange 150px, red 100%);
背景辐射渐变语法
@include background-image(radial-gradient(100% circle, pink 15%, red 100%);
使用compass image-url帮助工具添加背景图像
background-image: url('../img/img.png');
Compass语法(配置在config.rb文件中的image_dir):
background-image: image-url('img.png');
Compass缓存消除功能
可以阻止浏览器缓存已经更改过的旧文件,去除该功能:
background-image: image-url('img.png', false, false);
第一个参数用于url()函数,若设为true,编译后结果:
background-image: ../img/img.png;
Compass的image sprites功能
$buy-sprite-dimensions: true;
$buy-spacing: 10px;
$buy-layout: horizontal; // 水平排列方式
$buy-layout: vertical; // 纵向排列方式
$buy-layout: diagonal; // 对角排列,最浪费资源的排列方式
$buy-layout: smart; // 智能排列方式,最不浪费资源的方式
CSS变形功能
@include scale(2, 2); // 缩放:让它变为原尺寸的两倍
@include translateX(20px); // 平移:向右平移20px
给单一元素同时添加多种变化,可以使用 simple-transform混合宏,示例如下:
@include simple-transform(1.05, 3deg); // 图像放大1.05倍,并旋转3度
传递的参数必须按照scale、rotate、traslate-x、traslate-y、skew-x、skew-y、origin-x、origin-y的顺序排列
Compass过渡
@include single-transition(all, .3s, ease, 0s);
Sass的计算
.addition{ width: 20% + 80%; }
.addition { width: 100%; }
.subtraction { width: 80% - 20%; }
.subtraction { width: 60%; }
.multiplication { width: 20px * 80px; }
.multiplication { width: 20 * 80px; }
.multiplication { width: 1600px; }
.division { width: 80% / 20%; }
.division { width: 80%/20%; }
.division { width: (80% / 20%); }
.division { width: 4%; }
.addition-and-division-parenthesis { width: 20px + 80px / 5; }
.addition-and-division-parenthesis { width: 36px; }
Sass中的控制命令
@if $color-theme == pink { $color-brand: pink; } @else if $color-theme == orange { $color-brand: #ff9900; }
@for $i from 1 through 4 { // 将实现4次循环,从1到4 } @for $i from 1 to 4 { // 将实现3次循环,从1到3 }
$colors-list: $color-theme $theme-adjust-complement2 $theme-complement $theme-adjust $theme-adjust-complement $theme-shade $theme-tint @each $current-color in $colors-list { // do something }
@function test($para) { // do something @return $result; }