Css显示内容的flex包装

时间:2017-08-03 15:36:30

标签: javascript jquery css css3 flexbox

你可以对其动态内容进行div display: flex换行,而不是跨越整个页面/容器吗?

在此示例中,内容不是动态的,但我们假设它是:



$(document).ready(function() {
  dduCard("card", "Header", "Wrap around this content", "Footer");
});

function dduCard(targetDivId, cHeader, cBody, cFooter) {
  var targetDiv = $("#" + targetDivId);

  var mainDiv = $("<div id='placeholder'/>");
  var headerDiv = $("<div id='headerrow'/>");
  var bodyDiv = $("<div id='bodyrow' />");
  var footerDiv = $("<div id='footerrow'/>");
  mainDiv.css({
    border: "1px solid rgba(0,0,0,0.121569)",
    "border-radius": "4px",
    "box-sizing": "border-box",
    color: "rgb(41,43,44)",
    display: "flex",
    "flex-direction": "column",
    "font-size": "16px",
    "line-height": "24px",
    "text-align": "center"
  });

  headerDiv.css({
    "border-bottom-color": "rgba(0,0,0,0.121569)",
    "border-bottom-style": "solid",
    "border-bottom-width": "1px",
    "background-color": "rgb(247,247,249)",
    "border-radius": "3px 3px 0 0",
    display: "block",
    padding: "12px 20px"
  });
  bodyDiv.css({
    display: "block",
    "flex-basis": "auto",
    "flex-grow": "1",
    "flex-shrink": "1",
    "line-height": "24px",
    padding: "20px"
  });
  footerDiv.css({
    "border-top-color": "rgba(0,0,0,0.121569)",
    "border-top-style": "solid",
    "border-top-width": "1px",
    "background-color": "rgb(247,247,249)",
    "border-radius": "3px 3px 0 0",
    display: "block",
    padding: "12px 20px"
  });

  headerDiv.append(cHeader);
  bodyDiv.append(cBody);
  footerDiv.append(cFooter);

  //    append
  mainDiv.append(headerDiv).append(bodyDiv).append(footerDiv);
  targetDiv.append(mainDiv);
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div id="card">
</div>
&#13;
&#13;
&#13;

https://codepen.io/dumitrudan608/pen/eEdPVG

1 个答案:

答案 0 :(得分:0)

我不确定是什么&#39;环绕&#39;意思是,但我检查了&#39; bootstrap卡&#39;你提到的 - 并修改你的代码以适应账单。这有助于澄清问题吗? https://codepen.io/sheriffderek/pen/87cf169b570e6b0fcdfd0c928ffcabee/?editors=0010

标记

<section class="cards">

  <ul class='card-list' id='card-list'>
    <!-- dynamic list items -->
  </ul>

</section>


CSS

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.cards {
  max-width: 500px; /* arbitrary parent */
}

.card-list {

}

.card {
  border: 2px solid red;
}

.card:not(:first-of-type) {
  margin-top: 1rem; 
}

.card .row {
  padding: .5rem;
}

.card header {
  background: lightblue;
}

.card main {
  background: white;
}

.card footer {
  background: lightgreen;
}


脚本

var exampleData = [
  {
    id: 1,
    heading: "Card heading 1",
    body: "Card body 1",
    footer: "footer...",
  },
  {
    id: 2,
    heading: "Card heading 2",
    body: "Card body 2",
    footer: "footer...",
  },
  {
    id: 3,
    heading: "Card heading 3",
    body: "Card body 3",
    footer: "footer...",
  },
];

function buildCard(listName, headerCopy, mainCopy, footerCopy) {
  var $cardList = $('#' + listName);

  var $card = $("<li class='card'></li>");
  var $header = $("<header class='row header'></header>");
  var $main = $("<main class='row main'></main>");
  var $footer = $("<footer class='row footer'></footer>");

  // add content to rows
  $header.append(headerCopy);
  $main.append(mainCopy);
  $footer.append(footerCopy);

  // add rows to card
  $card.append($header).append($main).append($footer);
  $cardList.append($card);
}

$(document).ready( function() {

  // just to mock a server request for a 'list' of 'cards'
  exampleData.forEach( function(currentValue, currentIndex, fullArray) {
    var card = currentValue;
    buildCard('card-list', card.heading, card.body, card.footer);
  });

});