使用 JavaScript 的 JavaScript 可移动 Div

标签 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,位置应该设置为百像素?

例如,如果用户将div放在坐标(120/105)上,它应该得到移动到 (100/100)。另一个例子:(170/355) -> (200/400)。

如果可能的话,如果一个用户可以改变就好了,他会想要像以前那样的一百个 corrds 或 cutsom。

2018 年 11 月 12 日更新:
我发现现在可以检查坐标。但只有当坐标精确到 100 而不是 105 时才会放置它。有什么想法吗?演示在这里: http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW

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

最佳答案

尝试使用 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>

关于使用 JavaScript 的 JavaScript 可移动 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247852/

相关文章:

javascript - 在 Google Apps 脚本 (JavaScript) 中获取特定字符串格式的第二天日期

javascript - 试图理解两个简短的 JavaScript 函数中的作用域

javascript - 在 Gatsby 站点中集成 Drift 聊天服务

javascript - Paypal 按钮返回 'Order could not be captured'

javascript - 如何使用 JavaScript 将图像链接/url 转换为图像

python - 在 Django 中覆盖动态 CSS

html - Bootstrap 的下拉导航不起作用

html - Jekyll中高亮标签添加id属性

javascript - 如何在 JavaScript 中包含 ejs partials?

javascript - 当我运行 .onclick 时,它运行一次,然后就不再工作了