jquery 瀑布流插件
;(function($){$.fn.waterFall = function () {var defultes = {gap:15 ,//间距}defultes = $.extend(defultes);var $this = $(this);var item = $this.children();// console.log(item);var
·
;(function($){
$.fn.waterFall = function () {
var defultes = {
gap:15 ,//间距
}
defultes = $.extend(defultes);
var $this = $(this);
var item = $this.children();
// console.log(item);
var itemwidth = item.width();
var itemHeight = 0;
var itemarrayHeight = [];
var column = Math.floor($this.width()/itemwidth)
// console.log(column)
item.each(function (index,element) {
// console.log(index)
// console.log(element)
//获取第一行高度
itemHeight = $(element).height();
if(index < column){
$(element).css({
top:0,
left:(itemwidth+defultes.gap) * index
})
itemarrayHeight[index] = itemHeight;
}else{
var min_index = getMin(itemarrayHeight).index;
var min_value = getMin(itemarrayHeight).value;
// console.log(min_index)
// console.log(min_value)
$(element).css({
top:min_value+defultes.gap,
left:(itemwidth+defultes.gap) * min_index
})
itemarrayHeight[min_index] += itemHeight+defultes.gap;
}
// console.log(itemarrayHeight)
})
function getMin(arr) {
var min = {};
min.index = 0;
min.value = arr[min.index];
for(var i=0;i<arr.length;i++){
if(arr[i] < min.value){
min.value = arr[i];
min.index = i;
}
}
return min;
}
function getMax(arr) {
var max = {};
max.index = 0;
max.value = arr[max.index];
for(var i=0;i<arr.length;i++){
if(arr[i] > max.value){
max.value = arr[i];
max.index = i;
}
}
return max;
}
var max_value = getMax(itemarrayHeight).value;
$this.height(max_value);
}
})(jQuery)
使用
$(function(){
$(".ul").waterFall();
})
<ul class="ul">
<li class="li " style="height: 300px">
</li>
<li class="li" style="height: 400px">
</li>
<li class="li" style="height: 500px">
</li>
<style>
ul{
position:relative
}
ul li{
position:absolute
}
</style>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)