<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul{
list-style: none;
margin: 0;
padding: 0;
width: 500px;
height: 500px;
/* border: 1px solid black; */
background-color: #ccc;
position: relative;
}
ul li{
list-style: none;
/* background-color: black; */
background-image: url(./img/img5.jpg);
/* background-color: red; */
width: 50px;
height: 50px;
position: absolute;
left: 0;
top: 0;
transition: all linear 0.4s;
}
</style>
</head>
<body>
<ul id="container">
<li></li>
</ul>
<script>
var oUl = document.getElementById("container");
// 创建 100 个li
var str = "";
for (var i = 0; i < 100; i ++) {
str += "<li></li>"
}
oUl.innerHTML = str;
var aLi = document.getElementsByTagName("li");
for (var i = 0; i < 100; i ++) {
aLi[i].style.top = Math.floor(i / 10) * 51 + "px";
aLi[i].style.left = (i % 10) * 51 + "px";
aLi[i].style.backgroundPosition = -(i % 10) * 51 + "px " + (- Math.floor(i / 10) * 51) + "px";
aLi[i].style.opacity = "0";
aLi[i].onmouseover = function (){
this.style.opacity = "1";
}
}
</script>
</body>
</html>
自己运行康吧。