Answer the question
In order to leave comments, you need to log in
How to work with spritesmith in a SCSS project?
Good afternoon!
I'm trying to figure out spritesmith and automatic sprite generation. There are very few guides on the Internet, and it's hard for me to figure it out, due to my little experience.
In general, what we have.
SCSS + Gulp project.
Guided by an article on Habré , as well as a question on Toaster , I received the following in my project.
Code in Gulp file.
gulp.task('sprite', function() {
var spriteData =
gulp.src('src/img/sprite/*.*') // путь, откуда берем картинки для спрайта
.pipe(spritesmith({
imgName: 'sprite.png',
cssName: '_sprite.scss',
cssFormat: 'scss',
algorithm: 'binary-tree',
padding: 1,
cssTemplate: 'scss.template.mustache',
cssVarMap: function(sprite) {
sprite.name = 's-' + sprite.name
}//
}));
spriteData.img.pipe(gulp.dest('src/img/')); // путь, куда сохраняем картинку
spriteData.css.pipe(gulp.dest('src/sass/')); // путь, куда сохраняем стили
});
{{#items}}
${{name}}: {{px.x}} {{px.y}} {{px.offset_x}} {{px.offset_y}} {{px.width}} {{px.height}} {{px.total_width}} {{px.total_height}} '{{{escaped_image}}}';
{{/items}}
@mixin spriteWidth($sprite) {
width: nth($sprite,5);
}
@mixin spriteHeight($sprite) {
height: nth($sprite,6);
}
@mixin spritePosition($sprite) {
background-position: nth($sprite,3) nth($sprite,4);
}
@mixin spriteImage($sprite) {
background-image: nth($sprite,9);
}
@mixin sprite($sprite) {
@include spriteImage($sprite);
@include spritePosition($sprite);
@include spriteWidth($sprite);
@include spriteHeight($sprite);
}
$s-ic1-hover: 0px 0px 0px 0px 24px 24px 74px 49px 'sprite.png';
$s-ic1: 25px 0px -25px 0px 24px 24px 74px 49px 'sprite.png';
$s-ic2-hover: 0px 25px 0px -25px 24px 24px 74px 49px 'sprite.png';
$s-ic2: 25px 25px -25px -25px 24px 24px 74px 49px 'sprite.png';
$s-ic3-hover: 50px 0px -50px 0px 24px 24px 74px 49px 'sprite.png';
$s-ic3: 50px 25px -50px -25px 24px 24px 74px 49px 'sprite.png';
.block {
background: $s-ic1;
}
. Answer the question
In order to leave comments, you need to log in
In general, I found an error.
.block {
@include sprite($s-ic1);
}
@mixin spriteImage
@mixin spriteImage($sprite) {
background-image: url( unquote('../img/' + nth($sprite,9)));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question