One UL list split into multi columns with fixed height

久未见 提交于 2019-12-05 20:18:47

http://jsfiddle.net/eaewX/1/

If you encapsulate each list in a <div>, setting columns, column-width, or column-count on <div>, it will break the list into columns appropriately.

edit: It is in the jsfiddle, but I should clarify you'll need to use browser extensions for these properties currently, i.e. -webkit-, -moz-, -o-

For reference:

https://developer.mozilla.org/en-US/docs/CSS/Using_CSS_multi-column_layouts

http://dev.w3.org/csswg/css3-multicol/#columns

Here's a working solution

/* set max height of containers */
var maxHt=500;/* may need to adjust based on css*/

var $list=$('#startList')
var $container=$list.parent();

var $li=$list.find('li');

var totHt=0;    
var totItems=$li.length;
$li.each(function( idx){

    var ht=$(this).height();
    if( totHt + ht >= maxHt || idx==totItems-1){
        $('<ul>').append( $(this).prevAll().andSelf() ).appendTo( $container );
        totHt=0;
    }else{
        totHt += ht;
    }  

});

$list.remove()

DEMO: http://jsfiddle.net/rzw64/3/

Note that height set on UL needs a little extra than max height used for calcs. Additional CSS will help or can add code to get dynamic height of parent container to set maxHt allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!