This article shares the specific code of JavaScript to implement a product magnifying glass for your reference. The specific content is as follows HTML+CSS part: <style> .small{ position: relative; width: 400px; height: 450px; border: 1px solid #ccc; } .small img{ width: 100%; height: 100%; } .small .mask{ position: absolute; left: 0; top: 0; width: 300px; height: 300px; background-color: rgba(0, 255, 0, .2); } .big{ position: absolute; left: 450px; top: 8px; width: 550px; height: 550px; border: 1px solid #ccc; overflow: hidden; display: none; } .bigimg{ position: absolute; left: 0; top: 0; } </style> <body> <div class="small"> <img src="./img/small.jpg"> <div class="mask"></div> </div> <div class="big"> <img src="./img/big.jpg"> </div> </body> JS part: <script> var small = document.querySelector('.small'); var mask = document.querySelector('.mask'); var big = document.querySelector('.big'); var bigImg=document.querySelector('.big>img'); // Get the width and height of the big image var bigWidth=bigImg.offsetWidth; var bigHeight=bigImg.offsetHeight; // function move(e){ var x = e.pageX - this.offsetLeft; var y = e.pageY - this.offsetTop; console.log(x,y); // Move the mask position var maskHeight = mask.offsetHeight/2; var maskWidth = mask.offsetWidth/2; mask.style.left = x - maskWidth+'px'; mask.style.top = y - maskHeight + 'px'; // Limit the movement range of the mask // console.log('mask.offsetTop',mask.offsetTop); // console.log('mask.offsetLeft',mask.offsetLeft); var maxLeft=small.offsetWidth - mask.offsetWidth; if(mask.offsetTop<0){ mask.style.top=0; } if(mask.offsetLeft > small.offsetWidth - mask.offsetWidth){ mask.style.left =maxLeft+'px'; } if(mask.offsetLeft<0){ mask.style.left = 0; } if(mask.offsetTop > small.offsetHeight - mask.offsetHeight){ mask.style.top = small.offsetHeight - mask.offsetHeight + 'px'; } // bigImg big image big box big image movement // (bigImg.offsetWidth - big.offsetWidth) / (samll.offsetWidtth - mask.offsetWidth) // Maximum moving distance of large image = - moving distance of mask * maximum distance of large image / maximum moving distance of mask bigImg.style.left = -mask.offsetLeft*(bigImg.offsetWidth - big.offsetWidth) / (small.offsetWidth - mask.offsetWidth)+"px"; bigImg.style.top = -mask.offsetTop*(bigImg.offsetHeight - big.offsetHeight) / (small.offsetHeight - mask.offsetHeight)+"px"; } // Move the mask on the small imagesmall.addEventListener('mousemove',move); small.addEventListener('mouseover',function(){ big.style.display='block'; mask.style.display='block'; }) small.addEventListener('mouseout',function(){ big.style.display='none'; mask.style.display='none'; }) </script> Effect demonstration: 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:
|
<<: Deleting two images with the same id in docker
>>: XHTML tags that are easily confused by the location of the use
By default, MySQL can accept the insertion of 0 v...
We are not discussing PHP, JSP or .NET environmen...
Table of contents 01 Problem Description 02 Solut...
Many friends found that the box model is a square...
This article example shares the specific code of ...
When using Docker in a production environment, da...
In HTML, the Chinese phrase “學好好學” can be express...
I believe that students who have learned about th...
Preface There are two types of nginx modules, off...
Table of contents Preface environment Install Cre...
1. Nginx installation steps 1.1 Official website ...
There are two ways to install MySQL 5.7. One is t...
Effect The pictures in the code can be changed by...
Preface I always thought that UTF-8 was a univers...
The load is generally estimated during system des...