我尝试将项目拖放到页面上,但我没有移动项目本身,而是创建了项目的副本。
这是我的代码。 “copyDragDrop”是页面底部的一个div。它保持为空,直到用户拖动某些东西为止。
function coordSouris()
{
return {
x:event.clientX + document.body.scrollLeft - document.body.clientLeft,
y:event.clientY + document.body.scrollTop - document.body.clientTop
};
}
function drag()
{
var pos = coordSouris(event);
copie = event.srcElement.cloneNode(true);
document.getElementById('copieDragDrop').appendChild(copie);
copie.style.position = 'absolute';
copie.style.display = 'block';
document.onmousemove = mouseMove;
document.onmouseup = drop;
}
function mouseMove()
{
if (copie != null)
{
var pos = coordSouris(event);
copie.style.left = pos.x;
copie.style.top = pos.y;
}
}
function drop()
{
var divCopie = document.getElementById('copieDragDrop');
if (divCopie.hasChildNodes() )
{
while ( divCopie.childNodes.length >= 1 )
{
divCopie.removeChild(divCopie.firstChild);
}
}
}
此代码创建副本,开始移动它,但几个像素后,副本停止跟随鼠标。如果我释放鼠标,“onmouseup”不会被触发,但副本再次开始跟随鼠标!我已经在多个项目上尝试过该代码,但出现了相同的错误
我什么都不懂,非常欢迎任何帮助。
更新:我刚刚意识到我尝试代码的所有元素都有一些共同点:它们包含或被包含在 ASP.net 超链接控件中。相同的代码适用于常规 HTML 元素。肯定有一些自动生成的 JavaScript 链接会干扰我的代码。
最佳答案
找不到导致该问题的自动生成的代码,因此我只是通过用标准 HTML anchor 替换超链接控件来解决此问题。
关于javascript - 复制、拖放错误 : item stops moving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547434/