- sprite) {
- $sprite-offset-x: nth($sprite, 1);
- $sprite-offset-y: nth($sprite, 2);
- background-position: $sprite-offset-x $sprite-offset-y;
- }
- @mixin sprite($sprite) {
- @include sprite-position($sprite);
- @include sprite-width($sprite);
- @include sprite-height($sprite);
- }
- {{#sprite}}
- {{class}} {
- background-repeat: no-repeat;
- overflow: hidden;
- border: none;
- background: url('{{{escaped_image}}}?v=#{$version}');
- @include inline-block();
- vertical-align: middle;
- font-style: normal;
- color:$icon-font-color;
- }
- {{/sprite}}
- {{#items}}
- .{{name}}{
- @include sprite(${{name}});
- }
- {{/items}}
搞定!就是那麼簡單。
現在只需要把圖片丟到icon文件夾裡面,運行下gulp sprites,就可以生成一張雪碧圖icon.png和相對應的scss樣式文件_icon.sCSS了,也可以再新建一個監聽的任務,監聽icon文件夾,這樣就可以實時生成了。
我把_icon.sCSS貼出來:
CSS Code復制內容到剪貼板
- $icon-QQ: -262px -161px 60px 60px;
- $icon-email: -332px -161px 60px 60px;
- $icon-skype: -5px -252px 60px 60px;
- $icon-phone: -75px -252px 60px 60px;
- @mixin sprite-width($sprite) {
- width: nth($sprite, 3);
- }
- @mixin sprite-height($sprite) {
- height: nth($sprite, 4);
- }
- @mixin sprite-position($sprite) {
- $sprite-offset-x: nth($sprite, 1);
- $sprite-offset-y: nth($sprite, 2);
- background-position: $sprite-offset-x $sprite-offset-y;
- }
- @mixin sprite($sprite) {
- @include sprite-position($sprite);
- @include sprite-width($sprite);
- @include sprite-height($sprite);
- }
- .icon {
- background-repeat: no-repeat;
- overflow: hidden;
- border: none;
- background: url('#{$icon-sprite-path}/icon.png?v=#{$version}');
- @include inline-block();
- vertical-align: middle;
- font-style: normal;
- color:$icon-font-color;
- }
- .icon-QQ{
- @include sprite($icon-QQ);
- }
- .icon-email{
- @include sprite($icon-email);
- }
- .icon-skype{
- @include sprite($icon-skype);
- }
- .icon-phone{
- @include sprite($icon-phone);
- }
使用的時候只需要加上類似class="icon icon-QQ",就可以了。