一.先将HTMLCSS部分写好
(不要纠结这里写的好坏,每个人的习惯不同,能写出来想要的效果,自己写的代码自己首先得能看得懂)
<style>
.box1 {
width: 350px;
height: 350px;
position: relative;
border: 1px solid #ccc;
}
.small {
width: 350px;
height: 350px;
position: relative;
}
.big {
width: 480px;
height: 480px;
position: relative;
position: absolute;
top: 0;
right: -480px;
display: none;
overflow: hidden;
}
.imgbig {
position: absolute;
top: 0;
right: 0;
}
.zhezhao {
width: 210px;
height: 210px;
position: absolute;
top: 0;
left: 0;
background-color: #ff0;
opacity: .3;
cursor: move;
display: none;
}
</style>
<body>
<div class="box1">
<div class="small">
<img src="./small.jpg" alt="" class="imgsmall">
<div class="zhezhao"></div>
</div>
<div class="big">
<img src="./big.jpg" alt="" class="imgbig">
</div>
</div>
</body>
效果图:
二.JS部分
1.先将需要操作的元素找到
// 大盒子
let big = document.querySelector('.big')
// 小盒子
let small = document.querySelector('.small')
// 遮罩层
let shade = document.querySelector('.zhezhao')
// 大图片
let imgbig = document.querySelector('.imgbig')
2.鼠标经过小盒子让大盒子和遮罩层显示
因为放大镜默认鼠标不经过小盒子,大图片和遮罩层是隐藏的,所以在css样式中已经用display:none先将他们隐藏,只有鼠标经过小盒子时的时候,在让他们显示
small.onmouseenter = function() {
big.style.display = 'block'
shade.style.display = 'block'
}
3.在onmouseenter内在嵌套一个onmousemove鼠标移动事件
计算出遮罩层移动的距离,创建两个变量接收遮罩层X轴与Y轴移动的距离
a为事件触发对象,onmousemove为鼠标移动事件,所以这里a就代表鼠标
a.pageX为鼠标在X轴移动的距离,.scrollWidth为遮罩层宽度
a.pageX -shade.scrollWidth/2(因为图片宽高一致,所以这里写Width或者Height都可以) 除以二是为了让鼠标刚好在遮罩层中间,得到遮罩层移动的距离
small.onmousemove = function(a) {
let left = a.pageX - shade.scrollWidth / 2
let top = a.pageY - shade.scrollHeight / 2
}
4.判断移动距离是否超出small
这里做一下判断 因为如果鼠标移动的距离过短或过长,就会导致遮罩层溢出盒子 如果算出来的距离小于0 也就是盒子的左边界就让遮罩层的移动距离等于0 也就是不能移出边界外 ,同理如果移动过大会导致右边溢出,所以这里在做一下判断,先算出遮罩层最大移动距离 遮罩层最大移动距离为盒子的宽度减去遮罩层的宽度 如果移动的距离大于这个距离 就让它等于最大距离
if (left < 0) {
left = 0
} else if (left > small.scrollWidth - shade.scrollWidth) {
left = small.scrollWidth - shade.scrollWidth
}
if (top < 0) {
top = 0
} else if (top > small.scrollWidth - shade.scrollWidth) {
top = small.scrollWidth - shade.scrollWidth
}
5.将最终的值赋给遮罩层
这里别忘了加px 这里是将值赋给样式里,所以要加单位
shade.style.left = left + 'px'
shade.style.top = top + 'px'
6.接下来算大图移动的距离,先算出大图与小图的比例
let bili = small.scrollWidth / big.scrollWidth
7.算出大图移动的距离并赋给大图
因为大图与小图移动的距离相反所以这里别忘了加- 求负值
最后在将得到的值赋给大图相对应的left和top就行了
imgbig.style.left = -left / bili + 'px'
imgbig.style.top = -top / bili + 'px'
8.最后在让鼠标划出时让遮罩层和大图隐藏就可以了
small.onmouseout = function() {
shade.style.display = 'none'
big.style.display = 'none'
}
所有代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="../2.CSS/reset.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1 {
width: 350px;
height: 350px;
position: relative;
border: 1px solid #ccc;
}
.small {
width: 350px;
height: 350px;
position: relative;
}
.big {
width: 480px;
height: 480px;
position: relative;
position: absolute;
top: 0;
right: -480px;
display: none;
overflow: hidden;
}
.imgbig {
position: absolute;
top: 0;
right: 0;
}
.zhezhao {
width: 210px;
height: 210px;
position: absolute;
top: 0;
left: 0;
background-color: #ff0;
opacity: .3;
cursor: move;
display: none;
}
</style>
</head>
<body>
<div class="box1">
<div class="small">
<img src="./small.jpg" alt="" class="imgsmall">
<div class="zhezhao"></div>
</div>
<div class="big">
<img src="./big.jpg" alt="" class="imgbig">
</div>
</div>
<script>
// 大盒子
let big = document.querySelector('.big')
// 小盒子
let small = document.querySelector('.small')
// 遮罩层
let shade = document.querySelector('.zhezhao')
// 大图片
let imgbig = document.querySelector('.imgbig')
small.onmouseover = function() {
shade.style.display = 'block'
big.style.display = 'block'
small.onmousemove = function(a) {
let left = a.pageX - shade.scrollWidth / 2
let top = a.pageY - shade.scrollHeight / 2
if (left < 0) {
left = 0
} else if (left > small.scrollWidth - shade.scrollWidth) {
left = small.scrollWidth - shade.scrollWidth
}
if (top < 0) {
top = 0
} else if (top > small.scrollWidth - shade.scrollWidth) {
top = small.scrollWidth - shade.scrollWidth
}
shade.style.left = left + 'px'
shade.style.top = top + 'px'
let bili = small.scrollWidth / big.scrollWidth
imgbig.style.left = -left / bili + 'px'
imgbig.style.top = -top / bili + 'px'
}
}
small.onmouseout = function() {
shade.style.display = 'none'
big.style.display = 'none'
}
</script>
</body>
</html>