如何从可拖动项目制作可拖动项目?

瑞费尔南多

我有一个可拖动的窗口,我想使该窗口必须是独立的并且可以拖动的文档,并且在放置时将元素复制到拖动的位置并使其也留在窗口中。基本上,当您应该将文档拖出窗口到屏幕上的任何位置时,当您停止拖动时,文档将被复制,您在窗口上有一个文档,在屏幕上有一个文档。屏幕上的那个不能复制,但可以拖动。我该怎么做?

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>

萨沙M78

只是为了说明使用jQuery 的 draggable()的基本功能,请在 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;样式

更新 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
      });
    }
  }
});

这是一个显示更改版本小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使<select>标签项目可拖动?

来自分类Dev

使用jQuery可拖动UI使项目可拖动

来自分类Dev

如何从可拖动元素中删除项目符号?

来自分类Dev

如何从可拖动元素中删除项目符号?

来自分类Dev

如何在ajax调用过滤后使项目可拖动

来自分类Dev

如何仅在移动当前项目后加载下一个可拖动项目?

来自分类Dev

如何使纸张可拖动

来自分类Dev

如何将项目一直拖动到以前的位置,直到在可拖动的Vue中释放

来自分类Dev

拖动后如何使可拖动元素复制?

来自分类Dev

拖动后如何使可拖动元素复制?

来自分类Dev

拖动后如何使可拖动元素复制?

来自分类Dev

拖动后如何使可拖动元素复制?

来自分类Dev

使QTableView中的项目可拖动但不可选择

来自分类Dev

使用边距还原可拖动项目的位置

来自分类Dev

如何在可拖动的画布上制作矩形?

来自分类Dev

如何制作自定义可拖动脚本

来自分类Dev

如何使用mouseup / move制作可拖动对象?

来自分类Dev

如何在多个项目上应用可拖动的jQuery UI函数?

来自分类Dev

拖动具有滚动条的容器时,可拖动项目不可见

来自分类Dev

如何使弹出div可拖动

来自分类Dev

如何使传单circleMarker可拖动?

来自分类Dev

如何限制可拖动距离?

来自分类Dev

制作一些可拖动的UIView

来自分类Dev

制作一些可拖动的UIView

来自分类Dev

使用JavaScript制作可拖动的JSXGraph

来自分类Dev

单击可拖动的引发拖动

来自分类Dev

SortableJS-无法将项目从可拖动和可排序的范围中排除

来自分类Dev

可拖动和可排序项目编号更改时更新

来自分类Dev

如何使嵌套元素在可拖动容器中不可拖动?

Related 相关文章

热门标签

归档