根据你的包的结构/应用方式,你可以使用循环来生成一堆通用样式。在此处查看文档:http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#id35
你真的需要三个单独的组件来获取你的图片URL吗?设置$img
为/folder/img.ext
不会更容易吗?
另外,顺便说一下,你不需要用#{}
进行重复。
希望这就是你想要的……问题在实际需要什么结果方面不是非常具体。
祝好,
Jannis
更新:
好的,我看到你更新了你的问题(谢谢)。我认为这可能更适合一般使用:
@mixin background($imgpath,$position:0 0,$repeat: no-repeat) {
background: {
image: url($imgpath);
position: $position;
repeat: $repeat;
}
}
.testing {
@include background('/my/img/path.png');
}
然后将输出:
.testing {
background-image: url("/my/img/path.png");
background-position: 0 0;
background-repeat: no-repeat;
}
或者你可以使用简略版本:
@mixin backgroundShorthand($imgpath,$position:0 0,$repeat: no-repeat) {
background: transparent url(#{$imgpath}) $repeat $position;
}
.testing2 {
@include backgroundShorthand('/my/img/path.png');
}
这将生成:
.testing2 {
background: transparent url(/my/img/path.png) no-repeat 0 0;
}
最后,如果你想将图像目录的基本路径单独指定,可以执行以下操作:
$imagedir:'/static/images/'; // define the base path before the mixin
@mixin backgroundShorthandWithExternalVar($filename,$position:0 0,$repeat: no-repeat) {
background: transparent url(#{$imagedir}#{$filename}) $repeat $position;
}
.testing3 {
@include backgroundShorthandWithExternalVar('filename.png');
}
然后将生成:
.testing3 {
background: transparent url(/static/images/filename.png) no-repeat 0 0;
}
这符合你的需求吗?
如果不是,请随时更新问题或回复/评论。