镀铬拖动越野车?可以使用getData()读取DataTransfer项目

发布于 2025-01-21 18:15:04 字数 1180 浏览 0 评论 0 原文

看看这个小提琴: https://jsfiddle.net/zanpx9mw/1/1/1/

代码:

const draggable_element = document.getElementById("draggable_element");

draggable_element.addEventListener("dragstart", (e) => {
  const rect = e.srcElement.getBoundingClientRect();
  const dt = e.dataTransfer;
  dt.setData("dx", `${Math.round(e.clientX - rect.x)}`);
  dt.setData("dy", `${Math.round(e.clientY - rect.y)}`);
  [...dt.items].forEach(it => {
    console.log(`dragstart ${it.type}: ${dt.getData(it.type)}`);
  });
});

draggable_element.addEventListener("dragover", (e) => {
  const dt = e.dataTransfer;
  [...dt.items].forEach(it => {
    console.log(`dragover ${it.type}: ${dt.getData(it.type)}`);
  });
});

在Firefox上,我可以将数据存储在事件 dataTransfer 对象中的 dragstart 事件中,并在 dragover 事件中读取。

但是,在Chromium/Chrome中,我可以存储数据并在 dragstart 事件中成功阅读,但不再在 dragover 事件中。奇怪的是带有预期名称的项目仍然存在,但数据似乎是空的。

但是有一个有趣的解决方法:我可以将数据保存在项目类型中,但这似乎是错误的..

这​​是已知的行为吗?我做错了吗?

Have a look at this fiddle: https://jsfiddle.net/zanpx9mw/1/

Code:

const draggable_element = document.getElementById("draggable_element");

draggable_element.addEventListener("dragstart", (e) => {
  const rect = e.srcElement.getBoundingClientRect();
  const dt = e.dataTransfer;
  dt.setData("dx", `${Math.round(e.clientX - rect.x)}`);
  dt.setData("dy", `${Math.round(e.clientY - rect.y)}`);
  [...dt.items].forEach(it => {
    console.log(`dragstart ${it.type}: ${dt.getData(it.type)}`);
  });
});

draggable_element.addEventListener("dragover", (e) => {
  const dt = e.dataTransfer;
  [...dt.items].forEach(it => {
    console.log(`dragover ${it.type}: ${dt.getData(it.type)}`);
  });
});

On Firefox I can store data inside the events dataTransfer object in the dragstart event and read it later in the dragover event.

In Chromium/Chrome however I can store the data and read it successfully inside the dragstart event but not in the dragover event anymore. Strangely items with the expected names are still present but the data seems to be empty.

There is a funny workaround though: I can just save the data inside the item type but that seems wrong..

Is this known behavior? Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2025-01-28 18:15:04

看起来Chrome支持 GetData()仅在出于安全原因下drop 事件(从

为了规避这些“安全”措施,您必须将数据写入这样的类型:

const draggable_element = document.getElementById("draggable_element");

function int_list(dt, name) {
  for(const it of dt.items) {
    if (it.type.startsWith(`${name}`))  {
        return it.type.split("_").slice(1).map(x=>parseInt(x));
    }
  }
}

draggable_element.addEventListener("dragstart", (e) => {
  const rect = e.srcElement.getBoundingClientRect();
  e.dataTransfer.setData(`pos_${Math.round(e.clientX - rect.x)}_${Math.round(e.clientY - rect.y)}`, '');
});

draggable_element.addEventListener("dragover", (e) => {
  const pos = int_list(e.dataTransfer, "pos");
  e.srcElement.style.left = `${e.x - pos[0]}px`;
  e.srcElement.style.top = `${e.y - pos[1]}px`;
});

请参见工作示例

Looks like Chrome supports getData() only on drop events for security reasons (taken from this very similar question).

To circumvent those "security" measures you would have to write the data to the type like this:

const draggable_element = document.getElementById("draggable_element");

function int_list(dt, name) {
  for(const it of dt.items) {
    if (it.type.startsWith(`${name}`))  {
        return it.type.split("_").slice(1).map(x=>parseInt(x));
    }
  }
}

draggable_element.addEventListener("dragstart", (e) => {
  const rect = e.srcElement.getBoundingClientRect();
  e.dataTransfer.setData(`pos_${Math.round(e.clientX - rect.x)}_${Math.round(e.clientY - rect.y)}`, '');
});

draggable_element.addEventListener("dragover", (e) => {
  const pos = int_list(e.dataTransfer, "pos");
  e.srcElement.style.left = `${e.x - pos[0]}px`;
  e.srcElement.style.top = `${e.y - pos[1]}px`;
});

see working example https://jsfiddle.net/g7608m1j/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文