<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>哈哈</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 350px;
height: 350px;
margin: 100px;
border: 1px solid #ccc;
position: relative;
}
.big {
width: 400px;
height: 400px;
position: absolute;
top: 0;
left: 360px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
}
.mask {
width: 175px;
height: 175px;
background: rgba(255, 255, 0, 0.4);
position: absolute;
top: 0px;
left: 0px;
cursor: move;
display: none;
}
.small {
position: relative;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="small"><!--小层-->
<img src="images/small.png" width="350" alt=""/>
<div class="mask"></div><!--遮挡层
</div><!--小图-->
<div class="big"><!--大层-->
<img src="images/big.jpg" width="800" alt=""/><!--大图-->
</div><!--大图-->
</div>
<!--导入外部的js文件-->
<script src="common.js"></script>
<script>
var box=my$("box");
//获取小图的div
var small =box.children[0];
//获取遮挡层
var mask = small.children[1];
//获取大图div
var big =box.children[1];
//获取大图
var bigImg =big.children[0];
box.onmouseover=function(){
mask.style.display="block";
big.style.display="block";
}
box.onmouseout=function(){
mask.style.display="none";
big.style.display="noen";
}
small.onmousemove=function(e){
//获取鼠标此时可视区域的横纵坐标去掉margin
var x =e.clientX-100;
var y=e.clientY-100;
// 设置鼠标在遮挡层的中间显示
x=x-mask.offsetWidth/2;
y=y-mask.offsetHeight/2;
//设置坐标的大值和最小值
x= x<0?0:x
y= y<0?0:y
x=x>small.offsetWidth-mask.offsetWidth?small.offsetWidth-mask.offsetWidth:x;
y=y>small.offsetHeight-mask.offsetHeight?small.offsetHeight-mask.offsetHeight:y;
//赋值给遮挡层
mask.style.left=x+"px";
mask.style.top=y+"px";
var maxX=bigImg.offsetWidth-big.offsetWidth;
var maxY=bigImg.offsetHeight-big.offsetHeight;
var bigImgX=x*maxX/(small.offsetWidth-mask.offsetWidth)
var bigImgY=y*maxY/(small.offsetHeight-mask.offsetHeight)
bigImg.style.marginLeft=-bigImgX+"px";
bigImg.style.marginTop=-bigImgY+"px";
}
//遮挡层的移动距离/大图的移动距离 =遮挡最大的移 动距离/大图最大的移动距离
//大图的移动距离=遮挡层的移动距离*大图最大的移动距离/遮挡最大的移 动距离
`在这里插入代码片`***加粗样式***</script>
</body>
</html>
对比图片放大
最新推荐文章于 2022-11-22 15:36:38 发布