9951 explained code solutions for 126 technologies


vue.jsHow can I use Vue.js to implement a zoomable image?


Vue.js can be used to implement a zoomable image by utilizing the v-zoom directive. This directive allows you to bind a zoom value to an element, and it will automatically scale the element according to the zoom value.

For example, the following code will create an image that can be zoomed in and out using the mouse wheel:

<div>
  <img v-zoom="zoom" src="my-image.jpg" />
</div>
new Vue({
  el: '#app',
  data: {
    zoom: 1
  }
})

The v-zoom directive binds the zoom value in the Vue instance to the image element. When the zoom value changes, the image will be scaled accordingly.

The following code will bind the zoom value to the mouse wheel event, so that the image can be zoomed in and out using the mouse wheel:

new Vue({
  el: '#app',
  data: {
    zoom: 1
  },
  methods: {
    handleWheel: function(e) {
      this.zoom += e.deltaY * -0.01
    }
  }
})
<div @wheel="handleWheel">
  <img v-zoom="zoom" src="my-image.jpg" />
</div>

The v-zoom directive can be used with other elements, such as divs or spans, to create zoomable elements that are not images.

Helpful links

Edit this code on GitHub