//css
*{
margin: 0;
padding: 0;
}
.item{
padding: 20px;
float: left;
border:1px solid #eee;
font-size: 0;
}
.item img{
width: 240px;
}
.content{
margin: 0 auto;
}
//script
<script>
var baseURL = "http://192.168.4.18:8000/"
var url = "more"
axios({
url,
baseURL,
method:"get",
}).then(res=>{
var a = res.data;
a.forEach(item=>{
var htmlItem=`
<div class="item">
<img src=${item.imageUrl}>
</div>
`
$(".content").append(htmlItem)
})
var ww = $(window).width(); //获取窗口宽度
var box = $(".item").outerWidth(); //获取item块宽度(包括内外边框)
var num = Math.floor(ww/box); //得出行内item块个数
var arr = [];
$(".content").css({width:box*num + "px"})
setTimeout(function(){
$(".item").each((index,value)=>{
if(index<num){
var height = $(value).outerHeight();
console.log(height)
arr.push(height) //将第一行的item块高度加入一个列表中
}else{
var minHeight = Math.min(...arr); //找到高度最小的元素
var index = arr.indexOf(minHeight); //找到高度最小的元素再数组中的下标
var offsetLeft = $(".item").eq(index).offset().left; //找到其距矿口左边的距离
$(value).css({position:"absolute",left:offsetLeft,top:minHeight});
arr[index] = minHeight + $(value).outerHeight();//更新数组
}
})
},1000)
})
</script>
下拉刷新
<script>
var baseURL = "http://192.168.4.18:8000/"
var url = "more"
axios({
url,
baseURL,
method:"get",
}).then(res=>{
var a = res.data;
a.forEach(item=>{
var htmlItem=`
<div class="item">
<img src=${item.imageUrl}>
</div>
`
$(".content").append(htmlItem)
})
var ww = $(window).width();
var box = $(".item").outerWidth();
var num = Math.floor(ww/box);
var arr = [];
$(".content").css({width:box*num + "px"})
setTimeout(function(){
$(".item").each((index,value)=>{
if(index<num){
var height = $(value).outerHeight();
console.log(height)
arr.push(height)
}else{
var minHeight = Math.min(...arr);
var index = arr.indexOf(minHeight);
var offsetLeft = $(".item").eq(index).offset().left;
$(value).css({position:"absolute",left:offsetLeft,top:minHeight});
arr[index] = minHeight + $(value).outerHeight();
}
})
},100)
})
axios({
url,
baseURL,
method:"get",
}).then(res=>{
var a = res.data;
a.forEach(item=>{
var htmlItem=`
<div class="item">
<img src=${item.imageUrl}>
</div>
`
$(".content").append(htmlItem)
})
var ww = $(window).width();
var box = $(".item").outerWidth();
var num = Math.floor(ww/box);
var arr = [];
$(".content").css({width:box*num + "px"})
setTimeout(function(){
$(".item").each((index,value)=>{
if(index<num){
var height = $(value).outerHeight();
console.log(height)
arr.push(height)
}else{
var minHeight = Math.min(...arr);
var index = arr.indexOf(minHeight);
var offsetLeft = $(".item").eq(index).offset().left;
$(value).css({position:"absolute",left:offsetLeft,top:minHeight});
arr[index] = minHeight + $(value).outerHeight();
}
})
},100)
})
$(window).scroll(function(){
var scrollTop = $(window).scrollTop(); //获取滚动条距离顶部的高
var availHeight = $(window).height(); //获取可视区域的高
var dw = $(document).height(); //获取内容区域的高
console.log(scrollTop)
if((scrollTop + availHeight) > dw -100){
axios({
url,
baseURL,
method:"get",
}).then(res=>{
var a = res.data;
a.forEach(item=>{
var htmlItem=`
<div class="item">
<img src=${item.imageUrl}>
</div>
`
$(".content").append(htmlItem)
})
var ww = $(window).width();
var box = $(".item").outerWidth();
var num = Math.floor(ww/box);
var arr = [];
$(".content").css({width:box*num + "px"})
setTimeout(function(){
$(".item").each((index,value)=>{
if(index<num){
var height = $(value).outerHeight();
console.log(height)
arr.push(height)
}else{
var minHeight = Math.min(...arr);
var index = arr.indexOf(minHeight);
var offsetLeft = $(".item").eq(index).offset().left;
$(value).css({position:"absolute",left:offsetLeft,top:minHeight});
arr[index] = minHeight + $(value).outerHeight();
}
})
},100)
})
}
})
</script>