okan şahin
okan şahin

Reputation: 3

Vuejs add class dynamically

I am using vue-cli and this is my code. It is working perfectly but what i want to do is list them as array with v-for and not manually as below. I don't know how to match each item dynamically.

<template>
      <div class="slides">
        <div class="slide-1" :class="{active:selected == 1}">
          <figure class="photo">
            <img
          src="https://images.unsplash.com/photo-1518235506717-e1ed3306a89b?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80"
              alt
            >
          </figure>
        </div>
        <div class="slide-2" :class="{active:selected == 2}">
          <figure class="photo">
            <img
              src="https://images.unsplash.com/photo-1502602898657-3e91760cbb34?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1504&q=80"
              alt
            >
          </figure>
        </div>
        <div class="slide-3" :class="{active:selected == 3}">
          <figure class="photo">
            <img
              src="https://images.unsplash.com/photo-1474606030380-107829a22fc6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"
          alt
            >
          </figure>
        </div>
      </div>

      <nav>
        <h2><span @click="selected = 1" :class="{clicked:selected == 1}">new york</span></h2>
        <h2><span @click="selected = 2" :class="{clicked:selected == 2}">paris</span></h2>
        <h2><span @click="selected = 3" :class="{clicked:selected == 3}">london</span></h2>
      </nav>
</template>
<script>
export default {
  name: "app",
  data() {
    return {
      selected: 1
    }
  }
};
</script>

Upvotes: 0

Views: 1288

Answers (2)

dimas
dimas

Reputation: 389

Here is more into https://v2.vuejs.org/v2/guide/class-and-style.html

<div :class="{ active: isActive }"></div>

Class 'active' will be added if this.isActive == true

Upvotes: 0

Daniel Kemeny
Daniel Kemeny

Reputation: 690

You can do it as follows:

<template>
      <div class="slides">
        <div v-for="(item,index) in slides"
             :key="index"
             :class="[selected === item.id ? 'active' : '', 'slide-'+item.id]">
          <figure class="photo">
            <img :src="item.src" alt>
          </figure>
        </div>
      </div>

      <nav>
        <h2 v-for="(item, index) in slides" :key="index">
          <span @click="selected = item.id" :class="{clicked:selected === 1}">
            {{item.title}}
          </span>
        </h2>
      </nav>
</template>
<script>
export default {
  name: "app",
  data() {
    return {
      slides: [
        {
          id: 1,
          title: 'new york',
          src: 'https://images.unsplash.com/photo-1518235506717-e1ed3306a89b?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80',
        },
        {
          title: 'paris',
          id: 2,
          src: 'https://images.unsplash.com/photo-1502602898657-3e91760cbb34?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1504&q=80',
        },
        {
          title: 'london',
          id: 3,
          src: 'https://images.unsplash.com/photo-1474606030380-107829a22fc6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80',
        }
      ],
      selected: 1
    }
  }
};

The items what you are looping should be there as an object and then you can loop it trough and do the logic.

Upvotes: 2

Related Questions