compass精灵图

css雪碧图又叫css精灵css sprite,是一种背景图片的拼合技术。使用css雪碧图,可以减小页面的请求数、下降图片占用的字节,以此来达到提高页面访问速度的目的。可是它也有使人诟病的地方,就是拼图和后期维护的成本比较大。也正是由于这一点,致使不少开发者懒于使用css雪碧图。javascript

对于这种耗时、枯燥、重复性的工做,最好的解决方法仍是交给工具去处理。本文就介绍下怎样使用compass来自动合并css雪碧图。css

compass

安装compass

首先请确认电脑已经安装rubysass环境,rubysass的安装过程可参考:java

sass入门指南git

安装完成后可经过如下指令确认:github

$ ruby -v
ruby 2.0.0p451 (2014-02-24) [x64-mingw32]
$ sass -v
Sass 3.4.6 (Selective Steve)

接着安装compass:sass

$ gem install compass

// 查看compass版本
$ compass -v
Compass 1.0.1 (Polaris)

ps: 本文中代码运行环境为:sass: 3.4.6, compass: 1.0.1, 测试时请确认sass版本不低于3.4.6,compass版本不低于1.0.1ruby

配置compass项目

进入项目目录,命令行中运行:工具

$ compass init

会生成相应的目录和配置文件。在images目录下创建share目录存放需合并的图标。项目目录结构以下:布局

- sass
- stylesheet
- images
  |-- share
  |-- magic
  |-- setting

config.rb文件配置以下:测试

http_path = "/"
css_dir = "stylesheets"
sass_dir = "sass"
images_dir = "images"
javascripts_dir = "javascripts"

relative_assets = true    // 使用相对目录
line_comments = false    // 关闭行注释

完整的项目目录示例可在github上查看:https://github.com/Bubblings/compass-sprite

合并雪碧图

输出全部雪碧图样式

sass目录下新建share.scss文件,并写入如下代码:

@import "compass/utilities/sprites";    // 加载compass sprites模块
@import "share/*.png";                    // 导入share目录下全部png图片
@include all-share-sprites;                // 输出全部的雪碧图css

命令行调用compass compile进行编译,此时会发现images目录下出现了一个合并后的图片share-xxxxxxxx.pngstylesheet目录下生成了对应的share.css文件:

.share-sprite, .share-github, .share-qq, .share-weibo {
  background-image: url('../images/share-s7fefca4b98.png');
  background-repeat: no-repeat;
}

.share-github {
  background-position: 0 0;
}

.share-qq {
  background-position: 0 -23px;
}

.share-weibo {
  background-position: 0 -47px;
}

至此,咱们就实现了一个简单的雪碧图合并,并且只用了三行代码。是否是有点小激动^_^。 生成的代码中.share-sprite是雪碧图的基础类,后面介绍配置时会详细说明。生成的每一个雪碧图默认的class规则是:.目录名-图片名。若是想自定义,咱们能够经过下面调用单个雪碧图的方式来实现。

调用单个雪碧图样式

sass目录下新建single-share.scss文件,并写入如下代码:

@import "compass/utilities/sprites";    // 加载compass sprites模块
@import "share/*.png";                    // 导入share目录下全部png图片
.test {
    @include share-sprites(github);
}

编译后的css为:

.share-sprite, .test {
  background-image: url('../images/share-s7fefca4b98.png');
  background-repeat: no-repeat;
}

.test {
  background-position: 0 -23px;
}

利用魔术精灵选择器智能输出

有的时候咱们的图标会有多种状态,好比hoveractivefocustarget等。利用compass的魔术精灵选择器咱们就能够智能的合并各状态的图标,并输出对应的css。使用时,咱们须要将图标按照必定的规则命名。例如:

weibo.png            // 默认状态图标
weibo_hover.png     // hover状态图标
weibo_active.png     // active状态图标

sass目录下新建magic.scss文件,并写入如下代码:

@import "compass/utilities/sprites";
@import "magic/*.png";
@include all-magic-sprites;

编译后的css为:

.magic-sprite, .magic-weibo {
  background-image: url('../images/magic-s758f6928e8.png');
  background-repeat: no-repeat;
}

.magic-weibo {
  background-position: 0 0;
}
.magic-weibo:hover, .magic-weibo.weibo-hover {
  background-position: 0 -48px;
}
.magic-weibo:active, .magic-weibo.weibo-active {
  background-position: 0 -24px;
}

雪碧图配置

咱们已经利用compass实现了简单雪碧图的合成。固然compass还提供了不少可供配置的选项,下面来一一介绍。

PS: 如下的配置选项再也不单独举例,可参考示例项目中的setting.scss文件。

先来看下配置相关的语法:

$<map>-<property>: setting;                // 配置全部sprite
$<map>-<sprite>-<property>: setting;    // 配置单个sprite

说明:

  • <map>: 对应图标存放的文件夹名称,如上面例子中的:sharemagic
  • <sprite>: 对应单个图标的名称,如上面例子中的: weiboqqgithub

配置sprite间距

$<map>-spacing: 5px;                // 配置全部sprite间距为5px,默认为0px
$<map>-<sprite>-spacing: 10px;        // 配置单个sprite间距为10px,默认继承$<map>-spacing的值

配置sprite重复性

$<map>-repeat: no-repeat/repeat-x;        // 配置全部sprite的重复性,默认为no-repeat
$<map>-<sprite>-repeat: no-repeat/repeat-x;// 配置单个sprite的重复性,默认继承$<map>-repeat的值

配置sprite的位置

$<map>-position: 0px;                // 配置全部sprite的位置,默认为0px
$<map>-<sprite>-position: 0px;        // 配置单个sprite的位置,默认继承$<map>-position的值

配置sprite的布局方式

$<map>-layout: vertical/horizontal/diagonal/smart;        // 默认布局方式为vertical

清除过时的sprite

$<map>-clean-up: true/false;        // 默认值为true

每当添加、删除或改变图片后,都会生成新的sprite,默认状况下compass会自动的移除旧的sprite,固然也能够经过配置$<map>-clean-up: false;来保留旧的sprite。

配置sprite的基础类

在使用sprite时,compass会自动的生成一个基础类来应用公用的样式(如background-image),默认的类名为$<map>-sprite,上面例子中的.share-sprite.magic-sprite就是这个基础类,固然compass也提供了自定义这个类名的选项:

$<map>-sprite-base-class: ".class-name";

魔术精灵选择器开关

上面已经介绍了怎样利用利用魔术精灵选择器智能输出sprite,默认状况下compass是开启这个功能的,也就是说compass默认会将以_hover_active等名字结尾的图片自动输出对应的:hover:active等伪类样式。固然若是不想这样的话,也能够禁用它。

$disabled-magic-sprite-selectors: false;    // 默认为true

设置sprite尺寸

咱们在合并雪碧图时,不少时候图片的尺寸都不同,那么在使用时咱们如何给每一个sprite设置尺寸呢?compass有提供自动设置每一个sprite尺寸的配置,默认是关闭的,咱们只需手动启用便可。

$setting-sprite-dimensions: true;    // 启用自动设置sprite尺寸,默认值为false

这时输出的样式中会自动加上图片的尺寸,例如:

.setting-compass {
  background-position: -5px 0;
  height: 35px;
  width: 200px;
}

固然,若是只对某个sprite单独设置的话,compass也提供了这个功能。语法以下:

$<map>-sprite-width($name);     // $name为合并前的图片名称
$<map>-sprite-height($name);

例如:

.special {
    @include setting-sprite(compass);
    width: setting-sprite-width(compass);
    height: setting-sprite-height(compass);
}

则输出的css为:

.special {
  background-position: -5px 0;
  width: 200px;
  height: 35px;
}
相关文章
相关标签/搜索