Since you included the tag css then my suggestion is that you take advantage of this by using the selector :nth-child(...)
, similar to what Sveen fez.
However I think you need an explanation, I made an example in a simpler way (from my point of view) to be able to modify
var collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var html = '<div>' + collection.join('</div><div>') + '</div>';
document.getElementById('data').innerHTML = html;
#data > div:nth-child(5n),
#data > div:nth-child(5n-1),
#data > div:nth-child(5n-2),
#data > div:nth-child(5n-3),
#data > div:nth-child(5n-4) {
color: red;
}
#data > div:nth-child(10n),
#data > div:nth-child(10n-1),
#data > div:nth-child(10n-2),
#data > div:nth-child(10n-3),
#data > div:nth-child(10n-4) {
color: blue;
}
<div id="data"></div>
Explanation about the use of Nth-Child
First I must say that in this use you will not even need to iterate, note that it is not necessary for
, the join
has already resolved:
var html = '<div>' + collection.join('</div><div>') + '</div>';
Now about CSS, this first:
#data > div:nth-child(5n),
#data > div:nth-child(5n-1),
#data > div:nth-child(5n-2),
#data > div:nth-child(5n-3),
#data > div:nth-child(5n-4)
Note that:
5n
will apply the effect to every 5 elements
5n-1
will apply the effect a on the previous element (-1) to the fifth element of each cycle
5n-2
will apply the effect a on the previous element in 2 positions (-2) to the fifth element of each cycle
5n-3
will apply the effect a on the previous element in 3 positions (-3) to the fifth element of each cycle
5n-4
will apply the effect a on the previous element in 4 positions (-4) to the fifth element of each cycle
In this way it will apply to each 5 elements and to each 4 elements prior to cda one of these
Now this:
#data > div:nth-child(10n),
#data > div:nth-child(10n-1),
#data > div:nth-child(10n-2),
#data > div:nth-child(10n-3),
#data > div:nth-child(10n-4) {
color: blue;
}
This is quite similar, however the count starts from the 10, ie 10 in 10 elements, and then has been applying the style to the 4 elements prior to each of the tenth elements.
Ivan can do it with just a line of CSS... If you want an answer for yourself just so you know.
– hugocsl
@hugocsl a line unfortunately I believe not, what I got was at most with Nth-Child and applying negative values to apply style to the 4 previous elements https://pt.stackoverflowcom/a/299340/3635 - but if you know of any implementation please be sure to reply, can guarantee some :D points
– Guilherme Nascimento
@Guilhermenascimento uses negative value yes, but that’s the way, you can test there :)
ul li:nth-child(n+6):nth-child(-n+10) { color:red; }
You will get red color only from item 6 to 10.– hugocsl
@hugocsl this does not work, it will only affect 5 elements, those who come after the first 5.
– Guilherme Nascimento
@Guilhermenascimento Yes rss, takes the range of 5 elements. I had understood that this is what he wanted the top 5 and the last 5 picks up the pattern style that he set, and in the 6 to 10 the Nth-Child style... I wish I had a Even and Odd with a range, like
Odd-n5
– hugocsl