45

我想让 ID 为“drag_me”的 div 标签可以从左侧拖动到 300px 的长度,从顶部拖动到 460px 的长度,仅使用 CSS。

我也想让它调整大小。再次与上面相同的条件,即没有 Javascript 或 jquery。

任何人都可以提出解决方案...

在此先感谢大家

4

9 回答 9

48

这是没有 JavaScript 可以做到的最好的事情:

[draggable=true] {
  cursor: move;
}

.resizable {
  overflow: scroll;
  resize: both;
  max-width: 300px;
  max-height: 460px;
  border: 1px solid black;
  min-width: 50px;
  min-height: 50px;
  background-color: skyblue;
}
<div draggable="true" class="resizable"></div>

演示

于 2013-05-16T05:10:43.600 回答
21

你可以看看 HTML 5,但我不认为你可以限制你可以拖动它的区域,只是目的地:

http://www.w3schools.com/html/html5_draganddrop.asp

如果你不介意使用一些很棒的库,我会鼓励你尝试Dragula

于 2012-11-23T10:06:06.727 回答
12

仅使用 css 技术这对我来说似乎是不可能的。但是你可以使用 jqueryui draggable

$('#drag_me').draggable();
于 2012-11-23T10:03:43.420 回答
10

CSS 旨在描述文档的呈现方式。它有一些功能可以根据用户交互来更改演示文稿(主要是:hover为了表明您现在正在指向交互的东西)。

制作可拖动的东西并不是简单的展示问题。它完全属于由 JavaScript 处理的交互逻辑领域。

你想要的东西是无法实现的。

于 2012-11-23T10:20:37.313 回答
7

您现在可以使用 CSS 属性来执行此操作-webkit-user-drag

#drag_me {
  -webkit-user-drag: element;
}
<div draggable="true" id="drag_me">
  Your draggable content here
</div>

此属性仅受 webkit 浏览器支持,例如 Safari 或 Chrome,但它是仅使用 CSS 使其工作的好方法。

HTML5draggable属性仅设置为确保拖动适用于其他浏览器。

您可以在这里找到更多信息:http: //help.dottoro.com/lcbixvwm.php

于 2013-08-19T15:04:38.557 回答
6

从 W3Schools 发现这个真的很有帮助:

// Make the DIV element draggable:
dragElement(document.getElementById("mydiv"));

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;
    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;
  }
}
#mydiv {
  position: absolute;
  z-index: 9;
  background-color: #f1f1f1;
  border: 1px solid #d3d3d3;
  text-align: center;
}

#mydivheader {
  padding: 10px;
  cursor: move;
  z-index: 10;
  background-color: #2196F3;
  color: #fff;
}
 <!-- Draggable DIV -->
<div id="mydiv">
  <!-- Include a header DIV with the same name as the draggable DIV, followed by "header" -->
  <div id="mydivheader">Click here to move</div>
  <p>Move</p>
  <p>this</p>
  <p>DIV</p>
</div> 

我希望你能用它来!

于 2020-04-15T21:03:46.623 回答
1

Draggable div not possible only with CSS,如果你想要draggable div 你必须使用javascript。

http://jqueryui.com/draggable/

于 2012-11-23T10:23:02.247 回答
0

在尝试通过从 Stack Overflow 复制粘贴各种代码片段自己尝试做到这一点之后,我强烈建议只使用InteractJS 库,它允许您轻松创建可拖动和可调整大小的 div(有点)。

于 2019-09-07T18:28:25.757 回答
-1
$('#dialog').draggable({ handle: "#tblOverlay" , scroll: false });

    // Pop up Window 
          <div id="dialog">
                        <table  id="tblOverlay">
                            <tr><td></td></tr>
                         <table>
           </div>

选项:

  1. handle :避免粘性滚动条问题。有时您的鼠标指针在拖动时会粘在弹出窗口上。
  2. scroll :防止弹出窗口超出父页面或超出当前屏幕。
于 2018-05-03T20:59:09.250 回答