yarn add gulp yarn add gulp.spritesmith 本次依賴的版本是: "gulp": "^3.9.1" "gulp.spritesmith": "^6.5.1"
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/")) })
圖片:javascript
less:css
.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;}