Use the gulp -spritesmith property to generate rules: hover instead of the class selector

I use gulp-spritesmithto create my sprite and I run into a problem: I want some of the generated styles to be properties for hover rules, not class selectors. Adding a class to the mouseover event looks ugly, and I don't see it as a solution.

For example:

.icon-sr_ext_icon_right {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -300px -100px;
  width: 50px;
  height: 50px;
}
.icon-sr_ext_icon_right_hovered {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -222px -200px;
  width: 50px;
  height: 50px;
}

To be:

.icon-sr_ext_icon_right {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -300px -100px;
  width: 50px;
  height: 50px;
}
.icon-sr_ext_icon_right:hover{
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -222px -200px;
  width: 50px;
  height: 50px;
}

Here is my gulp task code:

gulp.task('external_sprite', function() {
    var spriteData =
        gulp.src(paths.external.sprite)
            .pipe(plugins.debug( { title: "Processing image for external sprite:" } ) )
            .pipe(plugins.spritesmith({
                imgName: 'external_sprite.png',
                imgPath: '/imgs/static/external_sprite.png',
                cssName: 'external_sprite.css'
            }));

    spriteData.img.pipe(gulp.dest('./www/imgs/static/'));
    spriteData.css.pipe(gulp.dest('./' + paths.external.src)); 
});
+4
source share
1 answer

, sass. , css sass :

@import 'external_sprite';
.icon-sr_ext_icon_right:hover {
    @extend .icon-sr_ext_icon_right_hovered;
}

github. cssOpts.cssClass:

cssOpts: {
  cssSelector: function (item) {
    // If this is a hover sprite, name it as a hover one (e.g. 'home-hover' -> 'home:hover')
    if (item.name.indexOf('-hover') !== -1) {
      return '.sprite-' + item.name.replace('-hover', ':hover');
    // Otherwise, use the name as the selector (e.g. 'home' -> 'home')
    } else {
      return '.sprite-' + item.name;
    }
  }
}

, .scss .

+4

Source: https://habr.com/ru/post/1611947/


All Articles