This article shares the specific code of vue3 encapsulation similar to the magnifying glass effect component of Jingdong product details page for your reference. The specific content is as follows First, complete the basic layout Complete the image switching effect and switch images through the mouseenter event Landing code <template> <div class="goods-image"> <!-- Preview large image --> <div class="large" v-show="show" :style="[{ backgroundImage: `url(${images[currIndex]})` }, bgPosition]"></div> <!-- Product image--> <div class="middle"> <!-- Product image--> <img ref="target" :src="images[currIndex]" alt="" /> <!-- The mouse enters the mask of the image--> <div class="layer" v-show="show" :style="[position]"></div> </div> <!-- Thumbnail --> <ul class="small"> <li v-for="(img, i) in images" :key="img" :class="{ active: i === currIndex }"> <!-- Move the mouse to the small picture next to the large picture of the product and the large picture will be displayed--> <img @mouseenter="currIndex = i" :src="img" alt="" /> </li> </ul> </div> </template> <script> import { reactive, ref, watch } from 'vue' import { useMouseInElement } from '@vueuse/core' export default { name: 'GoodsImage', props: { images: type: Array, default: () => [] } }, setup(props) { // Control the display and hiding of the preview image and mask layer const show = ref(false) // Control which image is displayed in the product image const currIndex = ref(0) // ref gets the DOM element const target = ref(null) // Record the coordinate position of the masked semi-transparent image in the product image const position = reactive({ top: 0, left: 0 }) // Record the coordinate position of the area covered by the mask layer in the preview image const bgPosition = reactive({ backgroundPositionX: 0, backgroundPositionY: 0 }) // The method useMouseInElement provided by the third-party vueuse gets the coordinates of the mouse in a certain area const { elementX, elementY, isOutside } = useMouseInElement(target) // The listener monitors the coordinates of the mouse when it enters the product image to manipulate the mask layer and the preview image effect watch([elementX, elementY, isOutside], () => { // When isisOutside.value is true, it means the mouse has not entered the target element. When it is false, it means the mouse has entered the target element. // When it is true, the coordinates are not recorded to avoid performance loss if (isOutside.value) { // The mask layer and preview image will not be displayed if the mouse does not enter the target element show.value = false return } // When the mouse enters the target element, the mask layer and the preview image are displayed.show.value = true // Determine the boundary value based on the size of the mask layer and the size of the product image // The boundary value of left (left, right) if (elementX.value < 100) { position.left = 0 } else if (elementX.value > 300) { position.left = 200 } else { position.left = elementX.value - 100 } // Boundary values of top (upper and lower) if (elementY.value < 100) { position.top = 0 } else if (elementY.value > 300) { position.top = 200 } else { position.top = elementY.value - 100 } // The coordinates of the part of the product image covered by the mask layer in the preview image, plus the unit bgPosition.backgroundPositionY = -position.top * 2 + 'px' bgPosition.backgroundPositionX = -position.left * 2 + 'px' // The mask layer is relative to the coordinates of the upper left corner of the product image, plus the unit position.top += 'px' position.left += 'px' }) // Return to the template using return { currIndex, show, target, position, bgPosition } } } </script> <style scoped lang="less"> .goods-image { width: 480px; height: 400px; position: relative; display: flex; z-index: 500; .large { position: absolute; top: 0; left: 412px; width: 400px; height: 400px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); background-repeat: no-repeat; background-size: 800px 800px; background-color: #f8f8f8; } .middle { width: 400px; height: 400px; position: relative; cursor: move; .layer { width: 200px; height: 200px; background: rgba(0, 0, 0, 0.2); left: 0; top: 0; position: absolute; } } .small { width: 80px; li { width: 68px; height: 68px; margin-left: 12px; margin-bottom: 15px; cursor: pointer; &:hover, &.active { border: 2px solid @xtxColor; } } } } </style> Final result 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:
|
<<: Example of implementing load balancing with Nginx+SpringBoot
>>: Mysql delete data and data table method example
This article shares the specific code of jQuery t...
1. What is Docker Swarm? Docker Swarm is a cluste...
How do I download MySQL from the official website...
About JS, CSS CSS: Stylesheet at the top Avoid CS...
Table of contents Introduction to NFS Service Wha...
CSS matches multiple classes The following HTML t...
First, your container must be running You can vie...
This article example shares the specific code of ...
Technical Background This application uses the vu...
Mind Map He probably looks like this: Most of the...
1. Regular expression matching ~ for case-sensiti...
Today, when I was installing CentOS6.2, I couldn&...
We often encounter this problem: how to use CSS t...
a href="#"> After clicking the link, ...
Preface In daily work, we sometimes run slow quer...