This article example shares the specific code of Vue to achieve the product magnifying glass effect for your reference. The specific content is as follows 1. IntroductionIn this prosperous e-commerce era, with all kinds of live streaming or self-service shopping, we have a better understanding of the products and further checked the details. We found that our products can be magnified. So, we used the front-end technology Vue framework and wrote a function similar to a magnifying glass. 2. Implementation ideas For the display space of the original image (left), you can replace the img with canvas to protect the image row. 3. Effect display4. Specific implementation logic codetemplate (remember to change the image path) <template> <div> <div class="left"> <img class="leftImg" src="../../src/assets/curry.jpg" alt="" /> <!-- Mouse layer cover--> <div v-show="topShow" class="top" :style="topStyle"></div> <!-- The top layer covers the entire original image space with a transparent layer --> <div class="maskTop" @mouseenter="enterHandler" @mousemove="moveHandler" @mouseout="outHandler" ></div> </div> <div v-show="rShow" class="right"> <img :style="r_img" class="rightImg" src="../../src/assets/curry.jpg" alt="" /> </div> </div> </template> style css <style scoped> /* Enlarge the image and position the upper left corner to (0,0) */ .rightImg { display: inline-block; width: 800px; height: 800px; position: absolute; top: 0; left: 0; } /* Image zoom space on the right*/ .right { margin-left: 412px; width: 400px; height: 400px; border: 1px solid red; position: relative; overflow: hidden; } /* A top-level mask */ .maskTop { width: 400px; height: 400px; position: absolute; z-index: 1; top: 0; left: 0; } /* Layer mask, position the upper left corner to (0,0) */ .top { width: 200px; height: 200px; background-color: lightcoral; opacity: 0.4; position: absolute; top: 0; left: 0; } /* Display of original image */ .leftImg { width: 400px; height: 400px; display: inline-block; } /* Container of the original image */ .left { width: 400px; height: 400px; border: 1px solid teal; float: left; position: relative; } </style> script core js <script> export default { data() { return { topStyle: { transform: "" }, r_img: {}, topShow: false, rShow: false, }; }, methods: { // The mouse enters the original image space function enterHandler() { // Display of layer mask and magnified space this.topShow = true; this.rShow = true; }, // Mouse movement function moveHandler(event) { // Mouse coordinate position let x = event.offsetX; let y = event.offsetY; // The coordinate position of the upper left corner of the layer mask, and limit it: it cannot exceed the upper left corner of the original image area let topX = x - 100 < 0 ? 0 : x - 100; let topY = y - 100 < 0 ? 0 : y - 100; // Restrict the position of the layer mask again to ensure that the layer mask can only be within the original image area if (topX > 200) { topX = 200; } if (topY > 200) { topY = 200; } // Move control through transform this.topStyle.transform = `translate(${topX}px,${topY}px)`; this.r_img.transform = `translate(-${2 * topX}px,-${2 * topY}px)`; }, // Mouse out function outHandler() { //Control the hiding of layer cover and magnification space this.topShow = false; this.rShow = false; }, }, }; </script> V. Summary and ThoughtsI originally added three mouse events to the left of the original image container, but problems kept occurring. 1. I added a transparent maskTop covering the mouse area to make the magnifying glass fully realized. If I don’t add this maskTop layer, when my mouse enters the original image area, the mouse mask will not move with the mouse, and will vibrate at a high frequency when the mouse moves. The magnified area on the right will not move smoothly. 2. If the maskTop layer is not added, when I move the mouse into the original image area, the mousemove event is only executed once, which seems to be because the mouse layer mask blocks the 3. I have tried to dynamically determine the initial position of the mouse mask before and put it in the mouseenter event. As a result, the mouseenter event was executed abnormally many times, as if it had become a mousemove event. I have seen other magnifying glass cases, but none of them need to add the top-level overlay mask. I hope someone passing by can help me solve this problem. 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 the principle and implementation process of Nginx configuration https
>>: How to assign default values to fields when querying MySQL
1. IT Mill Toolkit IT Mill Toolkit is an open sou...
In the previous article - The charm of one line o...
1. golang:latest base image mkdir gotest touch ma...
Find the problem I recently migrated the storage ...
0x00 Introduction A few months ago, I found a vul...
Whether you're trying to salvage data from a ...
How to install Nginx in a specified location in C...
When displaying long data in HTML, you can cut off...
1. Download MySQL database and install and config...
This article example shares the specific code of ...
Effect picture: html: <div class='site_bar...
The HTTP request methods specified by the HTTP/1....
Table of contents 1. async 2. await: 3. Comprehen...
Often, after a web design is completed, the desig...
Introduction: When using MySQL to create a table,...