This article example shares the specific code of Vue3 to achieve the image magnifying glass effect for your reference. The specific content is as follows Achieve results Code <template> <div class="goods-image"> <!-- Large image container --> <div class="large" :style="[ { backgroundImage: `url(${imageList[curId]})`, backgroundPositionX: position.backgroundPositionX, backgroundPositionY: position.backgroundPositionY, }, ]" v-if="isShow" ></div> <div class="middle" ref="target"> <img :src="imageList[curId]" alt="" /> <!-- Mask container --> <div class="layer" :style="{ left: left + 'px', top: top + 'px' }" v-if="isShow"></div> </div> <ul class="small"> <li v-for="(img, i) in imageList" :key="i" @mouseenter="curId = i" :class="{ active: curId === i }" > <img :src="img" alt="" /> </li> </ul> </div> </template> <script> import { reactive, ref, watch } from 'vue' import { useMouseInElement } from '@vueuse/core' export default { name: 'GoodsImage', props: { imageList: { type: Array, default: () => { return [] } } }, setup () { const curId = ref(0) const target = ref(null) // elementX: the offset value of the mouse from the left side // elementY: the offset value of the table from the top // isOutside: whether it is outside the container (true for outside and false for inside) const { elementX, elementY, isOutside } = useMouseInElement(target) const left = ref(0) // The distance from the slider to the left const top = ref(0) // The distance from the slider to the top const isShow = ref(false) // Show and hide the large image and the mask image const position = reactive({ // The position of the large image display, the default is 0 backgroundPositionX: 0, backgroundPositionY: 0 }) watch( //Monitoring object [elementX, elementY, isOutside], () => { if (elementX.value < 100) { // Minimum distance on the left left.value = 0 } if (elementX.value > 300) { left.value = 200 } if (elementX.value > 100 && elementX.value < 300) { left.value = elementX.value - 100 } if (elementY.value < 100) { // Minimum distance on the left top.value = 0 } if (elementY.value > 300) { top.value = 200 } if (elementY.value > 100 && elementY.value < 300) { top.value = elementY.value - 100 } // Control the movement of the background image // 1. The direction of the mask layer's movement is opposite to that of the background image // 2. Since the area ratio of the mask layer and the background image is 1:2, the background image moves two pixels for every pixel the mask layer moves // backgrondPosition: x, y; position.backgroundPositionX = -left.value * 2 + 'px' position.backgroundPositionY = -top.value * 2 + 'px' // When the value of isOutside changes, immediately negate it and assign it to isShow // isOutside: Is it outside the container? True for outside and false for inside isShow.value = !isOutside.value }, {} ) return { curId, target, left, top, position, isShow } } } </script> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of common operations of Docker images and containers
Table of contents 1. Introduction 2. Solution Imp...
For example, users who need screen reading softwar...
Apple Mug Icons and Extras HD StorageBox – add on...
Preface: MySQL master-slave architecture should b...
Table of contents MySQL delete syntax alias probl...
MyISAM storage engine MyISAM is based on the ISAM...
Table of contents Preface 1. Basic usage of liste...
1. Install xshell6 2. Create a server connection ...
Problem description: structure: test has two fiel...
This article shares the specific steps of VMware ...
React Native is a cross-platform mobile applicati...
Install Ubuntu 20.04 Install NVIDIA drivers Confi...
background This bug was caused by滾動條占據空間. I check...
As we all know, without the cd command, we cannot...
This should be something that many people have do...