vue.jsHow can I use Vue.js to zoom in on an image when hovering over it?
Using Vue.js to zoom in on an image when hovering over it is a relatively simple task. The following example code will do the job:
<template>
<div>
<img
:style="{ transform: `scale(${scale})` }"
@mouseenter="scale = 2"
@mouseleave="scale = 1"
src="example.jpg"
/>
</div>
</template>
<script>
export default {
data() {
return {
scale: 1,
};
},
};
</script>
When the mouse hovers over the image, the scale variable is set to 2, which is then used in the transform style to zoom in on the image. When the mouse leaves the image, the scale variable is set back to 1.
Code explanation
template: The HTML markup used to display the image.@mouseenter: A Vue.js directive that is triggered when the mouse enters the image.@mouseleave: A Vue.js directive that is triggered when the mouse leaves the image.:style: A Vue.js directive used to apply styles to the image.transform: A CSS style used to scale the image.scale: A data variable used to set the zoom level of the image.
Helpful links
More of Vue.js
- How do I set a z-index in Vue.js?
- How do I download a zip file using Vue.js?
- How do I integrate Yandex Maps with Vue.js?
- How to use a YAML editor with Vue.js?
- How do I make an XHR request with Vue.js?
- How do I use the v-model in Vue.js?
- How can I create a transition effect in Vue.js?
- How can I get Vue.js to be detected by Firefox?
- How can I integrate a Java backend with Vue.js?
- How do I set up a GitHub repository for a Vue.js project?
See more codes...