When flexbox items wrap in column mode, container does not grow its width

后端 未结 6 1992
长情又很酷
长情又很酷 2020-11-21 05:22

I am working on a nested flexbox layout which should work as follows:

The outermost level (ul#main) is a horizontal list that must expand to the right w

6条回答
  •  無奈伤痛
    2020-11-21 06:11

    Since no solution or proper workaround was suggested yet, I managed to obtain the requested behavior with a little different approach. Instead of separating the layout into 3 different divs, I'm adding all the items into 1 div and creating the separation with some more divs in between.

    The proposed solution is hard coded, assuming we have 3 sections, but can be extended to a generic one. The main idea is to explain how we can achieve this layout.

    1. Adding all the items into 1 container div that uses flex to wrap the items
    2. The first item of each "inner container" (I'll call it a section) will have a class, which helps us to do some manipulations that create the separation and styling of each section.
    3. Using :before on each first item, we can locate the title of each section.
    4. Using space creates the gap between the sections
    5. Since the space won't cover the full height of the section I'm also adding :after to the sections so positioning it with absolute position and white background.
    6. To style the background color of each section I'm adding another div inside the first item of each section. I will be position with absolute as well and will have z-index: -1.
    7. To get the correct width of each background, I'm using JS, setting the correct width, and also adding a listener to resize.

    function calcWidth() {
      var size = $(document).width();
      var end = $(".end").offset().left;
    
      var todoWidth = $(".doing-first").offset().left;
      $(".bg-todo").css("width", todoWidth);
    
      var doingWidth = $(".done-first").offset().left - todoWidth;
      $(".bg-doing").css("width", doingWidth);
    
      var doneWidth = $(".end").offset().left - $(".done-first").offset().left;
      $(".bg-done").css("width", doneWidth + 20);
    
    }
    
    calcWidth();
    
    $(window).resize(function() {
      calcWidth();
    });
    .container {
      display: flex;
      flex-wrap: wrap;
      flex-direction: column;
      height: 120px;
      align-content: flex-start;
      padding-top: 30px;
      overflow-x: auto;
      overflow-y: hidden;
    }
    
    .item {
      width: 200px;
      background-color: #e5e5e5;
      border-radius: 5px;
      height: 20px;
      margin: 5px;
      position: relative;
      box-shadow: 1px 1px 5px 0px rgba(0, 0, 0, 0.75);
      padding: 5px;
    }
    
    .space {
      height: 150px;
      width: 10px;
      background-color: #fff;
      margin: 10px;
    }
    
    .todo-first:before {
      position: absolute;
      top: -30px;
      height: 30px;
      content: "To Do (2)";
      font-weight: bold;
    }
    
    .doing-first:before {
      position: absolute;
      top: -30px;
      height: 30px;
      content: "Doing (5)";
      font-weight: bold;
    }
    
    .doing-first:after,
    .done-first:after {
      position: absolute;
      top: -35px;
      left: -25px;
      width: 10px;
      height: 180px;
      z-index: 10;
      background-color: #fff;
      content: "";
    }
    
    .done-first:before {
      position: absolute;
      top: -30px;
      height: 30px;
      content: "Done (3)";
      font-weight: bold;
    }
    
    .bg-todo {
      position: absolute;
      background-color: #FFEFD3;
      width: 100vw;
      height: 150px;
      top: -30px;
      left: -10px;
      z-index: -1;
    }
    
    .bg-doing {
      position: absolute;
      background-color: #EFDCFF;
      width: 100vw;
      height: 150px;
      top: -30px;
      left: -15px;
      z-index: -1;
    }
    
    .bg-done {
      position: absolute;
      background-color: #DCFFEE;
      width: 10vw;
      height: 150px;
      top: -30px;
      left: -15px;
      z-index: -1;
    }
    
    .end {
      height: 150px;
      width: 10px;
    }
    
    
    Drink coffee
    Go to work
    1
    2
    3
    4
    5
    1
    2
    3

提交回复
热议问题