gpt4 book ai didi

javascript - 在 HTML5 的原生拖放中克隆阴影

转载 作者:行者123 更新时间:2023-11-28 00:04:26 25 4
gpt4 key购买 nike

您好,我在 HTML5 中遇到原生拖放问题。

在我的站点中,我打开了类似窗口弹出窗口的东西,我想让用户更改该窗口的位置。到目前为止,我使用了自己的代码,工作完美,但现在是时候将其重写为原生 HTML5。

在这里你可以找到我现在使用的代码:http://jsfiddle.net/aapbf/6/

var box = document.getElementById('box');
function startDrag(e){
this.mouse = {x: e.clientX,y: e.clientY};
this.actual = {x: this.style.left, y: this.style.top};
}

function nowDrag(e){
this.style.left = this.actual.x+e.clientX-this.actual.x+'px';
this.style.top = this.actual.y+e.clientY-this.actual.y+'px';
this.textContent = e.clientX;
}
function handleDrop(){
this.style.left = this.actual.x+e.clientX-this.actual.x+'px';
this.style.top = this.actual.y+e.clientY-this.actual.y+'px';
}
box.addEventListener('drag',nowDrag,false);
box.addEventListener('dragstart',startDrag,false);
box.addEventListener('drop', handleDrop, false);

如何制作的想法很好(更改 css 属性)?如何避免创建拖动对象的影子克隆?或者如何使阴影克隆对象成为对象的相同克隆,这样我就不需要更改 css 属性来创建移动效果,这甚至可能吗?这将是更好的选择(从优化 Angular 来看),因为阴影已经跟踪光标,这是我需要的。如果你第一次拖动它一切正常,但在第二次尝试时没有任何反应,我不明白为什么。

编辑:

可能是我写的看不懂。我想在这个网站上产生这样的效果:

https://web.archive.org/web/20151230131916/https://developer.cdn.mozilla.net/media/uploads/demos/D/a/Darbicus/067780cbcb0cfea29e59def1e1acbc3c/jigsaw-puzzle_1376833848_demo_package/index.html

我预测你会写检查源代码,我做到了。我发现了什么?我讨厌在互联网上的东西。即使我在 DRAG&DROP 部分找到了这个示例,也没有任何内容,他们使用自己的代码。这个例子在那里做什么,我不知道。总结一下:主要问题是影子克隆拖动对象。

最佳答案

您可以在 dragstart 处理程序中明确设置拖动(或“重影”)图像:

function startDrag(e){
this.mouse = {x: e.clientX,y: e.clientY};
this.actual = {x: this.style.left, y: this.style.top};
e.dataTransfer.setDragImage(this, 0, 0); // set the drag image to be the element itself
}

这是一些 documentation on the DataTransfer object .

关于javascript - 在 HTML5 的原生拖放中克隆阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283543/

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