23

我对 Sass 有点陌生,但我正在尝试为自己创建一个工作流程。我为我的主题设计生成“颜色包”,并且需要为我的 mixin 指定以下变量。有一个更好的方法吗?:

// folder,filename,extension,repeat,x-pos,y-pos
@mixin background ($folder:style1, $img:file, $type:png, $repeat:no-repeat, $x:0, $y:0) {
    background-image: url(./images/#{$folder}/#{$img}.#{$type});
    background-repeat: #{$repeat};
    background-position: #{$x}px #{$y}px;
}

我这样插入:

#nav {
  @include background(style2,myimage,png,repeat-x,10,10);
}

这产生了这个:

#nav {
  background-image: url(./images/style2/myimage.png);
  background-repeat: repeat-x;
  background-position: 10px 10px;
}

如果可能,我更喜欢使用 CSS 速记,但我遇到了输出错误。如果这不是最好的方法,我将不胜感激任何专家的建议。

4

3 回答 3

35

根据您的包的结构/应用方式,您可能可以使用循环来生成一堆通用样式。请参阅此处的文档: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;
}

这是你需要的吗?

如果不能随意更新问题或回复/评论。

于 2011-03-27T08:36:16.353 回答
4

其他一些样本:

图片路径:

$path--rel      : "../images";

颜色

$black: #000000;

创建混合:

@mixin background-image($img, $background-position, $background-color) {
    background-image: url('#{$path--rel}/#{$img}');
    background-repeat: no-repeat;
    background-position: $background-position;
    background-color: $background-color ;
} 

使用混合:

.navbar-inverse {
  @include background-image("header.png", right, $black); 
}

结果:

.navbar-inverse {
  background-image: url("../images/header.png");
  background-repeat: no-repeat;
  background-position: right;
  background-color: #000000;
}
于 2016-06-15T09:03:18.030 回答
2

拿一个 $var ,它会让你更容易。

$list: pranav shah

=author-images
  @each $author in $list
.photo-#{$author}
  background: image-url("avatars/#{$author}.png") no-repeat

   .author-bio
+author-images

css

.author-bio .photo-adam {
 background: url('/images/avatars/adam.png') no-repeat;
 }
.author-bio .photo-john {
 background: url('/images/avatars/john.png') no-repeat;
 }
.author-bio .photo-wynn {
 background: url('/images/avatars/wynn.png') no-repeat;
 }
.author-bio .photo-mason {
 background: url('/images/avatars/mason.png') no-repeat;
 }
.author-bio .photo-kuroir {
background: url('/images/avatars/kuroir.png') no-repeat;
}
于 2015-02-02T05:29:58.637 回答