且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

随机排列照片,以便

更新时间:2023-11-18 23:01:10

如果你愿意有自己的设计相互重叠,你可以让他们之间的最小差距使用客户端code动态定位的项目:

If you're willing to have the images overlap each other you can have minimum gap between them using client side code to position the items dynamically:

window.onload = function() {
    var oList = document.getElementById("liParent")
    var arrItems = oList.getElementsByTagName("li");
    var totalWidth = parseInt(oList.style.width, 10);
    var curLeft = 0;
    var curTop = 0;
    var arrHeights = new Array();
    var colIndex = 0;
    for (var i = 0; i < arrItems.length; i++) {
        var oCurItem = arrItems[i];
        var oImage = oCurItem.getElementsByTagName("img")[0];
        oCurItem.style.position = "absolute";
        var curWidth = oImage.offsetWidth;
        var curHeight = oImage.offsetHeight;
        if (curLeft + curWidth > totalWidth) {
            curLeft = 0;
            colIndex = 0;
        }
        if (colIndex < arrHeights.length)
            curTop = arrHeights[colIndex];
        oCurItem.style.left = curLeft + "px";
        oCurItem.style.top = curTop + "px";
        arrHeights[colIndex] = (curHeight + curTop);
        curLeft += curWidth;
        colIndex++;
    }
}

更新的jsfiddle: http://jsfiddle.net/zHxPT/2/