在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):reducejs/gulp.spritesmith-multi开源软件地址(OpenSource Url):https://github.com/reducejs/gulp.spritesmith-multi开源编程语言(OpenSource Language):JavaScript 84.6%开源软件介绍(OpenSource Introduction):gulp.spritesmith-multiA wrapper for gulp.spritesmith to generate multiple sprites and stylesheets. Examplevar gulp = require('gulp')
var spritesmith = require('gulp.spritesmith-multi')
gulp.task('default', ['clean'], function () {
return gulp.src('default/**/*.png')
.pipe(spritesmith())
.on('error', function (err) {
console.log(err)
})
.pipe(gulp.dest('build'))
})
gulp.task('watch', ['default'], function (cb) {
gulp.watch('default/**/*.png', ['default'])
}) input:
output:
hover.css .sp-hover {
background-image: url(hover.png)
}
@media (-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
.sp-hover {
background-image: url([email protected])
background-size: 150px 200px
}
}
.sp-hover__sprite1:hover {
background-position: -100px 0px
width: 50px
height: 50px
}
.sp-hover__sprite1 {
background-position: -100px -50px
width: 50px
height: 50px
}
.sp-hover__sprite2 {
background-position: -100px -100px
width: 50px
height: 50px
}
.sp-hover__sprite3 {
background-position: 0px 0px
width: 100px
height: 200px
} Continuing the pipelineYou can continue the pipeline the way the original gulp.task('sprite', ['clean'], function () {
var merge = require('merge-stream')
var imagemin = require('gulp-imagemin')
var csso = require('gulp-csso')
// Generate our spritesheet
var spriteData = gulp.src('default/**/*.png')
.pipe(spritesmith({
spritesmith: function (options) {
options.imgPath = '../images/' + options.imgName
}
}))
// Pipe image stream through image optimizer and onto disk
var imgStream = spriteData.img
.pipe(imagemin())
.pipe(gulp.dest('build/images'))
// Pipe CSS stream through CSS optimizer and onto disk
var cssStream = spriteData.css
.pipe(csso())
.pipe(gulp.dest('build/css'))
// Return a merged stream to handle both `end` events
return merge(imgStream, cssStream)
})
Optionsto(iconFile)Specify the name of the sprite into which the given icon should be included Type: If By default, icons are grouped by their directory names. spritesmithSpecify options for each sprite. Type: The following fields are set by default: var options = {
imgName: sprite + '.png',
cssName: sprite + '.css',
cssTemplate: builtin.css,
cssSpritesheetName: 'sp-' + sprite,
} You can override them through this option. If Custom templatesThe default css template is To specify custom templates,
create a templater through var gulp = require('gulp')
var path = require('path')
var spritesmith = require('..')
var util = spritesmith.util
gulp.task('theme', ['clean'], function () {
var opts = {
spritesmith: function (options, sprite, icons){
if (sprite.indexOf('hover--') !== -1) {
options.cssTemplate = themeTemplate
}
return options
},
}
var themeTemplate = util.createTemplate(
path.join(__dirname, 'template', 'css.hbs'),
[addTheme, util.addPseudoClass]
)
function addTheme(data) {
var info = data.spritesheet_info
var match = info.name.match(/hover--(\w+)/)
data.theme = match && match[1]
}
return gulp.src('sp/**/*.png')
.pipe(spritesmith(opts))
.pipe(gulp.dest('build'))
}) Input: The custom template Icons
Output:
hover--theme.css .theme .sp-hover {
background-image: url(hover--theme.png);
}
@media (-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
.theme .sp-hover {
background-image: url([email protected]);
background-size: 150px 200px;
}
}
.sp-hover__sprite1:hover {
background-position: -100px 0px;
width: 50px;
height: 50px;
}
.sp-hover__sprite1 {
background-position: -100px -50px;
width: 50px;
height: 50px;
}
.sp-hover__sprite2 {
background-position: -100px -100px;
width: 50px;
height: 50px;
}
.sp-hover__sprite3 {
background-position: 0px 0px;
width: 100px;
height: 200px;
} Retina supportAll retina icon files should be named like Responsive CSS supportResponsive CSS sprites are able to be resized in relative length units such as You can use a builtin template var gulp = require('gulp')
var spritesmith = require('..')
gulp.task('responsive-css', ['clean'], function () {
var opts = {
spritesmith: {
cssTemplate: spritesmith.builtin.responsiveCss,
},
}
return gulp.src('responsive-css/**/*.png')
.pipe(spritesmith(opts))
.pipe(gulp.dest('build'))
}) input:
output:
hover.css .sp-hover {
background-image: url(hover.png);
}
@media (-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
.sp-hover {
background-image: url([email protected]);
}
}
.sp-hover__sprite1:hover {
background-position: 100% 0;
background-size: 300%;
width: 50px;
height: 50px;
}
.sp-hover__sprite1 {
background-position: 100% 33.33333%;
background-size: 300%;
width: 50px;
height: 50px;
}
.sp-hover__sprite2 {
background-position: 100% 66.66667%;
background-size: 300%;
width: 50px;
height: 50px;
}
.sp-hover__sprite3 {
background-position: 0 0;
background-size: 150%;
width: 100px;
height: 200px;
} Though there are default Utilsexports.utilType: Methods to work with. createTemplate(tplInfo, filter)Create a templater. tplInfoSpecify template. Type:
Either one of them should be specified. If filterSpecify data for the template. If If If addPseudoClassA template data filter to support generating pseudo classes. If the icon file is something like NOTE: for retina icons,
you should name them like exports.builtinType: Templates provided by default. Pick one of them, and set |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论