JS 拖放到目标而不知道其位置

发布于 2024-11-02 12:27:15 字数 1657 浏览 0 评论 0原文

我正在创建一个简单的应用程序,它允许将多个 img 拖动到页面上的目标 div。当 img 被拖动并在目标 div 上释放时,img 的属性会被记录下来。


不幸的是,我的网页是基于百分比构建的,我想知道是否有任何方法可以在不知道目标区域的顶部、左侧等位置的情况下完成此操作。换句话说,不要绝对化。

我已经让拖动部分工作正常,它只是模拟我发现很棘手的检测。

我认为我已经非常接近使用目标 div 上的 mouseover 事件,但当然拖动 img 会阻止事件触发。当我释放图像时,它会重置到正常位置,并给出短暂的时间让鼠标悬停看起来合适。

然而在测试中,这个时间间隔似乎没有给鼠标悬停足够的时间来触发(如果可能的话)。我相信是这种情况,因为我在拖动的图像停止阻挡目标 div 后立即发出警报,并且 img 的属性已成功打印到我的控制台日志中。


img.style.position = "relative";
img.style.left = "0px";
img.style.top = "0px";

var startDrag = function(e){
    initialMouseX = e.clientX;
    initialMouseY = e.clientY;
    initialElementX = initialElementY = "0px";
    img.style.zIndex = 1000;
    document.addEventListener("mouseup", removeDrag, true);
    document.addEventListener("mousemove",movement,true);
    document.getElementById("drag-area").addEventListener("mouseover",
    readPeriod,true); //mouseover event starts when the user presses the img
};

var removeDrag = function(e){
    document.removeEventListener("mousemove",movement,true);
    img.style.left="0px";
    img.style.top="0px";
    img.style.zIndex = 0;
    //an alert at this point gives the mouseup time to fire
    img.removeEventListener("mouseup", removeDrag, true);
        //mouseover ends after the img has stopped blocking the mouse
    };

var movement = function(e){
    /*Code which moves the dragging img by style.left/top*/
};

var readPeriod = function(e){
    console.log(img.name); //Point of simulated on target div
};

img.addEventListener("mousedown",startDrag,true);

所以总而言之,我试图模拟掉落到目标上的图像,因为不可能通过目标 div 在屏幕上的位置来推断它。
(不幸的是,在这种情况下我无法使用 js 库来帮助我。)

I am creating a simple app which allows for the dragging of multiple img's to a target div on a page. When the img is dragged and release on the target div the img's properties are noted.


Unfortunately my web page is built on percentages and I am wondering if there is anyway way this can be done without knowing the top, left etc positions of the target area. In other words, without working with absolutes.

I've gotten the dragging part working fine it's just simulating the detection which I am finding tricky.

I think I've gotten pretty close using the mouseover event on the target div but of course the dragging img blocks the event firing. When I release my image it resets to its normal position and gives a brief time for the mouseover to seem suitable.

However in tests it would seem that this time gap doesn't give the mouseover enough time to fire (if that is even possible). I believe this is the case because I put an alert right after the dragged image stops blocking the target div and the img's properties are succesfully printed to my console log.


img.style.position = "relative";
img.style.left = "0px";
img.style.top = "0px";

var startDrag = function(e){
    initialMouseX = e.clientX;
    initialMouseY = e.clientY;
    initialElementX = initialElementY = "0px";
    img.style.zIndex = 1000;
    document.addEventListener("mouseup", removeDrag, true);
    document.addEventListener("mousemove",movement,true);
    document.getElementById("drag-area").addEventListener("mouseover",
    readPeriod,true); //mouseover event starts when the user presses the img
};

var removeDrag = function(e){
    document.removeEventListener("mousemove",movement,true);
    img.style.left="0px";
    img.style.top="0px";
    img.style.zIndex = 0;
    //an alert at this point gives the mouseup time to fire
    img.removeEventListener("mouseup", removeDrag, true);
        //mouseover ends after the img has stopped blocking the mouse
    };

var movement = function(e){
    /*Code which moves the dragging img by style.left/top*/
};

var readPeriod = function(e){
    console.log(img.name); //Point of simulated on target div
};

img.addEventListener("mousedown",startDrag,true);

So in summary I am trying to simulate the image dropping onto the target since its impossible to deduce the target div by its location on screen.
(Unfortunately I am unable to use a js library to aid me in this situation.)

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

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

发布评论

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

评论(1

秋风の叶未落 2024-11-09 12:27:15

您可能应该使用 jQuery UI 的 Droppable。事情会简单得多,您不需要解决这样的问题。
人们投入了大量的时间来使其正常工作。您不应该再次浪费时间并再次解决所有这些错误。
最重要的是,您可以通过 ID 标记放置目标,而无需处理坐标

You should probably use jQuery UI's Droppable. Things would be much simpler and you won't need to solved problems like this.
People invested many hours in making this work correctly. You shouldn't waste your time again and solve all these bugs again.
On top of that, you would be able to mark the drop target by ID and won't need to deal with coordinates

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