javascript - 如何从可拖动元素制作可拖动元素?

标签 javascript html css

我有一个可拖动的窗口,我想让这个窗口必须是独立的并且可以被拖动的文档,当放下时,元素被复制到它被拖动的位置并且它也留在窗口中.基本上当你应该做的是将文档从窗口拖到屏幕的任何地方,当你停止拖动时,文档被复制并且你在窗口上有一个文档,在屏幕上有一个文档。屏幕上的无法复制,但可以拖动。我该怎么做?

dragElement(document.getElementById("documento"));
dragElement(document.getElementById("docwindow"));

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;
    }
}
#janela,
#docwindow,
#BlueWindow {
    position: absolute;
    width: 40%;
    height: 38%;
    left: 100px;
}

#docwindowheader,
#BlueWindowheader {
    height: 7%;
    background: rgb(30, 87, 153);
    /* Old browsers */
    background: -moz-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 50%, rgba(32, 124, 202, 1) 51%, rgba(125, 185, 232, 1) 100%);
    /* FF3.6-15 */
    background: -webkit-linear-gradient(top, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 50%, rgba(32, 124, 202, 1) 51%, rgba(125, 185, 232, 1) 100%);
    /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to bottom, rgba(30, 87, 153, 1) 0%, rgba(41, 137, 216, 1) 50%, rgba(32, 124, 202, 1) 51%, rgba(125, 185, 232, 1) 100%);
    /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8', GradientType=0);
    /* IE6-9 */
}

#closeDocs,
#closeBlue {
    width: 15px;
    height: 15px;
    position: absolute;
    border-radius: 100%;
    top: 4.2%;
    right: 1%;
    z-index: 2;
}

#docsHeadTexto,
#JoanaPTexto {
    color: black;
    text-align: center;
    text-shadow: 3px 2px grey;
    font-size: 95%;
    top: 25%;
}

#DocImg {
    width: 20%;
    height: 20%;
    background-color: none;
    padding: 5px;
}

img#DocImg {}

#bottomWindowDocs {
    background-color:pink;
    height: 80%;
    border-bottom-left-radius: 5%;
    border-bottom-right-radius: 5%;
}

#DocEx {
    position: absolute;
    top: 33%;
    left: 4%;
    font-size: 10px;
}

#DocEx {
    z-index: 6;
}
<div class="janelas" id="docwindow">
                <div id="docwindowheader">
                    <header class="windowTop">
                        <h1 id="docsHeadTexto">Documents</h1>
                        <img id="closeDocs" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png" alt="X" onclick="closeMain()">
                    </header>
                </div>

                <div id="bottomWindowDocs">
                    <div class="documents">
                        <div id="documento">
                            <img id="DocImg" src="https://img.icons8.com/pastel-glyph/2x/file.png" alt="doc">
                            <h1 id="DocEx">Doc-example</h1>
                        </div>
                    </div>
                    <!----<div id="DocExemplo" S>
                  <header class="windowhead">
                      Documento exemplo
                      <img class="X" src="https://banner2.kisspng.com/20180402/dwe/kisspng-computer-icons-social-media-email-webmail-cancel-button-5ac240963875f3.0504665115226799582313.jpg" alt="X" onclick="closeMain()">
                      <button id="share">share</button>
                      <button id="back">back</button>
                  </header>
                  <div id="corpo">
                      <h4>Este é um exemplo de Documento</h4>
                  </div>
              </div>-->
                </div>
            </div>

最佳答案

只是为了说明使用 jQuery's draggable() 的基本功能, 检查这个 demo on jsFiddle .

这是您需要允许拖动一个带有子元素的 JS,子元素可以单独拖放:

$('#doc-container').droppable({
  activeClass: "ui-state-default",
  hoverClass: "ui-state-hover",
  cursor: 'move',
  greedy: true,
  drop: function(event, ui) {
    console.log(ui);
    $(ui.draggable).removeClass("out-of-box").addClass("in-box");
    ui.draggable.detach().appendTo($(this));
  }
});

$('#larger-drop-target').droppable({
  activeClass: "ui-state-default",
  hoverClass: "ui-state-hover",
  cursor: 'move',
  drop: function(event, ui) {
    $(ui.draggable).removeClass("in-box").addClass("out-of-box");
    ui.draggable.detach().appendTo($(this));
  }
});

$("#doc-container, .draggable").draggable({});

我的示例使用以下 HTML 布局:

<div id="larger-drop-target">
  <div id="doc-container" class="ui-widget-header droppable">
    <header class="windowTop">
      <h1 id="docsHeadTexto">Documents</h1>
    </header>

    <div id="draggable" style="left:0;" class="draggable ui-widget-content">
      Doc Example
    </div>
  </div>
</div>

和 CSS 代码:

#larger-drop-target {
  width: 100%;
  height: 100%;
}

.draggable {
  width: 80px;
  height: 80px;
  padding: 5px;
  position: absolute;
}

#doc-container {
  height: 200px;
}

.in-box {
  background: #FEE;
}

.out-of-box {
  background: #EFE;
}

一些解释性的话:

  • droppable() 定义了那些可以接受要被拖动到的元素的元素。在示例中,我无法使用 HTML“body”元素,因为这在 fiddle 中不起作用,所以我决定将 #doc-container 包装在更大的容器中 #larger-drop -目标.detach().attachTo() 确保元素的父元素被移除并分配给当前放置目标,否则即使他们被丢在外面了。
  • draggable() 定义那些可以拖动的元素。
  • 应用于拖放元素的类仅用于演示目的;放在 #doc-container 上的元素显示为红色,放在外面的元素显示为绿色。
  • 拖动元素的 position: absolute 是必要的,否则只要移动一个元素,元素就会不断移动。如果您的容器中需要多个"file",请为每个文件指定合适的 left: X00px; 样式

更新 1:

您可以在每个元素离开父容器时立即克隆每个元素,并在将其拖回时使用类检查和 .clone()/.remove() 将其删除:

$('#doc-container').droppable({
  activeClass: "ui-state-default",
  hoverClass: "ui-state-hover",
  cursor: 'move',
  greedy: true,
  drop: function(event, ui) {
    if (ui.draggable.hasClass('out-of-box')) {
      ui.draggable.remove();
    } else {
      $(ui.draggable).removeClass("out-of-box").addClass("in-box");
      ui.draggable.detach().appendTo($(this));
    }

  }
});

$('#larger-drop-target').droppable({
  activeClass: "ui-state-default",
  hoverClass: "ui-state-hover",
  cursor: 'move',
  drop: function(event, ui) {
    if (ui.draggable.hasClass("in-box")) {
      var clone = ui.draggable.clone();
      clone.removeClass("in-box").addClass("out-of-box");
      clone.detach().appendTo($(this)).draggable({});
      ui.draggable.draggable({
        revert: true
      });
    }
  }
});

这是一个Fiddle显示更改后的版本。

关于javascript - 如何从可拖动元素制作可拖动元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56127692/

相关文章:

javascript - JS React 网站最好的无限滚动工具/组件?

javascript - 复选框唯一 ID 与样式脚本冲突

javascript - div 中的图像 - 褪色边缘

javascript - 如何衡量 jQuery 动画与 CSS3 转换的性能?

javascript - E2E Playwright (JS, TS) : how can i wait a loader disappear when have more than 1 element on page? 需要等待所有元素

javascript - 在 Node-JS 中应用回调设计模式

javascript - 将下拉列表中的值添加到 html 属性

html - 在 Bootstrap 4 中编写自定义 SCSS 的位置

css - 如何删除 JavaFX 8 TableView 中的条纹行

javascript - 使用扩展运算符更新包含对象的数组