doudang8824 2016-04-19 16:34
浏览 21
已采纳

希望分发div对象

I want to do a web page which can distribute objects hopefully considering the height and the width of the objects inputted.

In the first page I input the height and the width of the object, the quantity of that object, and a limit per row. And the second page will show the objects distributed.

Example:

object 35x60cm, 3 quantities | object 60x60cm, 4 quantities | object 70x70cm, 5 quantities and the limit is 200 cm a row.

The object will have distribute hopefully and when the 200cm per row is reached, another row will be added and the remain objects will situate in the 2nd row, and if the second row is not enough, then it will be a 3rd row, and so on.

Objects will be like divs!

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongyoufo5672 2016-04-19 17:57
    关注

    You could accomplish the distribution with CSS and JS only. Here's the HTML code:

    <input type="text" id="inputWidth"=> give me width</input>
    <input type="text" id="inputHeight"=> give me height</input>
    <input type="text" id="inputNum"=> give me number</input>
    <input type="text" id="inputContWidth"=> give me container width</input>
    <button onclick="addShapes()">Submit</button>
    <div id="container">
    </div>
    

    And the CSS code for the distribution. This is both to distribute the items and to make them visible:

    #container {
      position: relative;
      background-color: gray;
      height: auto;
      display: flex;
      flex-wrap: wrap;
      align-content: center;
      justify-content: center;
    }
    
    .item {
      position: relative;
      background-color: blue;
      border-style: solid;
    }
    

    Then use Javascript to add as many items as needed using the inputs:

    function addShapes() {
      removeItems();
      var inputWidth = document.getElementById("inputWidth").value;
      var inputHeight = document.getElementById("inputHeight").value;
      var inputNum = document.getElementById("inputNum").value;
      var inputContWidth = document.getElementById("inputContWidth").value;
      document.getElementById("container").style.width = inputContWidth;
      for (var i = 0; i < inputNum; i++) {
        var element = document.createElement("div");
        element.style.width = inputWidth;
        element.style.height = inputHeight;
        element.className = "item";
        document.getElementById("container").appendChild(element);
      }
    }
    
    function removeItems() {
      var elements = document.getElementsByClassName("item");
      while (elements.length > 0) {
        elements[0].parentNode.removeChild(elements[0]);
      }
    }
    

    And here's the codepen: http://codepen.io/gingerdeadshot/pen/JXZNGX

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库