使用弹性布局时,如何使弹性行宽相同

这是我的代码:


<!DOCTYPE html>

<html>

<head>

  <meta charset="utf-8">

  <!--mobile friendly-->

  <meta name="viewport" content="width=device-width, user-scalable=yes">

  <link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.css">

  <script type="text/javascript" src="../../node_modules/jquery/dist/jquery.js"></script>

  <style>

    #container {

      display:flex;

      flex-wrap: wrap;

    }

  </style>

</head>

<body>

<div id="container" >

</div>

<script>

  class App {

    constructor() {

      document.addEventListener("DOMContentLoaded", () => {

        this.init()

      })

    }


    init() {

      var container = document.querySelector("#container")

      $(container).width()


      function getRandomColor() {

        var letters = '0123456789ABCDEF';

        var color = '#';

        for (var i = 0; i < 6; i++) {

          color += letters[Math.floor(Math.random() * 16)];

        }

        return color;

      }


      for (var i = 0; i < 50; i++) {

        let el = $(`<div/>`);

        el.css("background-color", getRandomColor())

        el.width(Math.floor(100 + Math.random() * 100))

        el.css("height", "20vh")

        container.appendChild(el[0])

      }

    }

  }


 new App()

</script>

</body>

</html>

http://img4.mukewang.com/62fda6060001e4d006590297.jpg

它的右侧有不相同的填充,但我希望它就像谷歌图像搜索页面(所有行左侧和右侧填充都是相同的),如何做

http://img2.mukewang.com/62fda6160001f0e513360475.jpg

明月笑刀无情
浏览 144回答 3
3回答

守着星空守着你

只需添加到您的项目:flex-grow:1class App {&nbsp; constructor() {&nbsp; &nbsp; document.addEventListener("DOMContentLoaded", () => {&nbsp; &nbsp; &nbsp; this.init()&nbsp; &nbsp; })&nbsp; }&nbsp; init() {&nbsp; &nbsp; var container = document.querySelector("#container")&nbsp; &nbsp; $(container).width()&nbsp; &nbsp; function getRandomColor() {&nbsp; &nbsp; &nbsp; var letters = '0123456789ABCDEF';&nbsp; &nbsp; &nbsp; var color = '#';&nbsp; &nbsp; &nbsp; for (var i = 0; i < 6; i++) {&nbsp; &nbsp; &nbsp; &nbsp; color += letters[Math.floor(Math.random() * 16)];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return color;&nbsp; &nbsp; }&nbsp; &nbsp; for (var i = 0; i < 50; i++) {&nbsp; &nbsp; &nbsp; let el = $(`<div/>`);&nbsp; &nbsp; &nbsp; el.css("background-color", getRandomColor())&nbsp; &nbsp; &nbsp; el.width(Math.floor(100 + Math.random() * 100))&nbsp; &nbsp; &nbsp; container.appendChild(el[0])&nbsp; &nbsp; }&nbsp; }}new App()#container {&nbsp; display: flex;&nbsp; flex-wrap: wrap;}#container>* {&nbsp; flex-grow: 1;&nbsp; height:25vh;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"><div id="container"></div>

桃花长相依

#container {&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; flex-wrap: wrap;&nbsp; &nbsp; &nbsp; ////////////////&nbsp; &nbsp; &nbsp; justify-content: space-between;&nbsp; &nbsp; }

胡子哥哥

您必须为 div 标签的宽度设置 flex-basis:#container&nbsp; div{&nbsp; &nbsp; &nbsp; &nbsp; flex-grow: 0;&nbsp; &nbsp; &nbsp; &nbsp;flex-shrink: 1;&nbsp; &nbsp; &nbsp; &nbsp;flex-basis: 20%&nbsp; &nbsp; &nbsp; }然后在 js 中为宽度设置 100%:&nbsp;el.css("width", "100%")<!DOCTYPE html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <!--mobile friendly-->&nbsp; <meta name="viewport" content="width=device-width, user-scalable=yes"><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"><script&nbsp; src="https://code.jquery.com/jquery-3.5.1.min.js"&nbsp; integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="&nbsp; crossorigin="anonymous"></script>&nbsp; <style>&nbsp; &nbsp; #container {&nbsp; &nbsp; &nbsp; display:flex;&nbsp; &nbsp; &nbsp; flex-wrap: wrap;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#container&nbsp; div{&nbsp; &nbsp; &nbsp; &nbsp; flex-grow: 0;&nbsp; &nbsp; &nbsp; &nbsp;flex-shrink: 1;&nbsp; &nbsp; &nbsp; &nbsp;flex-basis: 20%&nbsp; &nbsp; &nbsp; }&nbsp; </style></head><body><div id="container" ></div><script>&nbsp; class App {&nbsp; &nbsp; constructor() {&nbsp; &nbsp; &nbsp; document.addEventListener("DOMContentLoaded", () => {&nbsp; &nbsp; &nbsp; &nbsp; this.init()&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; init() {&nbsp; &nbsp; &nbsp; var container = document.querySelector("#container")&nbsp; &nbsp; &nbsp; $(container).width()&nbsp; &nbsp; &nbsp; function getRandomColor() {&nbsp; &nbsp; &nbsp; &nbsp; var letters = '0123456789ABCDEF';&nbsp; &nbsp; &nbsp; &nbsp; var color = '#';&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < 6; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color += letters[Math.floor(Math.random() * 16)];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return color;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for (var i = 0; i < 50; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let el = $(`<div/>`);&nbsp; &nbsp; &nbsp; &nbsp; el.css("background-color", getRandomColor())&nbsp; &nbsp; &nbsp; &nbsp; el.css("width", "100%")&nbsp; &nbsp; &nbsp; &nbsp; el.css("height", "20vh")&nbsp; &nbsp; &nbsp; &nbsp; container.appendChild(el[0])&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp;new App()</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript