<!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>擦出图片</title>
<style>
body{
margin: 0;
background-color: rgb(211, 241, 213);
}
.wrap{
position: relative;
width: 590px;
margin: 100px auto;
}
.wrap div{
width: 50px;
height: 50px;
position: absolute;
background: url(./img.jpg);
background-color: rgb(98, 133, 95);
background-size: 1000% 1000%;
}
</style>
</head>
<body>
<div class="wrap">
</div>
<script>
var wrap = document.querySelector(".wrap");
var inner = "";
var w = 50;
var h = 50;
var ceils = 10; // 每一行有几个
/*
0 , 1, 2, 3, 4, 5, 6, 7, 8, 9
10,11,12,13,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29
30,
40,
*/
for(var i = 0; i < 100; i++){
//console.log(i%ceils);
var x = i%ceils;
var y = parseInt(i/ceils);
console.log(y);
inner += '<div style="left:'+(x*w)+'px;top:'+(y*h)+'px;background-position: -'+(x*w)+'px -'+(y*h)+'px"></div>';
}
wrap.innerHTML = inner;
/*
% 取模 -- 求余数
任何数%n 结果只能 0 - (n-1)
parseInt() //取整
*/
</script>
</body>
</html>