【发布时间】:2016-08-30 23:14:50
【问题描述】:
我有大约 196 张国旗图片,每张图片都以其两个字母的国家/地区代码命名。我使用 gulp.spritesmith npm 包生成了一个包含所有这些图像的精灵。它还生成一个 css 文件(在我的例子中是一个 scss 文件)以在我们的项目中引用它们。
为简单起见,以下生成的代码是因为这两个图像。
/*generated code*/
$ad-name: 'ad';
$ad-x: 0px;
$ad-y: 14px;
$ad-offset-x: 0px;
$ad-offset-y: -14px;
$ad-width: 19px;
$ad-height: 14px;
$ad-total-width: 19px;
$ad-total-height: 1932px;
$ad-image: 'locate-sprite.png';
$ad: (0px, 14px, 0px, -14px, 19px, 14px, 19px, 1932px, 'locate-sprite.png', 'ad', );
$ae-name: 'ae';
$ae-x: 0px;
$ae-y: 966px;
$ae-offset-x: 0px;
$ae-offset-y: -966px;
$ae-width: 19px;
$ae-height: 14px;
$ae-total-width: 19px;
$ae-total-height: 1932px;
$ae-image: 'locate-sprite.png';
$ae: (0px, 966px, 0px, -966px, 19px, 14px, 19px, 1932px, 'locate-sprite.png', 'ae', );
$spritesheet-sprites: ($ad, $ae,);
$spritesheet: (19px, 1932px, 'locate-sprite.png', $spritesheet-sprites, );
@mixin sprite-width($sprite) {
width: nth($sprite, 5);
}
@mixin sprite-height($sprite) {
height: nth($sprite, 6);
}
@mixin sprite-position($sprite) {
$sprite-offset-x: nth($sprite, 3);
$sprite-offset-y: nth($sprite, 4);
background-position: $sprite-offset-x $sprite-offset-y;
}
@mixin sprite-image($sprite) {
$sprite-image: nth($sprite, 9);
background-image: url(#{$sprite-image});
}
@mixin sprite($sprite) {
@include sprite-image($sprite);
@include sprite-position($sprite);
@include sprite-width($sprite);
@include sprite-height($sprite);
}
在我的项目中,我创建了一个 mixin,它覆盖了上面生成的 sprite mixin。如下:
@mixin blah($sprite){
&:before{
display: inline-block;
content: "\00a0";
position: relative;
vertical-align: middle;
top: -2px;
@include sprite-image($sprite);
@include sprite-position($sprite);
@include sprite-width($sprite);
@include sprite-height($sprite);
}
}
当我在我的项目中使用这个 mixin 时,我只需导入生成的 scss 文件并包含这个 mixin。下面是实现:
@import "_gen.sprite";
$country-list: ad ae;
@each $current-country in $country-list {
.imageflag-#{$current-country}{
@include blah($ad); // This works
@include blah($current-country); //This doesn't because, a string is passed instead of a variable.
margin-right: 10px;
}
}
但是,在上述实现中,我想在每个循环期间将给定的列表值(ad 和 ae)作为变量(即 $ad 和 $ae)传递。
你可能会说,你为什么不能有如下国家列表:
$country-list: $ad $ae;
我不能这样做,因为 $ad 和 $ae 值已经由 scss 文件中的插件生成,并且等效于无法传递给 mixin 的值,因为它会再次引发错误。期望一个变量而不是一个字符串。
所以,我想到了使用插值。所以,我做了以下事情:
$dollar: '$';
$country-list: ad ae;
@each $current-country in $country-list {
.imageflag-#{$current-country}{
@include blah(#{$dollar}#{$current-country}); //expected $ad and $ae but throws an error.
margin-right: 10px;
}
}
但插值不适用于美元。我无法将美元附加到字符串变量。
那么,有没有其他方法可以将美元符号插入或附加到变量或字符串以实现所需的输出。
非常感谢任何反馈或建议。
谢谢。
【问题讨论】:
-
您能否提供一个最小可重现案例的代码笔(/jsfiddle/what-have-you)?
-
@Andrew 这是 sassmeister 的要点sassmeister.com/gist/d907c9046779acf8510ef3103af86992
-
归根结底,我的问题只是“如何将'$'附加到变量”。如果我明白了,基本上就解决了所有问题。
-
似乎 mixins 只接受变量,而不是字符串:github.com/Ensighten/grunt-spritesmith/issues/51 这就是您的第一个示例有效的原因(因为它传递的是 $ad 而不是 '$ad')
-
@sammyray 是的,这是真的。我也看到了这个问题。我使用 unquote() 删除引号,但仍然出现相同的错误。我不确定,编译器实际上是如何区分正常的 $ad 和 unquote('$ad') 的输出(也是 $ad)。我评论了他们的问题以获得作者的反馈,但我还没有收到回复!
标签: css sass gulp-spritesmith