也可以计算好大小位置(position:absolute固定死),可参看小乐图客的拼图功能。
<!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>
div div {
width: 30px
height: 30px
display: inline-block
border: 1px solid black
background-repeat: no-repeat
background-size: 300% 300%
}
#img1 {
background-position: top left
}
#img2 {
background-position: top
}
#img3 {
background-position: top right
}
#img4 {
background-position: center left
}
#img5 {
background-position: center
}
#img6 {
background-position: center right
}
#img7 {
background-position: bottom left
}
#img8 {
background-position: bottom
}
#img9 {
background-position: bottom right
}
</style>
</head>
<body>
<div>
<div id="img1"></div>
<div id="img2"></div>
<div id="img3"></div>
</div>
<div>
<div id="img4"></div>
<div id="img5"></div>
<div id="img6"></div>
</div>
<div>
<div id="img7"></div>
<div id="img8"></div>
<div id="img9"></div>
</div>
<button>next</button>
</body>
<script>
let arr = ["http://iconfont.alicdn.com/t/1522400286994.jpg@100h_100w.jpg", "http://iconfont.alicdn.com/t/1510710350863.png@100h_100w.jpg", "http://iconfont.alicdn.com/t/1553563063102.jpg@100h_100w.jpg"]
let num = 0
let imgFun = () => {
document.querySelectorAll("div div").forEach(item => {
item.style.backgroundImage = 'url(' + arr[num] + ')'
})
}
imgFun()
document.querySelector("button").onclick = () => {
if (num < arr.length - 1) {
num++
} else {
num = 0
}
imgFun()
}
</script>
</html>
请采纳
拼图?不太懂
定义一个div的position为relative 然后里面的图片全部定义为position absolute属性的 然后控制他们的left 和top值就可以达到效果了
<style>.box{width:500pxheight:500px background-color:#dddposition:relative}
.box img{position:absolute}
.m1{left:0top:0}
.m2{left:20pxtop:20px}
.m3{left:40pxtop:0}
</style>
<div class="box">
<img src="xx" width="20" height="20" class="m1">
<img src="xx" width="20" height="20" class="m2">
<img src="xx" width="20" height="20" class="m3">
</div>
差不多就是这个逻辑