博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用gulp 插件gulp.spritesmith 完成小图合成精灵图,并自动输出样式文件
阅读量:7191 次
发布时间:2019-06-29

本文共 7009 字,大约阅读时间需要 23 分钟。

安装依赖

yarn add gulpyarn add gulp.spritesmith本次依赖的版本是:"gulp": "^3.9.1""gulp.spritesmith": "^6.5.1"

 

gulpfile.js的配置

 

const gulp = require("gulp")// 本实例为完成精灵图的合并const spritesmith = require("gulp.spritesmith")gulp.task('sprite',function(){    gulp.src("images/*.png")        .pipe(spritesmith({            imgName:'images/sprite.png', //合并后大图的名称            cssName:'css/sprite.less',            padding:2,// 每个图片之间的间距,默认为0px            cssTemplate:(data)=>{            // data为对象,保存合成前小图和合成打大图的信息包括小图在大图之中的信息               let arr = [],                    width = data.spritesheet.px.width,                    height = data.spritesheet.px.height,                    url =  data.spritesheet.image                // console.log(data)                data.sprites.forEach(function(sprite) {                    arr.push(                        ".icon-"+sprite.name+                        "{"+                            "background: url('"+url+"') "+                            "no-repeat "+                            sprite.px.offset_x+" "+sprite.px.offset_y+";"+                            "background-size: "+ width+" "+height+";"+                            "width: "+sprite.px.width+";"+                                                    "height: "+sprite.px.height+";"+                        "}\n"                    )                  })                // return "@fs:108rem;\n"+arr.join("")                return arr.join("")            }        }))        .pipe(gulp.dest("dest/"))})

  

  

 

 

未合并前的效果

小图文件未合并前的效果如下(小图一共40张)

合并之后的效果:图片与less

图片:

less:

.icon-aim{
background: url('../images/sprite.png') no-repeat -34px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-behance{
background: url('../images/sprite.png') no-repeat 0px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-blogger{
background: url('../images/sprite.png') no-repeat 0px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-deviant-art{
background: url('../images/sprite.png') no-repeat -34px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-digg{
background: url('../images/sprite.png') no-repeat -68px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-dribbble{
background: url('../images/sprite.png') no-repeat -68px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-dropbox{
background: url('../images/sprite.png') no-repeat 0px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-etsy{
background: url('../images/sprite.png') no-repeat -34px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-evernote{
background: url('../images/sprite.png') no-repeat -68px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-facebook{
background: url('../images/sprite.png') no-repeat -102px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-feed{
background: url('../images/sprite.png') no-repeat -102px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-flickr{
background: url('../images/sprite.png') no-repeat -102px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-forrst{
background: url('../images/sprite.png') no-repeat 0px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-foursquare{
background: url('../images/sprite.png') no-repeat -34px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-github{
background: url('../images/sprite.png') no-repeat -68px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-google+{
background: url('../images/sprite.png') no-repeat -102px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-google{
background: url('../images/sprite.png') no-repeat -136px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-instagram{
background: url('../images/sprite.png') no-repeat -136px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-last-fm{
background: url('../images/sprite.png') no-repeat -136px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-linked-in{
background: url('../images/sprite.png') no-repeat -136px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-mail{
background: url('../images/sprite.png') no-repeat 0px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-myspace{
background: url('../images/sprite.png') no-repeat -34px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-paypal{
background: url('../images/sprite.png') no-repeat -68px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-picasa{
background: url('../images/sprite.png') no-repeat -102px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-pinterest{
background: url('../images/sprite.png') no-repeat -136px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-rdio{
background: url('../images/sprite.png') no-repeat -170px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-reddit{
background: url('../images/sprite.png') no-repeat -170px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-share-this{
background: url('../images/sprite.png') no-repeat -170px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-skype{
background: url('../images/sprite.png') no-repeat -170px -102px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-soundcloud{
background: url('../images/sprite.png') no-repeat -170px -136px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-spotify{
background: url('../images/sprite.png') no-repeat 0px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-stumbleupon{
background: url('../images/sprite.png') no-repeat -34px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-tumblr{
background: url('../images/sprite.png') no-repeat -68px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-twitter{
background: url('../images/sprite.png') no-repeat -102px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-viddlr{
background: url('../images/sprite.png') no-repeat -136px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-vimeo{
background: url('../images/sprite.png') no-repeat -170px -170px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-wordpress{
background: url('../images/sprite.png') no-repeat -204px 0px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-xing{
background: url('../images/sprite.png') no-repeat -204px -34px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-yahoo{
background: url('../images/sprite.png') no-repeat -204px -68px;background-size: 236px 202px;width: 32px;height: 32px;}.icon-youtube{
background: url('../images/sprite.png') no-repeat -204px -102px;background-size: 236px 202px;width: 32px;height: 32px;}

 

转载于:https://www.cnblogs.com/lakeInHeart/p/7252240.html

你可能感兴趣的文章
CSS列表逆序
查看>>
javascript方法--bind()
查看>>
spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
查看>>
Install fail! Error: [@@babel/runtime/core-js/object/keys]
查看>>
基于vue 的 UI框架 -- Mint UI
查看>>
redux-saga 异步流
查看>>
Mina入门实例(一)
查看>>
HTML基本标签
查看>>
AngularJs学习——实现数据绑定的三种方式
查看>>
Ubuntu 安装yii2 advanced版 遇到的坑
查看>>
UVA - 11400 Lighting System Design
查看>>
[HNOI2005]狡猾的商人
查看>>
剑指offer(二十三,二十四,二十五)最小的k个数,连续子数组的最大和,链表中环的入口节点...
查看>>
linux下面如何让一个软件/命令开机自启动
查看>>
P4306 [JSOI2010]连通数
查看>>
RegExp
查看>>
提升软件的用户体验
查看>>
jquery 替换节点实例
查看>>
jQuery中$(this)与this的区别
查看>>
5分钟构建无服务器敏感词过滤后端系统(基于FunctionGraph)
查看>>