A
A
Ankozar2021-08-26 09:57:26
Vue.js
Ankozar, 2021-08-26 09:57:26

How to use vue to change the background-color of every second element of the list?

I draw the list using v-for. The background of every second element should be a different color. I don't understand how to do it. After all, if you immediately bind the style, then each element will have such a background.

I thought to specify a key with a value of 0 in data, and for every second element to specify a background if this parameter is equal to 1, but how to change the parameter?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NewUser4242, 2021-08-26
@Ankozar

Display the elements through v-for="(item,i) in items" (i is the index, we will check the remainder of the division by 2, i.e. is the number a pair)
Then each check :class="i % 2 === 0 ? 'active' : '' "
And in the styles of the element with the active class (replace with the one you need) write the background)

I
imko, 2021-08-26
@imko

Keep it simple, do it with css, :nth-child(even)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question