npm i gulp gulp-stylus gulp.spritesmith --save
.png
and .jpg/.jpeg
extensions are supported (depends on the engine used)imgOpts.format
property.css
(CSS), .sass
(SASS), .scss
(SCSS), .less
(LESS), .styl/.stylus
(Stylus), and .json
(JSON)cssFormat
propertyauto
and the most appropriate engine on your system will be usedphantomjs
, canvas
, gm
, pngsmith
top-down
values (default), left-right
, diagonal
, alt-diagonal
, binary-tree
png
and jpg
supported (depends on the engine used)gm
enginephantomjs
engine)true
defaultimagemagick
instead of graphicsmagick
(available only in gm
)css
(CSS), sass
(SASS), scss
(SCSS), scss_maps
(SCSS using map notation), less
(LESS), stylus
(Stylus) and json
(JSON) valuesmustache
template, giving the opportunity to customize the CSS file at the output gulp.task('sprite', function() { var spriteData = gulp.src('./src/assets/images/sprite/*.*') // , .pipe(spritesmith({ imgName: 'sprite.png', cssName: 'sprite.css', })); spriteData.img.pipe(gulp.dest('./built/images/')); // , spriteData.css.pipe(gulp.dest('./built/styles/')); // , });
/* Icon classes can be used entirely standalone. They are named after their original file names. ```html <i class="icon-home"></i> ``` */ .icon-home { background-image: url(sprite.png); background-position: 0px 0px; width: 16px; height: 16px; } .icon-home_hover { background-image: url(sprite.png); background-position: 0px -16px; width: 16px; height: 16px; } .icon-instagram { background-image: url(sprite.png); background-position: 0px -32px; width: 16px; height: 16px; } .icon-instagram_hover { background-image: url(sprite.png); background-position: 0px -48px; width: 16px; height: 16px; } .icon-pin { background-image: url(sprite.png); background-position: 0px -64px; width: 12px; height: 16px; } .icon-pin_hover { background-image: url(sprite.png); background-position: 0px -80px; width: 12px; height: 16px; } .icon-tras_hover { background-image: url(sprite.png); background-position: 0px -96px; width: 16px; height: 16px; } .icon-trash { background-image: url(sprite.png); background-position: 0px -112px; width: 16px; height: 16px; } .icon-user { background-image: url(sprite.png); background-position: 0px -128px; width: 16px; height: 16px; } .icon-user_hover { background-image: url(sprite.png); background-position: 0px -144px; width: 16px; height: 16px; }
binary-tree
image distribution algorithm. All variables, for clarity, I give the prefix s-
. I turn off the generation of mixins and put them into a separate file. And I create my own CSS template, because by default a lot of unnecessary garbage is generated, which decently inflates the file and is not used by me. gulp.task('sprite', function() { var spriteData = gulp.src('./src/assets/images/sprite/*.*') // , .pipe(spritesmith({ imgName: 'sprite.png', cssName: 'sprite.styl', cssFormat: 'stylus', algorithm: 'binary-tree', cssTemplate: 'stylus.template.mustache', cssVarMap: function(sprite) { sprite.name = 's-' + sprite.name } })); spriteData.img.pipe(gulp.dest('./built/images/')); // , spriteData.css.pipe(gulp.dest('./src/styles/')); // , });
$s-book = 16px 0px -16px 0px 16px 16px 80px 64px 'sprite.png'; $s-book_hover = 48px 16px -48px -16px 16px 16px 80px 64px 'sprite.png'; $s-comments = 0px 16px 0px -16px 16px 16px 80px 64px 'sprite.png'; $s-comments_hover = 16px 16px -16px -16px 16px 16px 80px 64px 'sprite.png'; $s-compose = 32px 0px -32px 0px 16px 16px 80px 64px 'sprite.png'; $s-compose_hover = 32px 16px -32px -16px 16px 16px 80px 64px 'sprite.png'; $s-faceboo_hover = 0px 32px 0px -32px 16px 16px 80px 64px 'sprite.png'; $s-facebook = 16px 32px -16px -32px 16px 16px 80px 64px 'sprite.png'; $s-globe = 32px 32px -32px -32px 16px 16px 80px 64px 'sprite.png'; $s-globe_hover = 48px 0px -48px 0px 16px 16px 80px 64px 'sprite.png'; $s-home = 0px 0px 0px 0px 16px 16px 80px 64px 'sprite.png'; $s-home_hover = 48px 32px -48px -32px 16px 16px 80px 64px 'sprite.png'; $s-instagram = 0px 48px 0px -48px 16px 16px 80px 64px 'sprite.png'; $s-instagram_hover = 16px 48px -16px -48px 16px 16px 80px 64px 'sprite.png'; $s-pin = 32px 48px -32px -48px 12px 16px 80px 64px 'sprite.png'; $s-pin_hover = 44px 48px -44px -48px 12px 16px 80px 64px 'sprite.png'; $s-tras_hover = 64px 0px -64px 0px 16px 16px 80px 64px 'sprite.png'; $s-trash = 64px 16px -64px -16px 16px 16px 80px 64px 'sprite.png'; $s-user = 64px 32px -64px -32px 16px 16px 80px 64px 'sprite.png'; $s-user_hover = 64px 48px -64px -48px 16px 16px 80px 64px 'sprite.png';
mixins.styl
.mixins.styl
file: spriteWidth($sprite) { width: $sprite[4]; } spriteHeight($sprite) { height: $sprite[5]; } spritePosition($sprite) { background-position: $sprite[2] $sprite[3]; } spriteImage($sprite) { background-image: url(../images/$sprite[8]); } sprite($sprite) { spriteImage($sprite) spritePosition($sprite) spriteWidth($sprite) spriteHeight($sprite) }
sprite($sprite)
. Instead of $sprite
we $sprite
the variable necessary to us. For example, sprite($s-home)
. background-image:url("../images/sprite.png"); background-position:0 0; width:16px; height:16px
:hover
and :active
icon will flash. This happens because the sprite
mixin generates a background-image
each time and the browser substitutes this image when it is hover.stylus
, a solution was found.spriteImage($sprite)
.mixin
sprite($sprite) if !match('hover', selector()) && !match('active', selector()) spriteImage($sprite) spritePosition($sprite) spriteWidth($sprite) spriteHeight($sprite)
spritePosition($sprite)
Source: https://habr.com/ru/post/227945/
All Articles