- mouseenter mouseleave 不会触发事件冒泡
- mouseover mouseout 会触发事件冒泡
在写放大镜功能时,当鼠标进入box的时候,如果用mouseover,在有的浏览器不会显示图片出来,或者鼠标移进去时会有闪动。但是用mouseenter就不会出现这个情况。mouseout也一样。所以移入,移出时最好用mouseenter和mouseleave。
思路如下:
- 鼠标经过的时候 显示 mask和bigBox , 当鼠标离开box的时候隐藏mask和bigBox
- 当鼠标在盒子中移动的时候,让mask和鼠标一起移动
- 当mask移动的时候,让大图片移动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 300px;
height: 300px;
border: 1px solid #cccccc;
margin: 200px;
position: relative;
}
.big {
width: 400px;
height: 400px;
border: 1px solid #cccccc;
left: 330px;
position: absolute;
top: 0;
display: none;
overflow: hidden;
}
.big img {
position: absolute;
}
.mask {
width: 150px;
height: 150px;
background: rgba(130, 205, 240, 0.3);
position: absolute;
top: 0;
left: 0;
cursor: move;
display: none;
}
.small {
position: relative;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="small">
<img src="images/11.jpg" alt="" width="300">
<div class="mask"></div>
</div>
<div class="big">
<img src="images/22.jpg" alt="" width="800">
</div>
</div>
<script>
// 获取页面滚动距离的浏览器兼容性问题
// 获取页面滚动出去的距离
function getScroll() {
var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
return {
scrollLeft: scrollLeft,
scrollTop: scrollTop
}
}
// 获取鼠标在页面的位置,处理浏览器兼容性
function getPage(e) {
var pageX = e.pageX || e.clientX + getScroll().scrollLeft;
var pageY = e.pageY || e.clientY + getScroll().scrollTop;
return {
pageX: pageX,
pageY: pageY
}
}
var box = document.getElementById("box");
var smallBox = box.children[0];
var bigBox = box.children[1];
var smallImg = smallBox.children[0];
var mask = smallBox.children[1];
var bigImg = bigBox.children[0];
//鼠标经过显示mask和bigBox
box.onmouseenter = function() {
mask.style.display = "block";
bigBox.style.display = "block";
}
//鼠标离开隐藏mask和bigBox
box.onmouseleave = function() {
mask.style.display = "none";
bigBox.style.display = "none";
}
//鼠标在盒子中的移动。mask和鼠标一起移动
box.onmousemove = function(e) {
e = e || window.event;
// 获取鼠标在盒子中的位置,就是mask的坐标
//鼠标在box的位置=鼠标在页面中的位置-box在页面的位置
var maskX = getPage(e).pageX - box.offsetLeft;
var maskY = getPage(e).pageY - box.offsetTop;
// 让鼠标出现在mask的中心点
maskX = maskX - mask.offsetWidth / 2;
maskY = maskY - mask.offsetHeight / 2;
//把mask限制在box中
maskX = maskX < 0 ? 0 : maskX;
maskY = maskY < 0 ? 0 : maskY;
maskX = maskX > box.offsetWidth - mask.offsetWidth ? box.offsetWidth - mask.offsetWidth : maskX;
maskY = maskY > box.offsetHeight - mask.offsetHeight ? box.offsetHeight - mask.offsetHeight : maskY;
mask.style.left = maskX + "px";
mask.style.top = maskY + "px";
//当mask移动的时候,大图片跟着移动
//mask移动的距离/mask最大能移动的距离=大图片移动的距离/大图片最大能移动的距离
// mask最大能够移动的距离
var maskMax = box.offsetWidth - mask.offsetWidth;
//大图片最大能移动距离
var bigImgMax = bigImg.offsetWidth - bigBox.offsetWidth;
var bigImgX = maskX * bigImgMax / maskMax;
var bigImgY = maskY * bigImgMax / maskMax;
bigImg.style.left = -bigImgX + "px";
bigImg.style.top = -bigImgY + "px"
}
</script>
</body>
</html>