Native js realizes the effect of commercial magnifying glass
- 2021-07-10 18:29:05
- OfStack
Implementation principle
Magnifying glass on big picture: Display area of small picture = size of big picture: size of small picture = offsetLeft of big picture: offsetLeft of small picture
Then only offsetLeft of large picture is unknown in the above formula, so offsetLeft of large picture = large picture size/small picture size * offsetLeft of small picture
There are detailed comments in the code
Complete code
Note: Replace the pictures to see the effect after copying to the local area
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
#demo{display:block;width:400px;height:255px;margin:50px;position:relative;border:1px solid#ccc}
#small-box{position:relative;z-index:1}
#float-box{display:none;width:160px;height:120px;position:absolute;background:#ffffcc;border:1px solid#ccc;filter:alpha(opacity=50);opacity:0.5}
#mark{position:absolute;display:block;width:400px;height:255px;background-color:#fff;filter:alpha(opacity=0);opacity:0;z-index:10}
#big-box{display:none;position:absolute;top:0;left:460px;width:400px;height:300px;overflow:hidden;border:1px solid#ccc;z-index:1}
#big-box img{position:absolute;z-index:5}
</style>
</head>
<body>
<div id="demo">
<div id="small-box">
<div id="mark"></div>
<div id="float-box"></div>
<img src="images/small.jpg"/>
</div>
<div id="big-box">
<img src="images/big.jpg"/>
</div>
</div>
<script type="text/javascript">
// Execute multiple function scenarios immediately after the page loads
function addloadEvent(func){
var oldonload=window.onload;
if(typeof window.onload !="function"){
window.onload=func;
}
else{
window.onload=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
// Execute multiple functions immediately after the page loads. Scenario ends
addloadEvent(b);
function b(){
// Get Perimeter Container
var demo=document.getElementById("demo");
// Get Small Picture Container
var s_Box=document.getElementById("small-box");
// Get Large Picture Container
var b_Box=document.getElementById("big-box");
// Get a large picture
var b_Image=b_Box.getElementsByTagName("img")[0];
// Get magnifying glass
var f_Box=document.getElementById("float-box");
// The top cover plate is used for mouse movement
var mark=document.getElementById("mark");
// Move into magnifying glass and large picture container display
mark.onmouseover=function(){
f_Box.style.display="block";
b_Box.style.display="block";
}
// Remove magnifying glass and hide large picture container
mark.onmouseout=function(){
f_Box.style.display="none";
b_Box.style.display="none";
}
// Move event
mark.onmousemove=function(ev){
// Get mouse coordinates window Compatible ie
var e=ev||window.event;
// Current mouse x Shaft - Container relative body Offset - Offset value of small container relative to parent container - Magnifying glass width 1 Half = Current position of magnifying glass
var left=e.clientX-demo.offsetLeft-s_Box.offsetLeft-f_Box.offsetWidth/2;
// The formula is the same as above
var top=e.clientY-demo.offsetTop-s_Box.offsetTop-f_Box.offsetHeight/2;
// Judge is displayed on the edge when the magnifying glass is moved out of the container
if(left<0){
left=0;
}else if(left>(s_Box.offsetWidth-f_Box.offsetWidth)){
left=s_Box.offsetWidth-f_Box.offsetWidth;
}
if(top<0){
top=0;
}else if(top>(s_Box.offsetHeight-f_Box.offsetHeight)){
top=s_Box.offsetHeight-f_Box.offsetHeight;
}
// Magnifier current position
f_Box.style.left=left+"px";
f_Box.style.top=top+"px";
// Acquisition ratio
var z=b_Image.offsetWidth/s_Box.offsetWidth;
// Offset with magnifying glass * Proportion = The offset of a large picture is in the opposite direction, so it is negative
b_Image.style.left=-left*z+"px";
b_Image.style.top=-top*z+"px";
}
}
</script>
</body>
</html>