HTML columns layout with fixed height and horizontal scroll

前端 未结 1 901
南笙
南笙 2021-01-26 16:16

For an android project, I need to show a view (WebView), that dynamically loads content. Content items will be

tags that are adde
1条回答
  •  死守一世寂寞
    2021-01-26 17:00

    http://jsfiddle.net/B4RPJ/

    You can iterate through the content items, check if they fit in the column, and if they don't, create a new column and move the rest of the items to the new column ... like so:

    $(".content-item").each( function() {
        // iterate the content items and see if the bottom is out of the container
         var bottom = $(this).position().top + $(this).height();
        if ( bottom > $(this).parent().height() ) {
            // shift it and following content items to new column
            columnShift( $(this) );
        }
    } );
    
    function columnShift( el ) {
        var parent = el.parent();
        var container = $(".content-container");
    
        // create a new column
        var newCol = $("
    "); // get the content items that don't fit and move them to new column el.nextAll(".content-item").appendTo( newCol ); el.prependTo( newCol ); // widen the parent container container.width( container.width() + parent.width() ); // add the new column to the DOM parent.after( newCol ); }

    with html

    with an arbitrary number of .content-item divs, containing an arbitrary amount of content

    and css of

    .content-holder {
        float: left;
        width: 300px;
        height: 300px;
        border: 1px solid #000000;
        overflow: hidden;
        margin: 5px;
        padding: 10px;
    }
    
    .content-item {
        max-height: 280px;
        overflow: hidden;
    }
    

    I'm sure you can make the code more clever, but this should be a start

    0 讨论(0)
提交回复
热议问题