由于最近没什么时间好好写博文,我把用sass做雪碧图的关键点贴出来方便自己记忆:

config.rb注释

# Set this to the root of your project when deployed:
#配置服务器路径
http_path = "http//:www.baidu.com/" #配置css sass images javascripts路径
css_dir = "public/stylesheets"
sass_dir = "public/sass"
images_dir = "public/images"
javascripts_dir = "javascripts" # You can select your preferred output style here (can be overridden via the command line):
#根据个人偏好选择输出样式 :nested嵌套 :compact紧密 :compressed压缩
# output_style = :expanded or :nested or :compact or :compressed # To enable relative paths to assets via compass helper functions. Uncomment:
#相对路径
relative_assets = true # To disable debugging comments that display the original location of your selectors. Uncomment:
# line_comments = false # If you prefer the indented syntax, you might want to regenerate this
# project again passing --syntax sass, or you can uncomment this:
# preferred_syntax = :sass
# and then run:
# sass-convert -R --from scss --to sass sass scss && rm -rf sass && mv scss sass

sass写法生产sprites:

@import "compass/utilities/sprites";    // 加载compass sprites模块
$book-spacing:100px; // 配置所有sprite间距为100px,默认为0px 此句要放在前面才生效
$book-position: 100px; // 配置所有sprite的位置,默认为0px
$book-base-class:"pfan";
$book-sprite-dimensions:true; //自动给每个html选择器添加宽度width和高度height
//$book-layout:smart; //智能布局的把每张图像放在最合适的地方
//$book-layout:horizontal; //水平排列
$book-layout:vertical; //纵向排列
//$book-layout:diagonal; //对角线布局,最浪费资源 @import "book/*.png"; // 导入share目录下所有png图片
@include all-book-sprites(); // 输出所有的雪碧图css
//$<map>-<sprite>-spacing: 10px; // 配置单个sprite间距为10px,默认继承$<map>-spacing的值
//$<map>-<sprite>-position: 0px; // 配置单个sprite的位置,默认继承$<map>-position的值

做雪碧图有两种方式:

  第一种,简单粗暴:

//导入雪碧图  通过@import导进图片,然后再通过@include合并成雪碧图
//@import "normal/*.png";
//@include all-normal-sprites;

  第二种,精细化,每个去做:

//引进图片合并给一个变量(后面会用到这个变量)
$sprites:sprite-map("leave/*.png");

  做移动端记得要设置间距

$<map>-spacing:100px; 

  

  第一种方案,我就不做太多介绍了,说说第二种,来个例子

//引进图片合并给一个变量(后面会用到这个变量)
$sprites:sprite-map("leave/*.png"); .test{
display:block;
background-repeat:no-repeat;
background-image:sprite-url($sprites);//获取整个雪碧图路径
background-position:sprite-position($sprites,update); //获取当个文件所移动的位置
width:image-width(sprite-file($sprites,update)); //设置ico宽度高度
height:image-height(sprite-file($sprites,update));
}

  生成代码:

.test {
display: block;
background-repeat: no-repeat;
background-image: url('../images/leave-s1df1db3dd3.png');
background-position: -86px;
width: 67px;
height: 25px;
}

最后附上雪碧图PC\WAP端引用的@mixin

//雪碧图mixin引块,因为目前编译不过GIF,故暂用png8
$media:false;
@mixin iconItem($sprites,$name,$iE6:null){
background-image:sprite-url($sprites) no-repeat; //获取整个雪碧图路径
@if $iE6 != null{ //null
_background-image:sprite-url($iE6) no-repeat; //此处传进来的格式须是png8
}
$width:image-width(sprite-file($sprites,$name)); //sprite-file 获取目标图片
$height:image-height(sprite-file($sprites,$name)) //获取目标图片
@if $media{//wap
height:ceil($height / );
width:ceil($width / ); //sprite-position 获取目标图的位置
background-position: round(nth(sprite-position($sprites,$name),)/)
round(nth(sprite-position($sprites,$name),)/);
background-size:ceil($width / ) auto;
} @else{//PC
height:$height;
width:$width;
background-position:sprite-position($sprites,$name);
}
}

另一个:

//compass 二倍图转rem
@mixin s2b($sprite, $name, $toRem:true) {
$pos_x: floor(nth(sprite-position($sprite, $name), ) / );
$pos_y: floor(nth(sprite-position($sprite, $name), ) / );
$width: ceil(image-width(sprite-file($sprite, $name)) / );
$height: ceil(image-height(sprite-file($sprite, $name)) / );
$size_w: ceil(image-width(sprite-path($sprite)) / );
$size_h: ceil(image-height(sprite-path($sprite)) / );
@if $toRem {
$pos_x: pxTorem($pos_x);
$pos_y: pxTorem($pos_y);
$width: pxTorem($width);
$height: pxTorem($height);
$size_w: pxTorem($size_w);
$size_h: pxTorem($size_h);
}
background-image: $sprite;
background-repeat: no-repeat;
background-position: $pos_x $pos_y;
background-size: $size_w $size_h;
//background-size: $size_w auto;
height: $height;
width: $width;
}

我自己的(这里面有一点要注意雪碧地图,加行间距要这样$sprites:sprite-map("leave/*.png",$spacing:10px,$layout: vertical);列到里面):

/*引进图片合并给一个变量(后面会用到这个变量)*/
$sprites:sprite-map("leave/*.png",$spacing:10px,$layout: vertical); /*转换px到rem*/
$browser-default-font-size : 20px !default;
@function pxTorem($px){
@if $px == {$px:0px}
@return $px / $browser-default-font-size * 1rem;
}
@function pxTo2rem($px){
@if $px == {$px:0px}
@return $px / ($browser-default-font-size*) * 1rem;
} /*雪碧图mixin引块,pc和移动端因为目前编译不过GIF,故暂用png8*/
$media:true;
@mixin iconItem($sprites,$name,$iE6:null){
background:sprite-url($sprites) no-repeat; //获取整个雪碧图路径
@if $iE6 != null{ //null
_background:sprite-url($iE6) no-repeat; //此处传进来的格式须是png8
}
$width:image-width(sprite-file($sprites,$name)); //sprite-file 获取目标图片
$height:image-height(sprite-file($sprites,$name)); //获取目标图片
$toalWidth:image-width(sprite-path($sprites)); //获取整张图的宽度
$totalHeight:image-height(sprite-path($sprites)); //获取整张图的高度
$widthHalf:ceil($width/); //获取宽度的一半
$heightHalf:ceil($height/); //获取高度的一半
//sprite-position 获取目标图的位置,nth操作数组
$posX:round(nth(sprite-position($sprites,$name),)); //获取沿x轴的位移
$posY:round(nth(sprite-position($sprites, $name), )); //获取沿y轴的位移
@if $media{//wap
height:pxTorem($heightHalf);
width:pxTorem($widthHalf);
font:$posX;
font:$posY;
background-position: pxTo2rem($posX) pxTo2rem($posY);
background-size:pxTo2rem($toalWidth) pxTo2rem($totalHeight);
} @else{//PC
height:$height;
width:$width;
background-position:sprite-position($sprites,$name);
}
}
/*带时间戳的图片,pc和移动端 ,$imgUrl必须带文件夹和文件名字符串,例"icon/pig.png"*/
@mixin timestampImg($imgUrl){
background:image-url($imgUrl) no-repeat;
$width:image-width($imgUrl);
$height:image-height($imgUrl);
@if $media{ //wap
width:pxTo2rem($width);
height:pxTo2rem($height);
background-size:pxTo2rem($width) pxTo2rem($height);
} @else{
height:$height;
width:$width;
}
}
/*base64位的图片,pc和移动端 ,$imgUrl必须带文件夹和文件名字符串,例"icon/pig.png"*/
@mixin base64Img($imgUrl){
background:inline-image($imgUrl) no-repeat;
$width:image-width($imgUrl);
$height:image-height($imgUrl);
@if $media{ //wap
width:pxTo2rem($width);
height:pxTo2rem($height);
background-size:pxTo2rem($width) pxTo2rem($height);
} @else{
height:$height;
width:$width;
}
}

compass制作雪碧图参考资料:

  使用compass自动合并css雪碧图

  使用Compass生成雪碧图

  sass技巧:compass制作“雪碧图”

  【Sass中级】使用Sass和Compass制作雪碧图

compass做雪碧图的更多相关文章

  1. 使用Compass制作雪碧图

    遇见好的文章,笔者也会转载.但是正所谓好记性不如烂笔头,单纯的拿来主义也不如自己的亲自实践.所以每次需要转载的文章,我都会自己敲一遍,中间加入一些自己的思考. 这篇文章转载自:http://www.h ...

  2. 使用sass与compass合并雪碧图(二)

    上一篇文章介绍了怎样使用compass合并雪碧图,生成的icons.css文件中单位是px,PC端可以直接在html文件中使用,但在移动端,我们需要根据不同分辨率的屏幕,来缩放图片大小,显然使用px单 ...

  3. 利用compass制作雪碧图

    compass是什么?是sass一款神奇插件,具体教程,我还是推荐阮一峰sass,compass教程,简单清晰明了. 用ps制作雪碧图,工作效率太低了.用compass来制作,方便很多.下图的用com ...

  4. 使用sass与compass合并雪碧图(一)

    雪碧图就是很多张小图片合并成一张大图片,以减少HTTP请求,从而提升加载速度.有很多软件可以合并雪碧图,但通常不太容易维护,使用compass生成雪碧图应该算是非常方便的方法了,可以轻松的生成雪碧图, ...

  5. compass Sprites 雪碧图 小图片合成[Sass和compass学习笔记]

    demo 源码 地址 https://github.com/qqqzhch/webfans 什么是雪碧图? CSS雪碧 即CSS Sprites,也有人叫它CSS精灵,是一种CSS图像合并技术,该方法 ...

  6. Sass和Compass制作雪碧图

    1.安装好了sass与compass之后设置一个配置文件 2.新增一个雪碧图文件夹用来存放将要合并的图片例如color文件夹 3.@import命令引用 .Compass看到@import指令的参数为 ...

  7. compass与css sprite(雪碧图)

    什么是css sprite? css sprite,中文叫雪碧图,也有人喊CSS精灵,就是一种背景拼合的技术,然后通过background-position来显示雪碧图中需要显示的图像. MDN相关链 ...

  8. compass框架的sprite雪碧图的用法简要

    ---恢复内容开始--- **简介** CSS SPRITE 即 CSS雪碧,即是将诸多图片合成一张图片,然后使用CSS 的background和background-position属性渲染. 这样 ...

  9. 使用compass自动合并css雪碧图(css sprite)

    本文转载自: 使用compass自动合并css雪碧图(css sprite)

随机推荐

  1. 【百题留念】hdoj 1524 A Chess Game(dfs + SG函数应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1524 #include<stdio.h> #include<cstring> ...

  2. IOS深入学习(19)之View object

    1 前言 本章主要介绍了View视图对象,包括了其属性,视图间关系和CALayer的简介. 英文原文:http://blog.csdn.net/developer_zhang/article/deta ...

  3. ios transition translate 闪屏问题总结

    webkit在绘制页面时会将结构分为各种层,当层足够大时就会变成很大的平铺层.这样一来webkit在每次页面结构发生变化时不需要都渲染整个页面而是渲染对应层了,这对渲染速度来说相当的重要.webkit ...

  4. Ruby学习资源汇总

    from:http://segmentfault.com/a/1190000000362058 Ruby 语言 Try Ruby: 无需在你的系统中安装.Ruby,只要通过浏览器便可立即体验 Ruby ...

  5. 分布式文件系统FastDFS设计原理

    原文地址: http://blog.chinaunix.net/uid-20196318-id-4058561.html FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker ...

  6. FLEX4中的Panel如何实现带自定义图标和按钮

      做过flex开发的程序员都知道,使用flex3中的panel自定义按钮很容易,而且flex3的panel有icon属性.但是flex4的中大部分的控件与flex3中的控件实现方式有很大的变化,同是 ...

  7. CarDAQ-Plus

    Overview CarDAQ-Plus is the most validated and accepted J2534 device in the world. It has been on th ...

  8. 【C#】ASP.NET网页中添加单点登录功能

    背景 首先,要说明的是,原先需求定义的是,同一个账号只能同时有一个人来登录,如果另外一个登录的话,前一个登陆者就自动被踢掉.本来原先要做成存储到服务器的数据库中,但是后来如果是非正常退出的话 下次就没 ...

  9. ibatis 分页的简单实现

    ibatis 物理分页的简单实现 cheungmine 2013-8-26 使用SSI (Struts2, Spring3, iBATIS2.3)框架开发Web服务的时候有一个需求就是对查询得到的结果 ...

  10. iOS 2D绘图详解(Quartz 2D)之Bitmap

    什么是Bitmap? Bitmap叫做位图,每一个像素点由1-32bit组成.每个像素点包括多个颜色组件和一个Alpha组件(例如:RGBA). iOS中指出如下格式的图片 JPEG, GIF, PN ...