1

I have a list of strings as such in LESS: @flag-codes: "Albania" "al", "United States" "us", ...; I created a loop in LESS which I have achieved by using:

.flag-classes(@flags; @index: 1) when (@index <= length(@flag-codes)) {

    @flag: extract(@flags, @index);

    @name:  extract(@flag, 1);
    @code: e(extract(@flag, 2));


    // X Y

    .flag-@{code} {
        background-position: (24 * mod(@index, 8)) mod(@index, 16);
    }

    .flag-classes(@flags; (@index + 1));
}


.flag-classes(@flag-codes);

As you can see I am also trying to generate a background-position property which will have the values based on the loop index, but that only on the x axis.

The image used is a PNG sprite that has a collection of sprites organised in a matrix (8 columns and 16 rows).

I have managed to figure out how to set the x property which will always have @index % 8, but on the y axis it will have to start from 1 to 16 and keep the 1 for 8 times (as many columns as we have) and after each 8 increase with a unit.

Is something as described above possible to do, and if so how would I do it?

1 Answer 1

2

That's pretty simple:

.flag-@{code} {
    @columns: 8;
    @x: (24 * mod(@index, @columns));
    @y: floor((@index / @columns + 1));
    background-position: @x @y;
}
Sign up to request clarification or add additional context in comments.

8 Comments

is there a way to make the index start from 0 in the calculations but not in the loop?
Actually @index - 1 will do :)
Correction: @index - 1 will do for the x axis, but on the y axis only works up to the first row - 1 ...
I intentionally added 1 to y since you mentioned "y axis it will have to start from 1 to 16". If you actually need "0 to 15" instead then it simply would be @y: floor((@index / @columns));
that is what I tried, but the 8th, 16th, etc. (8nth) already get's the next index value, so the 8th for instance instead of still being 0 it is 1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.