Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an image src into a modal dialog in Vue (in order to zoom the clicked image)?

What I'm trying to do is to zoom an image on click, the idea is:

  • you click on an image
  • dialog of width=85vw opens up with the image you just clicked on inside of it (so the image is displayed almost fullscreen now)

I cannot think of a better way of "zooming" an image on click, but to open it in a modal dialog (if there's an easier way, please let me know).

Code:

 <v-dialog v-model="dialog" max-width="85vw" >
     <img :src="img1" alt="" width="100%" @click.stop="dialog=false">
 </v-dialog>
                
 <img :src="img1" width="500px"  @click.stop="dialog = true">
 <img :src="img2" width="500px"  @click.stop="dialog = true">
 <img :src="img3" width="500px"  @click.stop="dialog = true">
 export default {
     data() {
         img1: "../../src/assets/pexels-photo-373912.jpg",
         img2: "../../src/assets/pexels-photo-373912.jpg",
         img3: "../../src/assets/pexels-photo-373912.jpg"
     }
 }

The problem is, it's not opening any clicked image in a dialog, just the one you hard coded in there, in this example it will always open img1 no matter what image you click.

I don't know how to pass the :src into the dialog dynamically - the :src of the image you clicked.

P.S. v-dialog is a component from Vuetify.js library


Question:

  • Is there an obviously better way of doing it?
  • If not really, how do I make this method to work and display the image I clicked in the modal dialog?
like image 572
Un1 Avatar asked Jan 02 '18 23:01

Un1


People also ask

How can I use img src in VUE JS?

To display an image with the img tag in vue, you can use v-bind:src directive, or :src . Or :src for short. Remember that :src expects a JavaScript expression, so if you want to use a string literal in :src you need to wrap the string in quotes.

How do I make modal close on click outside Vue?

Close Dialog while Click on Outside of Dialog in Vue Dialog component. By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method.

How do I import an image into Vue component?

To import and use image in a Vue. js single file component, we can set the src prop of the image to the image path returned by the require function. to set the src prop of the image to the path returned by require . We call require with the relative path of the image to return the resolved path of the image.


2 Answers

You need a variable to hold which image is selected. When you click on an image, it should set the variable to the url for that image. When you click on the dialog image, it should unset the variable.

The dialog should show when the variable is set and otherwise be hidden.

For simplicity, I'm not using an actual dialog, just a div. It looks like you would use it for the dialog's v-model rather than using the v-if I use.

new Vue({
  el: '#app',
  data: {
    selectedImage: null,
    images: [
      'http://via.placeholder.com/400x300?text=image%201',
      'http://via.placeholder.com/600x400?text=image%202',
      'http://via.placeholder.com/500x500?text=image%203'
    ]
  },
  methods: {
    zoom(url) {
      console.log("Zoom", url);
      this.selectedImage = url;
    }
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app">
  <div v-if="selectedImage" max-width="85vw">
    <img :src="selectedImage" alt="" width="100%" @click.stop="selectedImage = null">
    <hr>
  </div>

  <div v-for="url in images">
    <img :src="url" width="100px" @click="zoom(url)">
  </div>
</div>
like image 81
Roy J Avatar answered Oct 11 '22 00:10

Roy J


to click distributed images across the page define a property and change it with click event

in template

 <v-dialog v-model="dialog" max-width="60%" @keydown.esc="cancel">
      <v-card>
        <v-img :src="pic" alt="" contain/>
      </v-card>
    </v-dialog>
  <v-img
      src="require(@/assets/clinic/1.jpeg)"
      alt=""
      contain
      @click="openPic(require('@/assets/clinic/1.jpeg'))"//this part is important you have to require image
   />

in script

data() {
    return {
      pic: "",
      dialog: false
    }
  },
  methods: {
    openPic(image) {
      this.dialog = true
      this.pic = image
    },
    cancel() {
      this.dialog = false
    }
  }
like image 24
drascom Avatar answered Oct 10 '22 23:10

drascom