使用compass更高效的編輯css --- 圖片精靈

來源:互聯網
上載者:User

標籤:

  compass是sass的一個庫,關係相當於js中的jq。比較可惜的是compass的文檔比較混亂 看的不太舒服,下面是compass的文檔地址,有興趣的可以去看看

  http://compass-style.org/help/documentation/

  在這裡我就不仔細講compass的知識了,可以去阮一峰的部落格裡面有一遍compass的介紹,附上連結地址

  http://www.ruanyifeng.com/blog/2012/11/compass.html

  這裡主要講的是圖片精靈,我們在網上可以找到很多關於compass使用圖片精靈的方法,比較可惜的是不太滿足我們的需求,所以做了如下改變

  編譯之前的sass代碼:

@charset "utf-8";@import "compass/utilities/sprites";@import "compass/css3/background-size";// 公用圖片精靈的配置資訊$sprites_common: sprite-map("common/*.png", $spacing: 10px, $layout: vertical,$sort-by: ‘!width‘,$position:10px,$repeat:no-repeat,$clean-up:true);@mixin img-sprite($sprite, $name, $sprite-name) {    //擷取當前表徵圖的圖片名字    $imgurl: sprite-file($sprite, $name);    //擷取當前表徵圖的位置    $pos: sprite-position($sprite, $name);    //根據名字擷取擷取長度和寬度,並設定    width: image-width($imgurl) / 2;    height: image-height($imgurl) / 2;        //設定表徵圖的位置    background-position: nth($pos, 1) / 2 nth($pos, 2) / 2;    //使用compass的Css3模組 相容處理背景圖片大小    @include background-size(ceil(image-width(sprite-path($sprite)) / 2) auto);}@mixin img-common($name) {    @include img-sprite($sprites_common, $name, "common");}//設定icon的圖片資訊.common{    //下面這句代碼會觸發表徵圖合成一張圖    background-image: sprite-url($sprites_common);    background-repeat: no-repeat;    display: inline-block;    .icon-baike{        @include img-common(‘ic_baike‘)    }    .icon-ic_card_gongshang{        @include img-common(‘ic_card_gongshang‘)    } }

  編譯之後的css檔案:

/* line 29, ../sass/ie.scss */.common {  background-image: url(‘/img/common-sbb7f65b409.png‘);  background-repeat: no-repeat;  display: inline-block;}/* line 34, ../sass/ie.scss */.common .icon-baike {  width: 18px;  height: 20px;  background-position: -5px -1870px;  -moz-background-size: 59px auto;  -o-background-size: 59px auto;  -webkit-background-size: 59px auto;  background-size: 59px auto;}/* line 37, ../sass/ie.scss */.common .icon-ic_card_gongshang {  width: 30px;  height: 30px;  background-position: -5px -743px;  -moz-background-size: 59px auto;  -o-background-size: 59px auto;  -webkit-background-size: 59px auto;  background-size: 59px auto;}

使用compass更高效的編輯css --- 圖片精靈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.