【问题标题】:How to mix multiple background with LESS如何将多个背景与 LESS 混合
【发布时间】:2013-03-14 15:08:04
【问题描述】:

我有一个小问题... 是否有任何选项可以将多个背景与 LESS 混合?

我在 LESS 中有这个背景设置:

.background_centered(
  @url,
  @position_horizontal: center,
  @position_vertical: top,
  @background-repeat: no-repeat,
  @transparency: transparent) {
    background: @arguments;
}

现在:我需要用多个背景写入输出样式,所以我这样做:

.class {
   .background_centered(url('../img/war_top_baner_gp.png'),url('../img/war_header_bg.png'));
}

在最终输出中有些东西是不正确的,我有这个:

background: url('/../img/war_top_baner_gp.png') 
            url('/../img/war_header_bg.png') top no-repeat transparent;

怎么了?是否可以这样做?

【问题讨论】:

  • 您实际上是在发送url('/../img/war_header_bg.png') 作为@position_horizontal 的值。作为 Sass 用户,我通常会添加括号以消除歧义:@include background_centered((url('../img/war_top_baner_gp.png'),url('../img/war_header_bg.png')));。我不确定 LESS 的解决方法是什么。
  • 我尝试了,我有语法错误:(
  • 看我的回答,它使用简单的 LESS 语法来解决这个问题

标签: css less


【解决方案1】:

我不知道原生地具有应用/循环通过 mixin 的所有参数的功能,但是有很多选项可以解决这个问题。

您可以将自定义 javascript 函数添加到执行您想要的操作的 less 块中。 Here is a link to a nice reference for custom functions.

但你也可以在 less 中构建一个小循环:

    // for loop
    .for(@l,@obg,@i:1) when (@l > @i) {
      @nbg: `@{url}[@{i}]`;
      @bg: ~"@{obg}, @{nbg} @{rest}";
      .for(@l, @bg, @i + 1);
    }

    // multiple background urls + additional bg properties
    .bgmixin(@url, @rest){
      @num: unit(`@{url}.length`);
      @bg: ~`@{url}[0]` @rest;
      .for(@num, @bg);
      background: ~"@{bg}";
    }

    // defining bg urls
    @url: 'url("../img/war_top_baner_gp.png")', 'url("../img/war_header_b‌g.png")';

    // including the bgmixin in .class
    .class{
      .bgmixin(@url, center top no-repeat transparent);
    }

输出是

    .class {
          background: url("../img/war_top_baner_gp.png") center top no-repeat transparent,
                      url("../img/war_header_b‌g.png") center top no-repeat transparent;
    }

如果我理解正确,这就是你想要的。


编辑:我只是想在这里补充一下,我的想法是找到一个更通用的解决方案,它实际上是循环/递归数组元素,这使得使用不同的属性与它们各自的属性变得容易图像 - 所以你为函数提供一个 url 数组和一个其他属性数组。在这里,我将尝试说明这个想法:

    .for(@l,@obg,@i:1) when (@l > @i) {
      @nbg: `@{url}[@{i}]`; @nattr: `@{attr}[@{i}]`;;
      @bg: "@{obg}, @{nbg} @{nattr}";
      .for(@l, @bg, @i + 1);
    }

    .bgmixin(@url, @attr){
      @num: unit(`@{url}.length`);
      @bg: ~`@{url}[0]` ~`@{attr}[0]`;
      .for(@num, @bg);
      background: ~"@{bg}";
    }

    @urls: "url('../img/centered_image_bg.png')", "url('../img/left_image_bg.png')";
    @attr: "center top no-repeat transparent", "left top y-repeat";

    .class{
      .bgmixin(@urls, @attr);
    }

输出将如下所示:

    .class {
        background: url('../img/centered_image_bg.png') center top no-repeat transparent,
                    url('../img/left_image_bg.png') left top y-repeat;
    }

【讨论】:

  • 谢谢帮助,但我认为@ScottS 的解决方案更简单
  • 很好,您的解决方案确实允许为每个属性设置不同的属性。虽然这可能不是 OP 对他的名为 background-centered 的 mixin 的需要,但它是为其他人获得通用解决方案的宝贵贡献。 +1。
【解决方案2】:

在 LESS 中将多个属性值传递给单个属性是一项挑战。您当前的代码显然适用于单一背景。要获得多个,您通常必须使用字符串。

以下允许通过将多个 url 作为单个字符串传递给第一个参数来输入多个 url,然后使用内联 javascript 对字符串进行替换并将这些 url 连接到其他参数。

.background_centered(
  @urls,   
  @position_horizontal: center,
  @position_vertical: top,
  @background-repeat: no-repeat,
  @transparency: transparent ) {

  @combinedValues: ~"@{position_horizontal} @{position_vertical} @{background-repeat} @{transparency}";
  @urlsRewrite: ~`@{urls}.replace(/\)/g, ') @{combinedValues}')`;
  background: @urlsRewrite;
}

.class {
   .background_centered("url('../img/war_top_baner_gp.png'), url('../img/war_header_bg.png')");
}

输出

.class {
  background: url('../img/war_top_baner_gp.png') center top no-repeat transparent, url('../img/war_header_bg.png') center top no-repeat transparent;
}

【讨论】:

  • smooth ... 将这个东西应用到一个我什至没有想过放入单个字符串的数组中,我有点太兴奋了,呵呵 =)
  • 好的,我想这比上面的解决方案更灵活、更简单,所以它工作得很好,非常感谢,这很苛刻,BR
  • @djlukas777:是的,对于您给出的情况,我的回答更简单。如果您需要对背景图像应用不同设置的能力,Martin 更通用、更灵活。
【解决方案3】:

要将逗号分隔的参数列表传递给 mixin,只需在 mixin 调用中使用 ; 而不是逗号:

 .mixin(@bg, @color) {
   background: @bg;
   color: @color;
 }

 .class {
   .mixin(url('img/bg.png') no-repeat, red; white);
 }

输出:

.class {
  background: url('img/bg.png') no-repeat, #ff0000;
  color: #ffffff;
}

【讨论】:

    【解决方案4】:

    您不需要在 mixin 中粘合多个背景。可以使用LESS语言的原生merge功能。

    merge 功能允许聚合来自多个 属性到一个逗号或空格分隔的列表下 财产。 merge 对于 background 和 变换。

    因此,您的 less 代码可能如下所示:

    .element {
      background+: url("../1.png") center top no-repeat transparent;
      background+: url("../2.png") left top repeat;
      background+: url("../3.png") right top repeat;
    }
    

    这比复杂的 mixins 简单得多。

    生成的css:

    .element {
      background: url("../1.png") center top no-repeat transparent,
                  url("../2.png") left top repeat, 
                  url("../3.png") right top repeat;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      相关资源
      最近更新 更多