Javascript to turn an unordered list into multiple columns

后端 未结 7 648
忘掉有多难
忘掉有多难 2020-12-29 11:55

There doesn\'t seem to be an easy way in (well supported) css to do this. I\'m looking for a javascript solution, preferably jQuery.

I have an unordered list like th

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 12:47

    Just a little different moves up the next column instead of each li.

    var col_max_height = 6; //Max Items per column
    var col_width = 120; //Pixels
    var prevCol = 0; 
    $('.header ul').each(function() {
        $(this).find('li').each(function(index){
        column = parseInt(index/col_max_height);
        $(this).css('margin-left', column * col_width + 'px')
            if(prevCol != column) {
                $(this).css('margin-top',  '-92px').addClass('col_'+column);
                prevCol = column;
            } else {
                $(this).css('margin-top',  '0px').addClass('col_'+column);
            }
        });
    });
    

提交回复
热议问题