<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul,
li {
list-style: none;
}
ul {
width: 728px;
height: 172px;
margin: 0 auto;
}
li {
width: 172px;
height: 172px;
border: 1px solid black;
float: left;
margin: 5px;
}
.big {
width: 400px;
height: 400px;
border: 1px solid gray;
position: absolute;
top: 0;
left: 0;
display: none;
}
.big img {
width: 100%;
height: 100%;
}
</style>
<body>
<ul>
<li>
<div>
<img src="./images/shirt_1.jpg">
</div>
</li>
<li>
<div>
<img src="./images/shirt_2.jpg">
</div>
</li>
<li>
<div>
<img src="./images/shirt_3.jpg">
</div>
</li>
<li>
<div>
<img src="./images/shirt_4.jpg">
</div>
</li>
</ul>
<div class="big">
<img>
</div>
</body>
</html>
<script>
var oli = document.querySelectorAll("ul li");
var obox = document.querySelector(".big");
console.log(obox.children[0]);
for (let i = 0; i < oli.length; i++) {
oli[i].onmousemove = function (e) {
var e = e || event;
obox.style.display = "block";//鼠标划在li上的时候让box显示
obox.children[0].src = "./images/shirt_" + (i + 1) + ".jpg";
obox.style.top = e.pageY + 10 + "px";//加10:让鼠标不要落在box的身上防止闪动
obox.style.left = e.pageX + 10 + "px";
e.stopprogation ? e.stopprogation() : e.cancelBubble = true;//document也添加了相同事件,阻止冒泡
}
}
document.onmousemove = function () {
obox.style.display = "none";
}
</script>