Ale*_*rev 5 javascript css sprite gulp gulp-spritesmith
我正在用来gulp-spritesmith生成我的精灵,但我面临一个问题:我希望某些生成的样式成为悬停规则的属性,而不是类选择器的属性。在鼠标悬停事件上添加类看起来很难看,我不认为它是一个解决方案。
例如:
.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;
}
Run Code Online (Sandbox Code Playgroud)
成为:
.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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 gulp 任务代码:
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));
});
Run Code Online (Sandbox Code Playgroud)
我找到了一种使用技术自动创建悬停效果样式的方法sass。简单地说,生成精灵,然后将生成的 css 导入到另一个 sass 文件并扩展所需的类:
@import 'external_sprite';
.icon-sr_ext_icon_right:hover {
@extend .icon-sr_ext_icon_right_hovered;
}
Run Code Online (Sandbox Code Playgroud)
插件的主要贡献者在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
.scss但如果您设置样式文件的扩展名,则此解决方案不起作用。
| 归档时间: |
|
| 查看次数: |
773 次 |
| 最近记录: |