#box1{
width: 180px;
height: 180px;
overflow: hidden;
position: relative;
left: 100px;
cursor: move;
}
#box1 span{
position: absolute;
width: 100px;
height: 100px;
background: yellow;
opacity: .5;
display: inline-block;
top:0;
left: 0;
display: none;
}
#box2{
width: 500px;
height: 500px;
position: absolute;
overflow: hidden;
left: 350px;
top:0;
display: none;
}
#box2 img{
position: absolute;
top:0;
left: 0
}
</style>
<body>
<div id="box1">
<!-- 小图 -->
<img src="anli.PNG.png">
<!-- 鼠标移上去出现的拖动块 -->
<span></span>
</div>
<div id="box2">
<!-- 大图 -->
<img src="anli.PNG.png">
</div>
<script>
var box1 =document.querySelector('#box1')
var mask =box1.children[1]
var box2 =document.querySelector('#box2')
var bigimg=box2.children[0]
box1.addEventListener('mouseover',function(){
mask.style.display ='block'
box2.style.display ='block'
})
box1.addEventListener('mouseout',function(){
mask.style.display ='none'
box2.style.display ='none'
})
box1.addEventListener('mousemove',function(e){
var x=e.pageX-box1.offsetLeft-mask.offsetWidth/2
var y=e.pageY-box1.offsetTop-mask.offsetHeight/2
var mask_Max=box1.offsetWidth-mask.offsetWidth
if (x<=0) {
x=0
} else if(x>=mask_Max) {
x=mask_Max
}
if (y<=0) {
y=0
} else if(y>=mask_Max) {
y=mask_Max
}
mask.style.left=x+'PX'
mask.style.top=y+'PX'
var bigmax =bigimg.offsetWidth-box2.offsetWidth
var bigmay =bigimg.offsetHeight-box2.offsetHeight
bigx=x*bigmax/mask_Max
bigy=y*bigmay/mask_Max
bigimg.style.left=-bigx+'px'
bigimg.style.top=-bigy+'px'