前端学习Web前端之路程序员

compass自动生成雪碧图

2016-04-07  本文已影响1051人  webCoder

为了减少网页中的请求数,我们通常会将很多icon拼接到一个大的图片中,通过background和background-position来控制元素的显示。但是,拼接雪碧图以及后续的具体元素在雪碧图中位置的确定都是很麻烦的,精确也不准确。
今天,就来介绍一种简便的方法。使用此方法,当拼接工作量大时,能够很大程度地减少前端的工作;而且不会出现像素上的差别,十分准确。

1.环境配置

  gem install sass

但是往往会报:

gem 在国内的镜像不能用
Could not find a valid gem 'sass' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - Errno::E...

所以我们来使用淘宝的镜像,执行以下操作:

gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org
按照步骤进行就行,之后再进行如下命令:
sudo gem install sass
sudo gem install compass
ruby -v
sass -v
compass -v

2.compass项目

compass init

会在命令行输出以下内容,表示初始化成功:

directory sass/ 
directory stylesheets/ 
   create config.rb 
   create sass/screen.scss 
   create sass/print.scss 
   create sass/ie.scss 
    write stylesheets/ie.css
    write stylesheets/print.css
    write stylesheets/screen.css
Congratulations! Your compass project has been created.
require 'compass/import-once/activate'
# Require any additional compass plugins here.
# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "stylesheets"
sass_dir = "sass"
images_dir = "images"
javascripts_dir = "javascripts"
# You can select your preferred output style here (can be overridden via the command line):
# 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
根据config.rb中的设置,我们需要将图片放在images文件夹下
所以,我们先在项目根目录下新建一个images文件夹,
比如我们现在项目㓟多个模块,每个模块需要对应一个雪碧图,
那么我们在images中新建多个文件夹保存原始的图片;
@import "compass/utilities/sprites";    // 加载compass sprites模块
@import "share/*.png";                    // 导入share目录下所有png图片
@include all-share-sprites;                // 输出所有的雪碧图css
compass compile

如果命令执行成功,我们可以在images文件夹下生成一个以share开头的文件,比如我这的是:share-s0876535d58.png。

pic2.png

同时,我们在stylesheets文件夹下,看到新生成的share.css文件,内容如下:

/* line 56, share/*.png */
.share-sprite, .share-github, .share-weibo {
  background-image: url('/images/share-s0876535d58.png');
  background-repeat: no-repeat;
}

/* line 84, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.3/stylesheets/compass/utilities/sprites/_base.scss */
.share-github {
  background-position: 0 0;
}

/* line 84, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.3/stylesheets/compass/utilities/sprites/_base.scss */
.share-weibo {
  background-position: 0 -23px;
}

到此为止,基础的compass生成雪碧图的操作就完成了。使用此方法,当拼接工作量大时,能够很大程度地减少前端的工作;而且不会出现像素上的差别,十分准确。
上一篇下一篇

猜你喜欢

热点阅读