kabilong 发表于 2012-4-9 14:20
其他的我都不要,我只有“瀑布流”的
/*
piclist {"parent": 父元素, "list": 图片数组}
container 放置瀑布流的父容器
maxcolumn 最多多少列
space 图片间距
index 从第几张开始排列
返回值
index 当前瀑布流已经排列了多少个图片
totalwidth 当前瀑布流的总宽度
totalheight 当前瀑布流的总高度
*/
function waterfall(v) {
if(typeof(v.piclist) == "undefined") return;
var column = 1;
var totalwidth = typeof(v.totalwidth) == "undefined" ? 0 : v.totalwidth;
var totalheight = typeof(v.totalheight) == "undefined" ? 0 : v.totalheight;
var container = typeof(v.container) == "undefined" ? $("threadlist") : v.container;
var maxcolumn = typeof(v.maxcolumn) == "undefined" ? 0 : v.maxcolumn;
var space = typeof(v.space) == "undefined" ? 10 : v.space;
var index = typeof(v.index) == "undefined" ? 0 : v.index;
var tag = "";
var totalelem = [];
var tmpx = 0;
var tmpy = 0;
if(piclist.list.length > 0) {
tag = piclist.list[0].tagName;
totalelem = piclist.parent.getElementsByTagName(tag);
column = Math.floor((container.offsetWidth - space) / (piclist.list[0].offsetWidth + space));
if(maxcolumn && column > maxcolumn) {
column = maxcolumn;
}
if(!column) {
column = 1;
}
for(var i = index; i < totalelem.length; i++) {
tmpx = i % column;
tmpy = Math.floor(i / column);
totalelem.style.position = "absolute";
//如果是第一列则为 0,否则就取当前元素左侧的元素的位置加上宽度
totalelem.style.left = tmpx ? totalelem[i - 1].offsetLeft + totalelem[i - 1].offsetWidth + space + "px" : 0;
totalwidth = (totalelem[0].offsetWidth + space) * column - space;
//如果是第一行则为 0,否则就取当前元素正上方的元素的位置加上高度
totalelem.style.top = tmpy ? totalelem[i - column].offsetTop + totalelem[i - column].offsetHeight + space + "px" : 0;
totalheight = Math.max(totalheight, totalelem.offsetTop + totalelem.offsetHeight);
index++;
}
piclist.parent.style.height = totalheight + space + "px";
piclist.parent.style.width = totalwidth + "px";
}
return {"index": index, "totalwidth": totalwidth, "totalheight": totalheight};
}
function getPiclist(id, tag) {
id = id || "waterfall";
return {"parent": $(id), "list": $(id).getElementsByTagName(tag)};
} |