对齐网格中的Div

时间:2017-01-12 04:57:41

标签: javascript html css

我有8个div s,拖放在侧边栏中。我想将div对齐2行,每行4个。我排在第一排,但似乎无法得到其余部分。这是我的代码:

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    var el = ev.target;
    if (!el.classList.contains('dropzone')) {
       el = ev.target.parentNode;
       ev.target.remove();
    }
    el.appendChild(document.getElementById(data).cloneNode(true));
}

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementsByTagName("body")[0].style.marginLeft = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementsByTagName("body")[0].style.marginLeft= "0";
}
#div1, #div2, #div3, #div4 {
	float: left;
    width: 55px;
    height: 55px;
    border: 1px solid black;
}

#div5, #div6, #div7, #div8 {
	position: absolute;
    top: 136px;
    float: left;
    width: 55px;
    height: 55px;
    border: 1px solid black;
}

body {
  transition: margin .5s;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
<div id="main">
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Materials</span>
</div>

<div id="div1" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div2" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div3" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div4" class="dropzone" ondrop="drop(event)"ondragover="allowDrop(event)"></div>

<div id="div5" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div6" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div7" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div8" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>

<a href="#">

  	<img src="https://unsplash.it/50/50?image=3
" draggable="true" ondragstart="drag(event)" id="drag1" width="55" height="55">

   Bricks

</a>

<a href="#">

	<img src="https://unsplash.it/50/50?image=2
" draggable = "true" ondragstart="drag(event)" id="drag2" width="55" height="55">

  Stone

</a>
<a href="#">

  	<img src="https://unsplash.it/50/50?image=1
" draggable = "true" ondragstart="drag(event)" id="drag3" width="55" height="55">

	Wood

  </a>
  <a href="#">

    <img src="https://unsplash.it/50/50?image=0
" draggable="true" 			 ondragstart="drag(event)" id="drag4" width="55" height="55">

  Eraser

  </a>
</div>

虽然顶行是正确的,但第二行只有一个框。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你的意思是盒子像这样对齐吗?看到代码..

&#13;
&#13;
function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    var el = ev.target;
    if (!el.classList.contains('dropzone')) {
       el = ev.target.parentNode;
       ev.target.remove();
    }
    el.appendChild(document.getElementById(data).cloneNode(true));
}

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementsByTagName("body")[0].style.marginLeft = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementsByTagName("body")[0].style.marginLeft= "0";
}
&#13;
#div1, #div2, #div3, #div4, #div5, #div6, #div7, #div8 {
	float: left;
    width: 55px;
    height: 55px;
    border: 1px solid black;
}

body {
  transition: margin .5s;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}
.row {
  width:100%;
  display:inline-block;
  margin-bottom:10px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
&#13;
<div id="main">
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Materials</span>
</div>

<div class="row">
<div id="div1" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div2" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div3" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div4" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>


<div class="row">
<div id="div5" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div6" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div7" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div8" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>

<a href="#">

  	<img src="https://unsplash.it/50/50?image=3
" draggable="true" ondragstart="drag(event)" id="drag1" width="55" height="55">

   Bricks

</a>

<a href="#">

	<img src="https://unsplash.it/50/50?image=2
" draggable = "true" ondragstart="drag(event)" id="drag2" width="55" height="55">

  Stone

</a>
<a href="#">

  	<img src="https://unsplash.it/50/50?image=1
" draggable = "true" ondragstart="drag(event)" id="drag3" width="55" height="55">

	Wood

  </a>
  <a href="#">

    <img src="https://unsplash.it/50/50?image=0
" draggable="true" 			 ondragstart="drag(event)" id="drag4" width="55" height="55">

  Eraser

  </a>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您只是将另一行中的div分开

,它似乎工作正常
 <div id="main">
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Materials</span>
</div>

<div id="div1" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div2" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div3" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div4" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="row2">
<div id="div5" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div6" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div7" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div8" class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>

<a href="#">

    <img src="https://unsplash.it/50/50?image=3
" draggable="true" ondragstart="drag(event)" id="drag1" width="55" height="55">

   Bricks

</a>

<a href="#">

    <img src="https://unsplash.it/50/50?image=2
" draggable = "true" ondragstart="drag(event)" id="drag2" width="55" height="55">

  Stone

</a>
<a href="#">

    <img src="https://unsplash.it/50/50?image=1
" draggable = "true" ondragstart="drag(event)" id="drag3" width="55" height="55">

    Wood

  </a>
  <a href="#">

    <img src="https://unsplash.it/50/50?image=0
" draggable="true"           ondragstart="drag(event)" id="drag4" width="55" height="55">

  Eraser

  </a>
</div>

#div1, #div2, #div3, #div4 {
    float: left;
    width: 55px;
    height: 55px;
    border: 1px solid black;
}
#div5, #div6, #div7, #div8 {
    float: left;
    width: 55px;
    height: 55px;
    border: 1px solid black;
}

body {
  transition: margin .5s;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}
.row2{
    clear:both;
}
@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}