compass做雪碧图
由于最近没什么时间好好写博文,我把用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做雪碧图的更多相关文章
- 使用Compass制作雪碧图
遇见好的文章,笔者也会转载.但是正所谓好记性不如烂笔头,单纯的拿来主义也不如自己的亲自实践.所以每次需要转载的文章,我都会自己敲一遍,中间加入一些自己的思考. 这篇文章转载自:http://www.h ...
- 使用sass与compass合并雪碧图(二)
上一篇文章介绍了怎样使用compass合并雪碧图,生成的icons.css文件中单位是px,PC端可以直接在html文件中使用,但在移动端,我们需要根据不同分辨率的屏幕,来缩放图片大小,显然使用px单 ...
- 利用compass制作雪碧图
compass是什么?是sass一款神奇插件,具体教程,我还是推荐阮一峰sass,compass教程,简单清晰明了. 用ps制作雪碧图,工作效率太低了.用compass来制作,方便很多.下图的用com ...
- 使用sass与compass合并雪碧图(一)
雪碧图就是很多张小图片合并成一张大图片,以减少HTTP请求,从而提升加载速度.有很多软件可以合并雪碧图,但通常不太容易维护,使用compass生成雪碧图应该算是非常方便的方法了,可以轻松的生成雪碧图, ...
- compass Sprites 雪碧图 小图片合成[Sass和compass学习笔记]
demo 源码 地址 https://github.com/qqqzhch/webfans 什么是雪碧图? CSS雪碧 即CSS Sprites,也有人叫它CSS精灵,是一种CSS图像合并技术,该方法 ...
- Sass和Compass制作雪碧图
1.安装好了sass与compass之后设置一个配置文件 2.新增一个雪碧图文件夹用来存放将要合并的图片例如color文件夹 3.@import命令引用 .Compass看到@import指令的参数为 ...
- compass与css sprite(雪碧图)
什么是css sprite? css sprite,中文叫雪碧图,也有人喊CSS精灵,就是一种背景拼合的技术,然后通过background-position来显示雪碧图中需要显示的图像. MDN相关链 ...
- compass框架的sprite雪碧图的用法简要
---恢复内容开始--- **简介** CSS SPRITE 即 CSS雪碧,即是将诸多图片合成一张图片,然后使用CSS 的background和background-position属性渲染. 这样 ...
- 使用compass自动合并css雪碧图(css sprite)
本文转载自: 使用compass自动合并css雪碧图(css sprite)
随机推荐
- 获取父进程ID
本程序主要功能是:获取某程序的ParentProcessID 直接上代码: // parent.cpp (Windows NT/2000) // // This example will show t ...
- mysql index的长度限制
在InnoDB Storage Engine中单独一个列的最大的索引长度为767bytes,utf8字符集中,一个字符占3个字节,所以如果列的类型为char,那么要想在此列上建立索引,此列最多只能有2 ...
- 自定义Template,向其中添加新的panel
参考网站:https://www.devexpress.com/Support/Center/Example/Details/E2690 思路: 新建一个DefaultTemplate: ...
- 使用jQuery Mobile和Phone Gap开发Android应用程序
经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开 ...
- 安装nodejs和grunt以后出现 /usr/bin/env: node: No such file or directory
安装完成以后需要执行此命令 sudo ln -s /usr/bin/nodejs /usr/bin/node
- hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路
Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...
- Yeoman+Express+Angular在Linux上开发配置方法
$mkdir ExpressWithAngularTest $cd ExpressWithAngularTest choose needed components you'd like to add ...
- 使用NuGet安装EntityFramework4.2
1.下载NuGet 有两种方式下载NuGet 第一种:在微软的档案库下载,下载地址为:http://visualstudiogallery.msdn.microsoft.com/27077b70-9d ...
- Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能
Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能 二.减少 HTTP 的请求数 当用户浏览页面时,如果我们在用户第一次访问时将一些信息一次性加载到客户端缓存, ...
- iOS CocoaPods安装和使用图解
Cocoapods安装步骤 1.升级Ruby环境 sudo gem update --system 如果Ruby没有安装,请参考 如何在Mac OS X上安装 Ruby运行环境 2.安装CocoaPo ...