ミックスインのすべての引数を適用/ループする機能があまりネイティブにないことは認識していませんが、これを克服する方法はたくさんあります。
必要なことを行うカスタム JavaScript 関数を less ブロックに追加できます。これは、カスタム関数の優れたリファレンスへのリンクです。
しかし、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_bg.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_bg.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;
}