This article shares the specific code of js to achieve the magnifying glass effect of shopping website products for your reference. The specific content is as follows First, let me explain the principle, taking the magnifying glass effect of a certain product on Tmall as an example: The so-called magnifying glass effect is actually an effect that deceives our eyes. Here we can see that the row of small pictures below the picture are actually the same as the pictures in the cover layer and the pictures in the magnified layer, but the resolution is different. Therefore, to achieve the magnifying glass effect, we need several groups of pictures with different resolutions but the same content: Obviously, the small picture is the small picture with "small" in the picture name, the cover layer is the normal-sized picture, and the enlarged layer is the enlarged picture with "big". Then, by adding appropriate displacement and display effects, we can achieve the magnifying glass effect we see. Let's implement it through code: First write out the HTML structure: <div id="box"> <div class="show"> <img src="./images/1.jpg" alt="#"> <div class="drag"></div> </div> <div class="magnify"></div> <ul> <li class="active"><img src="./images/1.small.jpg" alt="#"></li> <li><img src="./images/2.small.jpg" alt="#"></li> </ul> </div> <script type="text/javascript" src="./javascript/mgfyGlass.js"></script> <script> const oBox = document.querySelector('#box'); const imgArr = [ {small: '1.small.jpg', normal: '1.jpg', big: '1.big.jpg'}, {small: '2.small.jpg', normal: '2.jpg', big: '2.big.jpg'} ]; </script> Then add the css style: body,div,ul,li{ margin: 0; padding: 0; list-style: none; font-size: 0; } img{ display: block; } #box{ width: 650px; position: relative; margin: 0 auto 0 240px; } #box .show{ width: 600px; border: solid 2px hotpink; position: relative; } #box .show img{ width: 100%; } #box .show .drag{ position: absolute; width: 200px; height: 200px; background-color: #e0a8d7; opacity: .4; left: 0; top: 0; display: none; } #box .magnify{ width: 800px; height: 800px; border: solid 2px #7d777b; position: absolute; left: 100%; top: 0; background: url("../images/1.big.jpg") no-repeat 0 0 / 2400px; display: none; } #box ul{ width: 100%; height: 150px; margin-top: 20px; } #box ul::after{ content: ''; display: block; clear: both; } #box ul li{ height: 100%; float: left; margin: 0 8px; border: solid 2px #fff; } #box ul li.active{ border-color: hotpink; } #box ul li img{ height: 100%; } Here are some things to note when setting the CSS style:
Here you need to use js to achieve several effects: 1. When the mouse moves into the normal picture box, the cover layer and the magnified layer box are displayed. When the mouse moves out of the normal picture box, the cover layer and the magnified layer box are hidden. Then the code: class MgnGlass { constructor(ele, array) { this.ele = ele; this.array = array; this.show = ele.querySelector('.show'); this.showImg = this.show.querySelector('img'); this.drag = ele.querySelector('.drag'); this.magnify = ele.querySelector('.magnify'); this.oUl = ele.querySelector('ul'); this.oUlis = ele.querySelectorAll('ul li'); } // Define a method to call all methods defined later // Entry function init() { this.setMouse(); this.setPosition(); this.setTab(); } //Mouse movement in and out setMouse() { //Mouse over, show the image area, and display the cover layer and magnifying glass this.show.addEventListener('mouseover', () => { this.drag.style.display = 'block'; this.magnify.style.display = 'block'; }); //Mouse out, show the image area, hide the cover layer and magnifying glass this.show.addEventListener('mouseout', () => { this.drag.style.display = 'none'; this.magnify.style.display = 'none'; }); } // Set the positioning effect // When the mouse moves in the image area // 1. Let the cover layer follow the mouse movement---similar to the previous mouse drag effect setPosition() { this.show.addEventListener('mousemove', (e) => { e = e || event; // 1. Position the cover layer // Calculate the coordinate position of the upper left corner of the cover layer by the mouse position let x = e.clientX - this.ele.offsetLeft - this.ele.clientLeft - this.drag.clientWidth / 2; let y = e.clientY - this.ele.offsetTop - this.ele.clientTop - this.drag.clientHeight / 2; // 2, set the boundary value // minimum is 0 maximum is parent div width and height - cover layer width and height if (x < 0) { x = 0; } else if (x > (this.show.clientWidth - this.drag.clientWidth)){ x = this.show.clientWidth - this.drag.clientWidth; } if (y < 0){ y = 0; } else if (y > (this.show.clientHeight - this.drag.clientHeight)){ y = this.show.clientHeight - this.drag.clientHeight; } // 3, position the value to the cover layer this.drag.style.left = x + 'px'; this.drag.style.top = y + 'px'; // 4. The background image of the magnifying glass on the right needs to move together // Add positioning to the background image // The image on the left is stationary, and the cover layer moves Cover layer moves 100 100 // On the right is the magnifying glass, which is not moving, and the background image is moving. Background image is moving -100 -100 // When moving, the positioning must be set according to the proportion // Background image positioning = background image size * cover layer positioning / image size // Calculate the background image positioning value by the proportion of the cover layer movement let backX = 2400 * x / 600; let backY = 2400 * y / 600; // Position the background image // Position the background image this.magnify.style.backgroundPosition = `-${backX}px -${backY}px`; }) } // Switching effect // 1. Add style to the label that the mouse is currently over // Remove styles from all labels and add style to the label that is currently clicked/passed over setTab() { this.oUlis.forEach((item, key) => { item.addEventListener('mouseover', () => { // 1, clear the style of all li tags this.oUlis.forEach((item2) => { item2.className = ''; }); // 2, add style to the current tag item.className = 'active'; // 3, set the image // The index subscript key of the current label is the index subscript of the image to be displayed in the corresponding image array // 1, set the path for the image label // Get the corresponding image name through array, index, and image attributes // label.src = assignment or label.setAttribute('src', attribute value) this.showImg.setAttribute('src', `./images/${this.array[key].normal}`); // 2. Set the path of the background image for the magnifying glass area. // All settings about the background image must be rewritten this.magnify.style.background = `url('./images/${this.array[key].big}') no-repeat 0 0 / 2400px`; }) }) } } To perfectly achieve the magnifying glass effect, you must pay attention to 2 proportions: 1. CSS style ratio: Image area size: Cover layer size = Background image size: Magnifying glass area size Then call our constructor to get the final HTML, and execute it to achieve our magnifying glass effect: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>Magnifying Glass</title> <link rel="stylesheet" type="text/css" href="./css/mgfyGlass.css" > </head> <body> <div id="box"> <div class="show"> <img src="./images/1.jpg" alt="#"> <div class="drag"></div> </div> <div class="magnify"></div> <ul> <li class="active"><img src="./images/1.small.jpg" alt="#"></li> <li><img src="./images/2.small.jpg" alt="#"></li> </ul> </div> <script type="text/javascript" src="./javascript/mgfyGlass.js"></script> <script> const oBox = document.querySelector('#box'); const imgArr = [ {small: '1.small.jpg', normal: '1.jpg', big: '1.big.jpg'}, {small: '2.small.jpg', normal: '2.jpg', big: '2.big.jpg'} ]; const mgnGlass = new MgnGlass(oBox, imgArr); mgnGlass.init(); </script> </body> </html> 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:
|
<<: Specific use of MySQL binlog_ignore_db parameter
>>: Docker starts the elasticsearch image and solves the error after mounting the directory
Solution to the problem that there is no unzip co...
Situation description: The database was started a...
Following are the quick commands to clear disk sp...
Location means "positioning", which is ...
1. Source of the problem A friend @水米田 asked me a...
Overview Let's summarize some SQL statements ...
Disable SeLinux setenforce 0 Permanently closed: ...
Table of contents Preface: 1. Create a project wi...
1. Always close HTML tags In the source code of p...
1. What is Vue Vue is a progressive framework for...
Table of contents 1. Change the 2375 port of Dock...
Defining an array in Bash There are two ways to c...
Initialize Dockerfile Assuming our project is nam...
The LIKE operator is used in the WHERE clause to ...
Often, we may need to export local database data ...