There is a picture in a big box. When you put the mouse on it, a semi-transparent mask layer will appear. When you move the mouse, the mask layer moves with it. There is also an enlarged picture next to the box. The position of the enlarged picture changes with the position of the mask layer. When the mouse leaves the big box, the mask layer and the enlarged picture disappear. Implementation ideas 1. Edit the box, mask layer, and enlarged image page in html and css, and set the mask layer and enlarged image to be hidden by default Note: The offset of the enlarged image is given a negative value, which is opposite to the direction of movement of the mask layer. Code Sample<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Mobile phone details page</title> <!-- <link rel="stylesheet" href="css/detail.css" > <script src="js/detail.js"></script> --> <style> * { margin: 0; padding: 0; } .detail-content { width: 1200px; margin: 0 auto; } img { border: 0; vertical-align: middle; } .preview_img { position: relative; width: 400px; height: 400px; margin-top: 30px; border: 1px solid #ccc; } .preview_img img { width: 100%; height: 100%; } .mask { display: none; position: absolute; top: 0; left: 0; width: 300px; height: 300px; background-color: pink; opacity: .5; cursor: move; } .big { display: none; position: absolute; top: 0; left: 410px; width: 500px; height: 500px; border: 1px solid #ccc; background-color: pink; z-index: 999; overflow: hidden; } .bigimg { position: absolute; top: 0; left: 0; width: 800px; height: 800px; } </style> </head> <body> <div class="detail-content"> <div class="preview_img"> <img src="upload/s3.png" alt=""> <div class="mask"></div> <div class="big"> <img src="upload/big.jpg" alt="" class="bigImg"> </div> </div> </div> <script> var previewImg = document.querySelector('.preview_img'); var mask = document.querySelector('.mask'); var big = document.querySelector('.big'); var bigImg = document.querySelector('.bigImg'); // Mouse over box event previewImg.addEventListener('mouseover', function() { // Set the occlusion layer and magnified image display mask.style.display = 'block'; big.style.display = 'block'; }) // Mouse leaves the box event previewImg.addEventListener('mouseout', function() { // Set the occlusion layer and the enlarged image to hide mask.style.display = 'none'; big.style.display = 'none'; }) // Mouse moves in the box previewImg.addEventListener('mousemove', function(e) { // Get the position of the mouse in the box var x = e.pageX - this.offsetLeft; var y = e.pageY - this.offsetTop; // Calculate the movement value of the occlusion layer and put the mouse in the middle of the occlusion layer var maskX = x - mask.offsetWidth / 2; var maskY = y - mask.offsetHeight / 2; // The box is a square, so only the maximum horizontal X-axis movement value is calculated. The maximum Y-axis movement value is consistent with the X-axis var maskMax = previewImg.offsetWidth - mask.offsetWidth; // Limit the occlusion layer to move within the box if (maskX <= 0) { maskX = 0; } else if (maskX >= maskMax) { maskX = maskMax; } if (maskY <= 0) { maskY = 0; } else if (maskY >= maskMax) { maskY = maskMax; } mask.style.left = maskX + 'px'; mask.style.top = maskY + 'px'; // Calculate the maximum movement value of the enlarged image, the horizontal movement value of the large image = the movement value of the occlusion layer * the maximum movement distance of the large image / the maximum movement distance of the occlusion layer var bigMax = bigImg.offsetWidth - big.offsetWidth; var bigX = maskX * bigMax / maskMax; var bigY = maskY * bigMax / maskMax; bigImg.style.left = -bigX + 'px'; bigImg.style.top = -bigY + 'px'; }) </script> </body> </html> Page effect: 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:
|
<<: Analysis of MySql index usage strategy
>>: Docker container orchestration implementation process analysis
In MySQL, you may encounter the problem of case s...
This article example shares the specific code of ...
Table of contents Implementation ideas There are ...
Due to the company's business requirements, t...
Basic Concepts Absolute positioning: An element b...
This article shares the specific code of JS to im...
1. Promise description Promise is a standard buil...
Today we will introduce several ways to use CSS t...
In order to extend the disk life for storing audi...
background Some time ago, our project team was he...
Table of contents Web Components customElements O...
I believe that many friends who are new to web pag...
Body part: <button>Turn on/off light</bu...
This article shares the specific code of js to ac...
Table of contents 1. exists 1.1 Description 1.2 E...