使用sass与compass合并雪碧图(二)
上一篇文章介绍了怎样使用compass合并雪碧图,生成的icons.css文件中单位是px,PC端可以直接在html文件中使用,但在移动端,我们需要根据不同分辨率的屏幕,来缩放图片大小,显然使用px单位肯定是不行的。所以需要做一下单位转换的工作。
移动端使用rem作为单位是最合适不过了。并不是使用了rem就可以,还需要做一些准备工作。我们都知道rem是基于html标签的font-size的,所以需要使用js动态的计算html的font-size。这里我使用淘宝的lib-flexible。
在上一篇文章中,有讲过雪碧地图(Sprite maps),如下面:
$icons: sprite-map("icons/*.png", $spacing: 8px, $layout: smart);
.icon {
width: image-width(sprite-file($icons, card-icon));
height: image-height(sprite-file($icons, card-icon));
background-image: sprite-url($icons);
}
生成css:
.icon {
width: 77px;
height: 64px;
background-image: url('/images/icons-s37f950be3b.png');
}
现在,需要把px转换成rem。我们不可能在icons.css中转换,应该在icons.scss文件中转换。
在icons.scss声明一个转换函数px2rem,:
@function px2rem ($px) {
@return $px / 64px * 1rem;
}
这里的64是因为视觉稿是640px的,如果是750px的就是75。可以看一下lib-flexible的说明。
加上转换函数的icons.scss文件是这样的:
$icons: sprite-map("icons/*.png", $spacing: 8px, $layout: smart);
@function px2rem ($px) {
@return $px / 64px * 1rem;
}
.icon {
width: px2rem(image-width(sprite-file($icons, card-icon)));
height: px2rem(image-height(sprite-file($icons, card-icon)));
background-image: sprite-url($icons);;
}
生成的css如下:
.icon {
width: 1.20313rem;
height: 1rem;
background-image: url('/images/icons-s37f950be3b.png');
}
好了,第一步转换工作就完成了。我们都知道,使用雪碧图,肯定要使用background-position属性,它的单位也是px,也需要转换,所以需要在icons.scss加上:
$icons: sprite-map("icons/*.png", $spacing: 8px, $layout: smart);
@function px2rem ($px) {
@return $px / 64px * 1rem;
}
.icon {
width: px2rem(image-width(sprite-file($icons, card-icon)));
height: px2rem(image-height(sprite-file($icons, card-icon)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, car-icon));
}
但是,编译的时候出错了,错误如下:

意思就是:background-position的值为-250px 0,并不能简单的使用px2rem函数,那该怎么办?我们先来判断一下传递给函数的参数的类型:
@function px2rem ($px) {
@warn type-of($px);
@return $px / 64px * 1rem;
}
再次编译(可以使用compass watch进行监听文件的修改),命令行提示如下图:

从图中可以知道:$width、$height的类型是number,而$pos类型是list。知道了什么类型就可以对症下药了,修改函数如下:
@function px2rem ($px) {
@if (type-of($px) == "number") {
@return $px / 64px * 1rem;
}
@if (type-of($px) == "list") {
@return nth($px, 1) / 64px *1rem nth($px, 2) / 64px * 1rem;
}
}
nth方法可以取出list中的每一项进行运算,输出css如下:
.icon {
width: 1.20313rem;
height: 1rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: -1.46875rem -1.40625rem;
}
这边又会有个问题:background-position的值有可能是0 0、0 100px或者100px 0,而0是没有单位的,这样转换的时候会报错,继续对px2rem函数进行改造,如下:
@function px2rem ($px) {
@if (type-of($px) == "number") {
@return $px / 64px * 1rem;
}
@if (type-of($px) == "list") {
@if (nth($px, 1) == 0 and nth($px, 2) != 0) {
@return 0 nth($px, 2) / 64px * 1rem;
} @else if (nth($px, 1) == 0 and nth($px, 2) == 0) {
@return 0 0;
} @else if (nth($px, 1) != 0 and nth($px, 2) == 0) {
@return nth($px, 1) / 64px * 1rem 0;
} @else {
@return nth($px, 1) / 64px *1rem nth($px, 2) / 64px * 1rem;
}
}
}
上面对各种为0的情况进行了判断,避免了错误。
下面还需要对background-size属性进行转换。在PC端如果图片不要缩放的话,其实不需要该属性,但在移动端一般是需要的。在移动端,可能很多人不知道该怎么用background-size属性,到底是设置整个雪碧图的大小,还是设置单个sprite的的大小呢?其实是设置整个雪碧图的大小。
好像compass没有内置的方法获得雪碧图的大小,没关系,我们可以等到雪碧图生成的时候,再去查看雪碧图的大小。可以先用两个变量保存雪碧图的宽高,初始化为0:
$bigWidth: 0;
$bigHeight: 0;
等雪碧图生成后,查看图片大小,再修改,如:
$bigWidth: 242px;
$bigHeight: 270px;
这时icons.scss文件内容如下:
$icons: sprite-map("icons/*.png", $spacing: 8px, $layout: smart);
$bigWidth: 242px;
$bigHeight: 270px;
@function px2rem ($px) {
@if (type-of($px) == "number") {
@return $px / 64px * 1rem;
}
@if (type-of($px) == "list") {
@if (nth($px, 1) == 0 and nth($px, 2) != 0) {
@return 0 nth($px, 2) / 64px * 1rem;
} @else if (nth($px, 1) == 0 and nth($px, 2) == 0) {
@return 0 0;
} @else if (nth($px, 1) != 0 and nth($px, 2) == 0) {
@return nth($px, 1) / 64px * 1rem 0;
} @else {
@return nth($px, 1) / 64px *1rem nth($px, 2) / 64px * 1rem;
}
}
}
.icon {
width: px2rem(image-width(sprite-file($icons, card-icon)));
height: px2rem(image-height(sprite-file($icons, card-icon)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, card-icon));
background-size: px2rem(($bigWidth, $bigHeight));
background-repeat: no-repeat;
}
生成css如下:
.icon {
width: 1.20313rem;
height: 1rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: -1.46875rem -1.40625rem;
background-size: 3.78125rem 4.21875rem;
background-repeat: no-repeat;
}
到这里,应该可以说是很完美了,但还有改进的空间。我们需要自定义很多类,如:
.icon1 {
width: px2rem(image-width(sprite-file($icons, card-icon)));
height: px2rem(image-height(sprite-file($icons, card-icon)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, card-icon));
background-size: px2rem(($bigWidth, $bigHeight));
background-repeat: no-repeat;
}
.icon2 {
width: px2rem(image-width(sprite-file($icons, watch-icon)));
height: px2rem(image-height(sprite-file($icons, watch-icon)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, watch-icon));
background-size: px2rem(($bigWidth, $bigHeight));
background-repeat: no-repeat;
}
......
上面的每个类中的属性都是一样的,为什么不使用一个mixin,把相同的属性都放进这个mixin中,然后在每个类中引入就可以了。下面来定义一个mixin:
@mixin sprite-info ($icons, $name) {
width: px2rem(image-width(sprite-file($icons, $name)));
height: px2rem(image-height(sprite-file($icons, $name)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, $name));
background-size: px2rem(($bigWidth, $bigHeight));
background-repeat: no-repeat;
}
使用这个mixin:
.card {
@include sprite-info($icons, card-icon);
}
.watch {
@include sprite-info($icons, watch-icon);
}
生成css如下:
.card {
width: 1.20313rem;
height: 1rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: -1.46875rem -1.40625rem;
background-size: 3.78125rem 4.21875rem;
background-repeat: no-repeat;
}
.watch {
width: 1.3125rem;
height: 1.40625rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: 0 0;
background-size: 3.78125rem 4.21875rem;
background-repeat: no-repeat;
}
现在可以说是非常完美了。下面贴出icons.scss文件中最终的代码:
$icons: sprite-map("icons/*.png", $spacing: 8px, $layout: smart);
$bigWidth: 242px;
$bigHeight: 270px;
@function px2rem ($px) {
@if (type-of($px) == "number") {
@return $px / 64px * 1rem;
}
@if (type-of($px) == "list") {
@if (nth($px, 1) == 0 and nth($px, 2) != 0) {
@return 0 nth($px, 2) / 64px * 1rem;
} @else if (nth($px, 1) == 0 and nth($px, 2) == 0) {
@return 0 0;
} @else if (nth($px, 1) != 0 and nth($px, 2) == 0) {
@return nth($px, 1) / 64px * 1rem 0;
} @else {
@return nth($px, 1) / 64px *1rem nth($px, 2) / 64px * 1rem;
}
}
}
@mixin sprite-info ($icons, $name) {
width: px2rem(image-width(sprite-file($icons, $name)));
height: px2rem(image-height(sprite-file($icons, $name)));
background-image: sprite-url($icons);
background-position: px2rem(sprite-position($icons, $name));
background-size: px2rem(($bigWidth, $bigHeight));
background-repeat: no-repeat;
}
.card {
@include sprite-info($icons, card-icon);
}
.watch {
@include sprite-info($icons, watch-icon);
}
生成的icons.css代码如下:
.card {
width: 1.20313rem;
height: 1rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: -1.46875rem -1.40625rem;
background-size: 3.78125rem 4.21875rem;
background-repeat: no-repeat;
}
.watch {
width: 1.3125rem;
height: 1.40625rem;
background-image: url('/images/icons-s37f950be3b.png');
background-position: 0 0;
background-size: 3.78125rem 4.21875rem;
background-repeat: no-repeat;
}
使用sass与compass合并雪碧图(二)的更多相关文章
- 使用sass与compass合并雪碧图(一)
雪碧图就是很多张小图片合并成一张大图片,以减少HTTP请求,从而提升加载速度.有很多软件可以合并雪碧图,但通常不太容易维护,使用compass生成雪碧图应该算是非常方便的方法了,可以轻松的生成雪碧图, ...
- Sass和Compass制作雪碧图
1.安装好了sass与compass之后设置一个配置文件 2.新增一个雪碧图文件夹用来存放将要合并的图片例如color文件夹 3.@import命令引用 .Compass看到@import指令的参数为 ...
- compass做雪碧图
由于最近没什么时间好好写博文,我把用sass做雪碧图的关键点贴出来方便自己记忆: config.rb注释 # Set this to the root of your project when dep ...
- 使用Compass制作雪碧图
遇见好的文章,笔者也会转载.但是正所谓好记性不如烂笔头,单纯的拿来主义也不如自己的亲自实践.所以每次需要转载的文章,我都会自己敲一遍,中间加入一些自己的思考. 这篇文章转载自:http://www.h ...
- 利用compass制作雪碧图
compass是什么?是sass一款神奇插件,具体教程,我还是推荐阮一峰sass,compass教程,简单清晰明了. 用ps制作雪碧图,工作效率太低了.用compass来制作,方便很多.下图的用com ...
- compass Sprites 雪碧图 小图片合成[Sass和compass学习笔记]
demo 源码 地址 https://github.com/qqqzhch/webfans 什么是雪碧图? CSS雪碧 即CSS Sprites,也有人叫它CSS精灵,是一种CSS图像合并技术,该方法 ...
- compass与css sprite(雪碧图)
什么是css sprite? css sprite,中文叫雪碧图,也有人喊CSS精灵,就是一种背景拼合的技术,然后通过background-position来显示雪碧图中需要显示的图像. MDN相关链 ...
- sass制作雪碧图
1.配置文件config.rb http_path = "../../../" css_dir = "Content/css" sass_dir = " ...
- compass框架的sprite雪碧图的用法简要
---恢复内容开始--- **简介** CSS SPRITE 即 CSS雪碧,即是将诸多图片合成一张图片,然后使用CSS 的background和background-position属性渲染. 这样 ...
随机推荐
- windows同时安装python2和python3
系统之前安装了python2.7,现在准备装个python3.6 1:首先下载一个python3.6适合windows32位的包python-3.6.5.exe 然后直接默认双击安装,安装的时候勾选a ...
- 学习笔记·堆优化$\mathscr{dijkstra}$
嘤嘤嘤今天被迫学了这个算法--其实对于学习图论来说我内心是拒绝的\(\mathscr{qnq}\) 由于发现关于这个\(\mathscr{SPFA}\)的时间复杂度\(O(kE)\)中的\(k \ap ...
- P2467 [SDOI2010]地精部落
题目描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为N的山脉H可分为从左到右的N段,每段有一个独一无二的高度Hi,其中Hi是1到N之间的正整数 ...
- 404 Note Found 队-Beta5
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- GIt帮助文档之创建新的Git仓库——现有目录下,通过导入所有文件来创建
1.新建仓库初始化操作 1.1打开Git Bash命令窗口,切换到项目文件夹目录: $ cd weixin 1.2执行命令: $ git init 初始化操作,把项目weixin纳入Git管理.初始化 ...
- linux查看磁盘占用情况
一:首先是先登录 二:查看当前目录 命令:df -h 三:查看具体文件夹占用情况 命令:du --max-depth=1 -h /data/ 或者:为了快算显示,同时也只是想查看目录整体占用大小 命 ...
- PHP 抓取函数curl 实践
最近在学习curl的抓取实践, 在里面也学到了一些东西. 有一些网站需要cookie才可以抓取成功.这个时候我们就可以通过fiddle4 去抓包实现.然后通过构建头部信息 ,绕过网站端的验证. 以下是 ...
- HBase数据存取流程
一.HBase的特点是什么 1.HBase一个分布式的基于列式存储或者行式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于 ...
- 使用Android studio搭建Android环境
最近安装Android studio遇到了很多问题,现在总结一下安装过程 因为我的电脑是AMD的cpu,好像不能使用虚拟机(具体原因不知道),所以我使用 软件+手机 去开发APP 先说一下使用And ...
- UWP Test Webview JS interact, new window open (新窗口打开)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...