为什么我的自定义拖放脚本失败?

发布于 2024-07-09 00:30:24 字数 602 浏览 7 评论 0原文

我目前正在尝试编写自己的 JS 拖放脚本(出于纯粹的好奇和无聊,我知道使用框架会容易得多)。 我的目标是一个完全可用的 Firefox3 版本,IE 现在可以等待。

我刚刚遇到了一个奇怪的错误。 当我第一次拖动div时,它工作正常。 当我第二次拖动它时,释放按钮后它不会粘住,我必须再次单击才能将其放下。 第三次和随后的拖动再次完美地工作(!?!)。

请参阅[原始页面][1](正如我所说,目前仅使用 FireFox)了解发生的情况。 整个事情是作为一个 div 完成的,有两个事件(onmousedown 和 onmouseup),使用 document.captureEvents(Event.MOUSEMOVE) 进行中间移动。 该脚本可以在[这里][2]找到(忽略底部的ajax部分,它是为一些额外的技巧准备的,如果我把它拿出来,错误仍然存​​在)。

如果您过去遇到过类似的情况或者您在某个地方发现了错误,请告诉我。 我知道可能有更好的方法来解决整个问题,但我正在专门寻找一种方法来使我的方法发挥作用。

编辑:Chrome 和 Safari 可以工作。

编辑:使链接离线,在新版本上工作。

I am currently trying to code my own JS drag and drop script (out of sheer curiosity and boredom, I know it would be much easier with a framework). My aim is a fully working Firefox3 version , IE can wait for now.

I just got stuck on a weird bug. When I drag the div for the first time, it works ok. When I drag it for the second time, it does not stick after releasing the button and I have to click once more to get it down. Third and consequent drags work flawlessly again (!?!).

Please see [the original page][1] (as I said, FireFox only for now) for an idea of what happens. The whole thing is done as a div with two events (onmousedown and onmouseup) using document.captureEvents(Event.MOUSEMOVE) for the intermediate movement. The script can be found [here][2] (disregard the bottom ajax part, it is prepared for some additional tricks and the bug stays if I take it out).

Please let me know if you have encountered something similar in the past or if you see a mistake somewhere. I know there may be better ways to go around the whole thing but I am specifically looking for a way to make my approach work.

EDIT: Chrome and Safari work.

EDIT: Taking the links offline, working on new version.

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

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

发布评论

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

评论(1

旧情勿念 2024-07-16 00:30:24

好吧,首先,如果你想问的话,这对我来说在 FF3 中是有效的。

这不会是您想听到的,但我强烈建议您从 mootools 或 jquery 或类似工具中选择 DnD 方法。 仅从效率的角度来看,DnD 编码是一件可怕的事情(我自己也做过几次),如果你没有能力(这里没有冒犯的意思)解决出现的大量错误,那将是一个巨大的错误。与仅使用现成的强大成熟的实施相比,会浪费您的时间。 这是一件很难做到的事情。

如果您继续使用自己的代码(作为练习或出于自豪 - 我可以理解:))这种问题通常是由于某些其他事件而未在您认为的位置捕获事件的结果首先,一个标志没有设置在您认为应该设置的位置,或者(或因为)一个错误在意外的点打破了您的代码。 尝试通过注销事件触发器来逻辑地跟踪正在发生的情况。

如果您可以更详细地定义它如何不起作用,我也许可以进一步追踪它(因为我似乎无法复制),但我确实建议您探索可靠库的好处。

Well first up this works for me in FF3 if that's what you're asking.

This isn't going to be what you want to hear, but I strongly recommend you pick up a DnD method from mootools or jquery or similar. Just from an efficiency standpoint, DnD is a horrible thing to code up (done it a few times myself) and if you're not capable (no offence meant here) of resolving the numerous bugs that come up it's just going to be a huge drain of your time compared to just going with a robust mature implementation off the shelf. It is a hard thing to do.

If you do what to continue with your own code (as an exercise or out of pride - I can appreciate that :) ) this kind of problem is typically the result of either an event not being captured where you think it is because some other event got in the way first, a flag not being set where you think it is, or (or because of) an error which breaks out of your code at an unexpected point. Try and trace logically what's happening by logging out the event triggers.

If you could define how it wasn't working in more detail I might be able to trace it further (since I seemingly can't replicate), but I do suggest you explore the benefits of a solid library.

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