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

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

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题