Javascript 每个(功能(i){ 如果(i!=0){ var item_width=$(this).outerWidth(), 项目高度=$(此).outerHeight(), 顶部页边=-((项目高度*行页边限制)+(parseInt($(this).css('margin-top'))*行页边限制)), 左边距=(项目宽度*i)+(parseInt($(this).css('margin-left'))*(i+1)); $(此[0]).css('margin-top',top\u margin); $(this.css('margin-left',left_margin); } }); } buildColumns('ul#一些#列表',5); });

Javascript 每个(功能(i){ 如果(i!=0){ var item_width=$(this).outerWidth(), 项目高度=$(此).outerHeight(), 顶部页边=-((项目高度*行页边限制)+(parseInt($(this).css('margin-top'))*行页边限制)), 左边距=(项目宽度*i)+(parseInt($(this).css('margin-left'))*(i+1)); $(此[0]).css('margin-top',top\u margin); $(this.css('margin-left',left_margin); } }); } buildColumns('ul#一些#列表',5); });,javascript,jquery,html,css,cfml,Javascript,Jquery,Html,Css,Cfml,由于我有同样的问题,找不到任何“干净”的东西,我想我已经发布了我的解决方案。在本例中,我使用了一个反向的while循环,因此我可以使用拼接而不是切片。现在的优点是splice()只需要一个索引和一个范围,而slice()需要一个索引和总数。后者在循环时往往变得困难 缺点是在追加时需要反转堆栈 示例: // @param (list): a jquery ul object // @param (cols): amount of requested columns function multiCo

由于我有同样的问题,找不到任何“干净”的东西,我想我已经发布了我的解决方案。在本例中,我使用了一个反向的
while
循环,因此我可以使用
拼接
而不是
切片
。现在的优点是splice()只需要一个索引和一个范围,而slice()需要一个索引和总数。后者在循环时往往变得困难

缺点是在追加时需要反转堆栈

示例:

// @param (list): a jquery ul object
// @param (cols): amount of requested columns
function multiColumn (list, cols) {
    var children = list.children(),
        target = list.parent(),
        liCount = children.length,
        newUl = $("<ul />").addClass(list.prop("class")),
        newItems,
        avg = Math.floor(liCount / cols),
        rest = liCount % cols,
        take,
        stack = [];

    while (cols--) {
        take = rest > cols ? (avg + 1) : avg;
        liCount -= take;

        newItems = children.splice(liCount, take);
        stack.push(newUl.clone().append(newItems));
    }

    target.append(stack.reverse());
    list.remove();
}
cols=4; liCount=35

对于切片为[0,9]的循环;[9, 18]; [18, 27]; [27,35]

使用拼接时反转=[27,8];[18, 9]; [9, 9]; [0,9]

代码:

// @param (list): a jquery ul object
// @param (cols): amount of requested columns
function multiColumn (list, cols) {
    var children = list.children(),
        target = list.parent(),
        liCount = children.length,
        newUl = $("<ul />").addClass(list.prop("class")),
        newItems,
        avg = Math.floor(liCount / cols),
        rest = liCount % cols,
        take,
        stack = [];

    while (cols--) {
        take = rest > cols ? (avg + 1) : avg;
        liCount -= take;

        newItems = children.splice(liCount, take);
        stack.push(newUl.clone().append(newItems));
    }

    target.append(stack.reverse());
    list.remove();
}
/@param(list):jquery ul对象
//@param(cols):请求列的数量
函数多列(列表、列){
var children=list.children(),
target=list.parent(),
liCount=children.length,
newUl=$(“
    ”)addClass(list.prop(“class”), 新项目, 平均值=数学楼层(liCount/cols), 剩余=li计数%cols, 拿 堆栈=[]; 而(科尔斯--){ 服用=休息>感冒?(平均+1):平均; liCount-=取数; newItems=子项。拼接(liCount,take); stack.push(newUl.clone().append(newItems)); } append(stack.reverse()); list.remove(); }
您可以尝试将其转换为cols

CSS:

Html部分:

<ul class="col">
    <li>Number 1</li>
    <li>Number 2</li>

    <li>Number 19</li>
    <li>Number 20</li>
</ul>
<ul class="col">
    <li>Number 21</li>
    <li>Number 22</li>

    <li>Number 39</li>
    <li>Number 40</li>
</ul>
  • 第一
  • 二号
  • 19号
  • 20号
  • 21号
  • 22号
  • 39号
  • 40号

Flexbox可用于在行和列方向包装项目

主要思想是将容器上的
弹性方向设置为

NB:现在很好

(示例标记取自)

ol{
显示器:flex;
弹性流:柱包裹;/*弹性方向:柱*/
高度:100px;/*需要指定高度:-(*/
}
ol~ol{
柔性流:行换行;/*柔性方向:行*/
最大高度:自动;/*替代列方向的最大高度*/
}
李{
宽度:150px;
}
a{
显示:内联块;
右边填充:35px;
}
列方向上的项目


  • 行方向上的项目


  • 令人遗憾的是,两年后仍然没有干净的方法可以做到这一点。感谢IE+1,我发现方法6是最干净的方法-它可以调整为不使用标记和很少的CSS。这个jquery插件对你有用吗?看看jquery插件。我也在使用这个解决方案,但我正在丢失列表项目符号…甚至重新设置它们或尝试一个新的解决方案n图像不工作。有人吗?现在。检查CSS中关于列选项的更多信息。
    <div class="columns cols2">
      <div class="column">
        <ul class="foo">
          <li>a</li>
          <li>b</li>
        </ul>
      </div>
      <div class="column collast">
        <ul class="foo">
          <li>c</li>
        </ul>
      </div>
    </div>
    
    $("ul#listname").clone().attr("id","listname2").after()
    
    $("ul#listname li:even").remove();
    $("ul#listname2 li:odd").remove();
    
    <cfset thelist = "1,2,3,4,5,6,7,8,9,10,11,12,13">  
    <cfset container = []>  
    <cfset container[1] = []>  
    <cfset container[2] = []>  
    <cfset container[3] = []>  
    
    <cfloop list="#thelist#" index="i">  
        <cfif i mod 3 eq 0>  
            <cfset arrayappend(container[3], i)>  
        <cfelseif i mod 2 eq 0>  
            <cfset arrayappend(container[2], i)>  
        <cfelse>  
            <cfset arrayappend(container[1], i)>  
        </cfif>  
    </cfloop>  
    
    <style type="text/css"> 
        ul li { float: left; }  
        ul li ul li { clear: left; }  
    </style>  
    
    <cfoutput>  
    <ul>  
        <cfloop from="1" to="3" index="a">  
        <li>  
            <ul>  
                <cfloop array="#container[a]#" index="i">  
                <li>#i#</li>  
                </cfloop>  
            </ul>  
        </li>  
        </cfloop>  
    </ul>  
    </cfoutput>
    
    <cfset numberOfColumns = 3 />
    <cfset numberOfEntries = 34 />
    <ul style="float:left;">
        <cfloop from="1" to="#numberOfEntries#" index="i">
            <li>#i#</li>
                <cfif NOT i MOD ceiling(numberOfEntries / numberOfColumns)>
                    </ul>
                    <ul style="float:left;">
                </cfif>
        </cfloop>
    </ul>
    
    var $cat_list = $('ul#catList'); // UL with all list items.
    var $cat_flow = $('div#catFlow'); // Target div.
    var $cat_list_clone = $cat_list.clone(); // Clone the list.
    $('li:odd', $cat_list).remove(); // Remove odd list items.
    $('li:even', $cat_list_clone).remove(); // Remove even list items.
    $cat_flow.append($cat_list_clone); // Append the duplicate to the target div.
    
    1.      4.      7.       10.
    2.      5.      8.       11.
    3.      6.      9.       12.
    
    $(document).ready(function(){
        Array.prototype.chunk = function(chunk_size){
            var array = this,
                new_array = [],
                chunk_size = chunk_size,
                i,
                length;
    
            for(i = 0, length = array.length; i < length; i += chunk_size){
                new_array.push(array.slice(i, i + chunk_size));
            }
            return new_array;
        }
    
        function buildColumns(list, row_limit) {
            var list_items = $(list).find('li').map(function(){return this;}).get(),
            row_limit = row_limit,
            columnized_list_items = list_items.chunk(row_limit);
    
            $(columnized_list_items).each(function(i){
                if (i != 0){
                    var item_width = $(this).outerWidth(),
                        item_height = $(this).outerHeight(),
                        top_margin = -((item_height * row_limit) + (parseInt($(this).css('margin-top')) * row_limit)),
                        left_margin = (item_width * i) + (parseInt($(this).css('margin-left')) * (i + 1));
    
                    $(this[0]).css('margin-top', top_margin);
                    $(this).css('margin-left', left_margin);
                }
            });
        }
    
        buildColumns('ul#some_list', 5);
    });
    
    // @param (list): a jquery ul object
    // @param (cols): amount of requested columns
    function multiColumn (list, cols) {
        var children = list.children(),
            target = list.parent(),
            liCount = children.length,
            newUl = $("<ul />").addClass(list.prop("class")),
            newItems,
            avg = Math.floor(liCount / cols),
            rest = liCount % cols,
            take,
            stack = [];
    
        while (cols--) {
            take = rest > cols ? (avg + 1) : avg;
            liCount -= take;
    
            newItems = children.splice(liCount, take);
            stack.push(newUl.clone().append(newItems));
        }
    
        target.append(stack.reverse());
        list.remove();
    }
    
    ul.col {
        width:50%;
        float:left;
    }
    
    div.clr {
        clear:both;
    }
    
    <ul class="col">
        <li>Number 1</li>
        <li>Number 2</li>
    
        <li>Number 19</li>
        <li>Number 20</li>
    </ul>
    <ul class="col">
        <li>Number 21</li>
        <li>Number 22</li>
    
        <li>Number 39</li>
        <li>Number 40</li>
    </ul>