gpt4 book ai didi

javascript - 拖放 - 拖动一个已经放下的项目

转载 作者:行者123 更新时间:2023-11-30 21:09:42 27 4
gpt4 key购买 nike

早上好,我不能在 jscript 中执行此拖放操作。拿起元素并将其放入“conteudo”div 后,我无法再次拿起它(在 conteudo div 内)并自由移动它,因为它返回错误,有人可以帮助我吗?

enter image description here

enter image description here

      //Drag'n Drop functions
function allowDrop(ev)
{
ev.preventDefault();
}

function drag(ev)
{
ev.dataTransfer.setData("text", ev.target.id);
ev.dataTransfer.effectAllowed = "copy";
}

function drop(ev)
{
ev.preventDefault();
var x = ev.clientX;
var y = ev.clientY;
var data = ev.dataTransfer.getData("text");
var copyimg = document.createElement("img");
var original = document.getElementById(data);
copyimg.src = original.src;
ev.target.appendChild(copyimg);
copyimg.setAttribute("style", "position: fixed; top: "+(y-50)+"px; left:"+(x-50)+"px;");
copyimg.setAttribute('draggable', true);
}
    #conteudo{
width:500px;
height:500px;
float:left;
background-color:#ff1;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
}
<html>
<body>
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<div id="conteudo" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>
</body>
</html>

最佳答案

var elementCounter = 0; // to assign ids to dropped element.
//Drag'n Drop functions
function allowDrop(ev)
{
ev.preventDefault();
}

function drag(ev)
{
ev.dataTransfer.setData("text", ev.target.id);
ev.dataTransfer.effectAllowed = "copy";
}

function drop(ev)
{
ev.preventDefault();
var x = ev.clientX;
var y = ev.clientY;
var data = ev.dataTransfer.getData("text");
var copyimg = document.createElement("img");
var original = document.getElementById(data);
copyimg.src = original.src;
ev.target.appendChild(copyimg);
if(original.parentNode.id == "conteudo"){
original.parentNode.removeChild(original);
}
copyimg.id = "dropped_elem"+(++elementCounter);
copyimg.setAttribute("style", "position: fixed; top: "+(y-50)+"px; left:"+(x-50)+"px;");
copyimg.setAttribute('draggable', true);
copyimg.setAttribute('ondragstart', "drag(event)");
}
#conteudo{
width:500px;
height:500px;
float:left;
background-color:#ff1;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
}
<html>
<body>
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<img id="drag1" width="70" src="https://orig00.deviantart.net/0772/f/2015/291/2/9/29820d36256689bdeae12f344e4f0b7a-d9djrwh.gif" draggable="true" ondragstart="drag(event)" alt="" />
<div id="conteudo" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>
</body>
</html>

我在您的代码中添加了以下内容:-

  1. copyimg.id = "dropped_elem"+(++elementCounter); :- 将 id 分配给放置的元素。
  2. copyimg.setAttribute('ondragstart', "drag(event)"); :- 以便可以再次拖动该元素。
  3. elementCounter :- 用于将 id 分配给删除的元素的变量。

关于javascript - 拖放 - 拖动一个已经放下的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46279037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com