带有JavaScript的JavaScript可移动Divs

时间:2018-11-11 10:29:54

标签: javascript html

我想做一些有趣的可移动div。 我有以下代码:

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

          function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            // 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;
            e.preventDefault();
            // 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;
          }
        }
#draggable_shortcut {
    position: absolute;
    z-index: 9;
    text-align: center;
    cursor: grab;
}
#draggable_shortcut img {
    width: 50px;
    height: 50px;
}
#draggable_shortcut p {
    color: black;
    font-size: 14px;
    margin: 0px;
}
<div id="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>

这确实是上帝,没有问题。 如果再添加一些div,该脚本将无法正常工作, 因为这只是一个而已。 但是我想做一个以上的可移动的div。

我是JavaScript编程的新手。有什么想法吗?


感谢您的想法和脚本。 不是我对这个脚本的最后一个问题: 例如,如果用户将div放到坐标(120/105)上,是否可以将位置设置为100 px?

移至(100/100)。另一个例子:(170/355)->(200/400)。

如果可能的话,如果用户可以进行更改,那就太好了,他希望像以前一样拥有一百个软木塞或金枪鱼。

更新12.11.2018:
我发现现在可以检查坐标了。但是,只有在坐标为100而不是105的情况下,它才会被放置。有任何想法吗?演示在这里: http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW

更新12.11.2018以后...
我现在发现了可能性。对于这些想要相同的人:http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW

2 个答案:

答案 0 :(得分:2)

尝试使用 class 而不是 id

您必须分别为每个元素调用函数。您可以使用Document.querySelectorAll() 获取代表文档元素列表的静态(非实时) NodeList 。然后使用Array.prototype.forEach()调用每个元素的函数:

document.querySelectorAll(".draggable_shortcut").forEach(function(el){
  dragElement(el);
});

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

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // 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;
    e.preventDefault();
    // 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;
  }
}
.draggable_shortcut {
    position: absolute;
    z-index: 9;
    text-align: center;
    cursor: grab;
}
.draggable_shortcut img {
    width: 50px;
    height: 50px;
}
.draggable_shortcut p {
    color: black;
    font-size: 14px;
    margin: 0px;
}
<div class="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>

<div class="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>

答案 1 :(得分:0)

您需要为每个项目制作循环。

    var elements = document.getElementsByClassName("drag");
    for(var i=0; i<elements.length; i++) {
        console.log(elements[i].id)
        dragElement(document.getElementById(elements[i].id));
            function dragElement(elmnt) {
              var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
              if (document.getElementById(elmnt.id + "top-content")) {
                /* if present, the header is where you move the DIV from:*/
                document.getElementById(elmnt.id + "top-content").onmousedown = dragMouseDown;
              } else {
                /* otherwise, move the DIV from anywhere inside the DIV:*/
                elmnt.onmousedown = dragMouseDown;
              }

              function dragMouseDown(e) {
                e = e || window.event;
                e.preventDefault();
                // 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;
                e.preventDefault();
                // 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;
              }
            }
}

还将id更改为classes

.drag {
    position: absolute;
    z-index: 9;
    text-align: center;
    cursor: grab;
}
.drag img {
   width: 50px;
    height: 50px;
}
.drag p {
  color: black;
    font-size: 14px;
    margin: 0px;
}

现在每个div必须具有单独的id和相同的class

工作勤奋的人:http://plnkr.co/edit/R8PUSaxkWebiZoq1XraC?p=preview