我有大約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);
}
在我的項目中,我創建了一個覆蓋上面生成的sprite mixin的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;
}
}
但是,在上面的實施,我想通過給定的值列表(廣告和AE)作爲變量(即$廣告和$ AE),在每個循環中。
你可能會說,爲什麼你不能只是有國家名單如下:
$country-list: $ad $ae;
這是因爲,$廣告和$ 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;
}
}
但是插值對美元不起作用。我無法將美元附加到字符串變量。
那麼,有沒有其他的方法來插入或附加美元符號到一個變量或字符串來實現所需的輸出。
任何意見或建議,非常感謝。
謝謝。
能否請您提供一個codepen(/的jsfiddle /什麼具備的,你)最低可重複情況? – Andrew
@Andrew這裏是sassmeister的要點http://www.sassmeister.com/gist/d907c9046779acf8510ef3103af86992 – ShellZero
在一天結束時,我的問題只是「如何將$'附加到變量」。如果我明白了,基本上解決了一切。 – ShellZero