可拖动的DIV元素彼此保持一致

时间:2018-06-09 13:00:07

标签: javascript html css

我使用的代码 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_draggable

并稍微改变一下

的CSS:

<style>
.mydiv {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    width: 48%;
    height:400px;
}
.mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
</style>

HTML:

<div id="first" class ='mydiv' >
</div>
<div id="second" class ='mydiv' >
</div>

JavaScript的:

dragElement(document.getElementById(("first")));
dragElement(document.getElementById(("second")));

问题是当我加载html文件时,第二个div超过了第一个div。

2 个答案:

答案 0 :(得分:0)

这与JavaScript代码没有任何关系。

  • 如果两个元素具有相同的z-index(假设它们位于absolute),那么标记中的最后一个(此处为#second)将位于顶部:

&#13;
&#13;
.box {
  width: 100px;
  height: 100px;
  position: absolute;
}

#first {
  background: lightgreen;
}

#second {
  background: lightblue;
}
&#13;
<div class="box" id="first"></div>
<div class="box" id="second"></div>
&#13;
&#13;
&#13;

  • 否则,如果它们具有不同的z-index值,则具有最高值的那个值排在最前面:

&#13;
&#13;
.box {
  width: 100px;
  height: 100px;
  position: absolute;
}

#first {
  background: lightgreen;
  z-index: 2;
}

#second {
  background: lightblue;
  z-index: 1;
}
&#13;
<div class="box" id="first"></div>
<div class="box" id="second"></div>
&#13;
&#13;
&#13;

&#13;
&#13;
dragElement(document.getElementById(("first")));
dragElement(document.getElementById(("second")));
&#13;
.mydiv {
  position: absolute;
  background-color: #f1f1f1;
  text-align: center;
  border: 1px solid #d3d3d3;
  width: 48%;
  height: 400px;
}

#first {
  background: pink;
  z-index: 2;
}

#second {
  background: lightblue;
  z-index: 1;
}
&#13;
<div id="first" class='mydiv'></div>
<div id="second" class='mydiv'></div>

<script>
  function dragElement(elmnt) {
    var pos1 = 0,
      pos2 = 0,
      pos3 = 0,
      pos4 = 0;
    if (document.getElementById(elmnt.id + "header")) {
      /* if present, the header is where you move the DIV from:*/
      document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
    } else {
      /* otherwise, move the DIV from anywhere inside the DIV:*/
      elmnt.onmousedown = dragMouseDown;
    }

    function dragMouseDown(e) {
      e = e || window.event;
      // get the mouse cursor position at startup:
      pos3 = e.clientX;
      pos4 = e.clientY;
      document.onmouseup = closeDragElement;
      // call a function whenever the cursor moves:
      document.onmousemove = elementDrag;
    }

    function elementDrag(e) {
      e = e || window.event;
      // calculate the new cursor position:
      pos1 = pos3 - e.clientX;
      pos2 = pos4 - e.clientY;
      pos3 = e.clientX;
      pos4 = e.clientY;
      // set the element's new position:
      elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
      elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    }

    function closeDragElement() {
      /* stop moving when mouse button is released:*/
      document.onmouseup = null;
      document.onmousemove = null;
    }
  }
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这可能会对你有所帮助

<div id="first" class ='mydiv' >
</div>
<div id="second" class ='mydiv' style="top:8px;left:51%">
</div>
<div id="third" class ='mydiv' style="top:412px;">
</div>
<div id="fourth" class ='mydiv' style="top:412px;left:51%">
</div>
相关问题