JingdongMagnifying glass effect
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>放大镜</title> <style type="text/css"> *{ margin: 0; padding: 0; } #small{ float: left; width:450px; height:450px; border: 1px solid black; margin-left: 100px; position:absolute; } #big{ float: left; width:600px; height:600px; overflow: hidden; border: 1px solid black; position: absolute; left:600px; top: 0px; } #magnifying{ width: 200px; height:200px; background-color: cornflowerblue; opacity: 0.4; left: 0px; top: 0px; position: absolute; } #bigImg{ position: absolute; width: 1350px; height:1350px; } </style> </head> <body> <p id="small"> <img src="img/1.png" / alt="How to create a magnifying glass effect for JD product details" >//记得将其设置与小框大小一致 <p id="magnifying"></p> </p> <p id="big"> <img src="img/2.jpg" id="bigImg" / alt="How to create a magnifying glass effect for JD product details" > </p> <script type="text/javascript"> var small=document.getElementById("small"); var magnifying=document.getElementById("magnifying"); var big=document.getElementById("big"); var bigImg=document.getElementById("bigImg"); small.onmouseenter=function(){ magnifying.style.display="block"; bigImg.style.display="block" } small.onmouseleave=function(){ magnifying.style.display="none"; bigImg.style.display="none"; } small.onmousemove=function(event){ var left=event.clientX-small.offsetLeft-magnifying.offsetWidth/2; var top=event.clientY-small.offsetTop-magnifying.offsetHeight/2; var leftMax=small.offsetWidth-magnifying.offsetWidth; var topMax=small.offsetHeight-magnifying.offsetHeight; //限制鼠标移动的区域 left = left<=0 ? 0 : left; top = top <=0? 0:top; //限制右边界与下边界 left =left>=leftMax?leftMax:left; top =top>=topMax?topMax:top; magnifying.style.left=left+"px"; magnifying.style.top=top+"px"; //核心代码 var imgLef=-left/leftMax *(bigImg.offsetWidth-big.offsetWidth); var imgTop=-top/topMax * (bigImg.offsetHeight-big.offsetHeight); bigImg.style.left=imgLef+"px"; bigImg.style.top=imgTop+"px"; } </script> </body></html>
Detailed explanation of the constructor pattern of JS design patterns
Why should the front end use modularity?
Web front-end solution to browser compatibility issues
The above is the detailed content of How to create a magnifying glass effect for JD product details. For more information, please follow other related articles on the PHP Chinese website!