1、准备两张图片,其中一张图片分辨率为另一张图片的二倍。
2、前端页面布局

//box1位左侧原图,box2为右侧放大图额显示框,son为iv class="box1">
3、js写逻辑
let box1=document.querySelector(".box1");
let box2=document.querySelector(".box2");
let son=document.querySelector(".son");
let ceng=document.querySelector(".ceng");
//鼠标移入:son,box2出现
ceng.onmouseenter=function(){
son.style.display="block"
box2.style.display="block"
}
//鼠标移出:son,box2消失
ceng.onmouseleave=function(){
son.style.display="none"
box2.style.display="none"
}
//鼠标移动:son的位置随鼠标移动,box2背景图的位置变化
ceng.onmousemove=function(e){
let sw=e.clientX-son.offsetWidth/2
let sh=e.clientY-son.offsetHeight/2
if(sw>ceng.offsetWidth-son.offsetWidth){
sw=ceng.offsetWidth-son.offsetWidth
}
else if(sw<=0){
sw=0;
}
if(sh>ceng.offsetHeight-son.offsetHeight){
sh=ceng.offsetHeight-son.offHeight
}
else if(sh<=0){
sh=0;
}
son.style.left=sw+"px"
son.style.top=sh+"px"
box2.style.backgroundPosition=`${-2*sw}px ${-2*sh}px`
}完整代码
放大镜