如何用可拖动物品制作可拖动物品?

时间:2019-05-14 09:56:42

标签: 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>

1 个答案:

答案 0 :(得分:1)

仅为了说明使用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”元素,因为这在摆弄中不起作用,因此我决定将#doc-container包装在较大的容器#larger-drop-target中。 .detach().attachTo()确保已删除元素的父元素并将其分配给当前放置目标,否则子元素即使已被放置在其父元素之外,也仍将与其父元素一起移动。
  • draggable()定义了可以拖动的元素。
  • 应用于放置的元素的类仅用于演示目的;放置在#doc-container上的元素显示为红色,放置在position: absolute之外的元素显示为绿色。
  • 所拖动元素的left: X00px;是必要的,因为否则每移动一个元素,元素就会不断地移动。如果您的容器中需要多个“文档”,请为每个文档提供合适的.clone()样式

更新1:

您可以在每个元素离开父容器后对其进行克隆,并在拖回时使用类检查和.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 }); } } }); 将其删除:

{{1}}

这是Fiddle,显示更改的版本。