根据您的包的结构/应用方式,您可能可以使用循环来生成一堆通用样式。请参阅此处的文档:http: //sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#id35
您真的需要 3 个单独的组件来获取您的图片网址吗?不会:$img
然后将其设置为/folder/img.ext
容易得多?
另外,顺便说一下,您不需要#{}
for 重复。
我希望这就是您所追求的……就您实际需要的结果而言,这个问题并不是很具体。
干杯,詹尼斯
更新:
好的,我看到您已经更新了您的问题(谢谢)。我相信这对于一般用途可能会更好一些:
@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;
}
这是你需要的吗?
如果不能随意更新问题或回复/评论。