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
Prerequisites To run containers on Windows Server...
The installation tutorial of MySQL 5.7.27 is reco...
When developing a website, you often need to use ...
uninstall First, confirm whether it has been inst...
The reason why Docker is so popular nowadays is m...
Learned ConcurrentHashMap but don’t know how to a...
Two ways to navigate the page Declarative navigat...
<br />Related article: Analysis of Facebook&...
Docker installation Use the official installation...
When the scale of Docker deployment becomes large...
A database index is a data structure whose purpos...
Recently, I need to frequently use iframe to draw ...
When we edit a layout, we usually use horizontal ...
Today I learned to install MySQL, and some proble...
The core is mysqldump and Runtime The operation i...