2019 10 16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
#itemBox {
width: 1050px;
margin: 0 auto;
position: relative;
}
.item {
border: 1px solid #ccc;
padding: 4px;
position: absolute;
}
</style>
</head>
<body>
<div id="itemBox">
<div class="item">
<img src="images/P_000.jpg">
</div>
<div class="item">
<img src="images/P_001.jpg">
</div>
<div class="item">
<img src="images/P_002.jpg">
</div>
<div class="item">
<img src="images/P_003.jpg">
</div>
<div class="item">
<img src="images/P_004.jpg">
</div>
<div class="item">
<img src="images/P_005.jpg">
</div>
<div class="item">
<img src="images/P_006.jpg">
</div>
<div class="item">
<img src="images/P_007.jpg">
</div>
<div class="item">
<img src="images/P_008.jpg">
</div>
<div class="item">
<img src="images/P_009.jpg">
</div>
<div class="item">
<img src="images/P_010.jpg">
</div>
<div class="item">
<img src="images/P_011.jpg">
</div>
<div class="item">
<img src="images/P_012.jpg">
</div>
<div class="item">
<img src="images/P_013.jpg">
</div>
<div class="item">
<img src="images/P_014.jpg">
</div>
<div class="item">
<img src="images/P_015.jpg">
</div>
<div class="item">
<img src="images/P_016.jpg">
</div>
<div class="item">
<img src="images/P_017.jpg">
</div>
<div class="item">
<img src="images/P_018.jpg">
</div>
</div>
<script>
window.onload = function() {
var itemBox = document.getElementById('itemBox');
var items = document.getElementsByClassName('item');
var itemBoxWidth = itemBox.offsetWidth;
var itemWidth = items[0].offsetWidth;
var colum =parseInt(itemBoxWidth / itemWidth);
var distance = (itemBoxWidth - colum *itemWidth) / (colum-1);
var arr = [];
waterFull();
function waterFull() {
for (var i = 0; i < items.length; i++) {
if (i<colum) {
items[i].style.left = (itemWidth+distance)*i+'px';
arr[i] = items[i].offsetHeight;
}else{
var minH = getMin(arr).minH;
var minI = getMin(arr).minI;
items[i].style.left = (itemWidth + distance) *minI+'px';
items[i].style.top = minH+distance+'px';
arr[minI] = minH + distance+items[i].offsetHeight;
}
}
}
function getMin(arr) {
var obj = {};
obj.minH = arr[0];
obj.minI = 0;
for (var i = 1; i < arr.length; i++) {
if (obj.minH > arr[i]) {
obj.minH = arr[i];
obj.minI = i;
}
}
return obj;
}
window.onscroll = function() {
if (window.pageYOffset + window.innerHeight > getMin(arr).minH) {
var json = [
{'src':'images/P_000.jpg'},
{'src':'images/P_001.jpg'},
{'src':'images/P_002.jpg'},
{'src':'images/P_003.jpg'},
{'src':'images/P_004.jpg'},
{'src':'images/P_005.jpg'},
{'src':'images/P_006.jpg'},
{'src':'images/P_007.jpg'},
{'src':'images/P_008.jpg'},
{'src':'images/P_009.jpg'},
{'src':'images/P_010.jpg'}
];
for (var i = 0; i < json.length; i++) {
var div = document.createElement('div');
div.className = 'item';
var img = document.createElement('img');
img.src = json[i].src;
div.appendChild(img);
itemBox.appendChild(div);
}
waterFull();
}
}
}
</script>
</body>
</html>