2

There are several images. I want to make image 1 pop up when I click on image 1, and I want image 2 to pop up when I click on image 2. Can I solve it using the index in class?

Carousel.vue

<template>
  <div v-for="(item, index) in items" :key="index">
    <img :src="item.thumbnail" />
    <button type="button" @click="imgClick()" :class="`img-index--${index}`">button-{{ index }}</button>
  </div>
  <Modal v-if="showModal" @close="showModal = false">
    <div slot="body" v-for="(item, index) in items" :key="index">
      <img :src="item.thumbnail" :class="`img-index--${index}`"/>
    </div>
  </Modal>
</template>

<script>
import Modal from './Modal.vue'
export default {
  props: {
    items: { type: Array, default: () => [] }
  },
  data() {
    return {
      showModal: false
    }
  },
  methods: {
    imgClick() {
      this.showModal = !this.showModal;
    }
  },
  components: {
    Modal: Modal
  }
}
</script>
ddon
  • 185
  • 3
  • 4
  • 15
  • Welcome to S.O.! I'm not sure what you want your code to do... It looks like you have an array of images. You are rendering them like this: `[img1][button1][img2][button2][img3][button3]`, etc. Then you have a modal. In the modal body, you are rendering: `[img1][img2][img3]` ... etc. Finally, you have a method that toggles the presence of the modal. This is kind of a confusing setup! Can you explain what it is supposed to do? – Vince Sep 19 '19 at 19:17
  • Here, I've created a JSFiddle for your question:https://jsfiddle.net/kpt8hyu1/ Use that as a starting point to describe what your desired behavior is. – Vince Sep 19 '19 at 19:36
  • @Vince Sorry for the lack of explanation. Thanks for the answer :) I would like to float an image next to modal button 0 in button 0 click. For example: [link](https://codepen.io/webcane/full/lHGJf) (I wanted the modal to float on image click, but it didn't work when I put an `imgClick()` event in the ``.) – ddon Sep 19 '19 at 20:01

1 Answers1

1

You can build a new data from your items prop and inject a new property show: false for each image.

You don't need 2 v-for loops this way. You can put the Modal component inside the first loop and just use the item.show to display or not the modal.

<template>
  <div>
    <div v-for="(item, index) in photos" :key="index">
      <div @click="imgClick(item)" style="cursor:pointer;>
        <img :src="item.thumbnail" />
      </div>
      <Modal v-if='item.show' @close="item.show = false">
        <div slot='body'>
          <img :src="item.thumbnail" :class="`img-index--${index}`"/>
        </div>        
      </Modal>
    </div>
  </div>
</template>
<script>
import Modal from './Modal.vue'
export default {
  props: {
    items: { type: Array, default: () => [] }
  },
  data() {
    return {
      photos: {}
    }
  },
  created() {
    this.photos = this.items.map(item => {
      return { ...item, show: false }
    })
  },
  methods: {
    imgClick(item) {
      item.show = true
    }
  },
  components: {
    Modal: Modal
  }
}
</script>

Fiddle example here


Note: you can wrap the thumbnail inside a div to manage the click image.

Sovalina
  • 5,410
  • 4
  • 22
  • 39