K
K
kroha30002019-08-11 15:34:29
Vue.js
kroha3000, 2019-08-11 15:34:29

How to make multiple styles/classes change on click?

For example, there is a code that changes the style of the list output by clicking on a button (icon):

<div id="app">

<button @click="active1 = !active1, active2 = !active2, active3 = !active3">поменять стиль списка</button>

<ul>
  <li>
  <div :class="{ active1 }"></div>
  <div :class="{ active2 }"></div>
  <div :class="{ active3 }"></div>
  </li>
<ul>
</div>

new Vue({
  el: '#app',
  data() {
    return {
      active1: false,
      active2: false,
      active3: false
    }
  }
})

Can it be made more convenient, or what?
Is it possible to output @click values ​​to an array? - or a stupid question?
or is it easier and better for such a task to use, for example, v-if / v-show then for the whole <li></li>?
ps the list itself <li></li>- output from the array.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-11
@kroha3000

data: () => ({
  items: [ 'active1', 'active2', 'active3' ],
  active: false,
}),

<button @click="active = !active"></button>
...
<div v-for="n in items" :class="{ [n]: active }"></div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question