Unable to execute a Less CSS loop properly -
i'm trying create different loops (the class should have different background colours), i'm able compile first one.
here's example: http://codepen.io/anon/pen/mywgdo?editors=1100
and code i'm using:
@temp0-9: #1976d2; @temp10-20: #00bcd4; @gap1: 10; @gap2: 10; .first (@i) when (@i > 0) { span.temp-@{i} { display:block; background: @temp0-9; } .first(@i - 1); } .first(@gap1); .second (@i) when (@i > 15) { span.temp-@{i} { display:block; background: @temp10-20; } .second(@i - 1); } .second(@gap2);
the compiled result following:
span.temp-9 { display: block; background: #1976d2; } span.temp-8 { display: block; background: #1976d2; } span.temp-7 { display: block; background: #1976d2; } span.temp-6{ display: block; background: #1976d2; } span.temp-5{ display: block; background: #1976d2; } span.temp-4{ display: block; background: #1976d2; } span.temp-3{ display: block; background: #1976d2; } span.temp-2{ display: block; background: #1976d2; } span.temp-1{ display: block; background: #1976d2; } span.temp-0{ display: block; background: #1976d2; }
only 10 entries instead of 20 expecting.
any help?
you've got loop's guard conditions wrong. guard condition states loop executed when input (@i
) greater 15 value passed input (@gap2
) 10 , hence loop never gets executed.
for output expecting, change guard condition in below snippet. now, guard @i > 0
, loop executed selector interpolation in 2nd mixin uses @j
variable (which @i + @gap1
). since adding @gap1
loop's index, no. value appended selector greater 10 second loop.
@temp0-9: #1976d2; @temp10-20: #00bcd4; @gap1: 10; @gap2: 10; .first (@i) when (@i > 0) { span.temp-@{i} { display:block; background: @temp0-9; } .first(@i - 1); } .first(@gap1); .second (@i) when (@i > 0) { @j: @i + @gap1; span.temp-@{j} { display:block; background: @temp10-20; } .second(@i - 1); } .second(@gap2);
if have multiple such gaps, writing single loop (with complex logic) better writing multiple loop mixins. below sample:
@gaps: 46, 19, 3, 4, 4, 14; /* gap array */ @temps: red, crimson, orange, gold, yellow, green; /* temps corresponding each gap */ .gaps-loop(@i, @prevgap) when (@i > 0){ @gap: extract(@gaps, @i); /* extract each gap 1 one based on loop index */ @temp: extract(@temps, @i); /* extract temp corresponding each gap */ .span-gen-loop(@j) when (@j < @gap){ /* loop generate spans - executed many times @gap */ @k: @j + @prevgap; /* add current index previous gaps - generates running number 0-90 */ span.temp-@{k}{ display:block; background: @temp; } .span-gen-loop(@j + 1); } .span-gen-loop(0); .gaps-loop(@i - 1, @prevgap + @gap); /* send current gap + previous gap(s) */ } .gaps-loop(length(@gaps), 0); /* loop many times there gaps */
Comments
Post a Comment