javascript - 检测容器,同时在其上移动元素

标签 javascript drag-and-drop draggable

我制作了一个简单的拖放界面。我有一堆容器(“包装器”)和其中一个动态添加的项目(“dragElement”)。所以我需要,当我将项目移到另一个容器上时,JS 会检测到它并在拖动完成后将项目移到那里。

我尝试在拖动项目时使用“onmouseover”和“mouseup”检测容器,但没有成功,因为实际上,鼠标总是在被拖动的元素上。 那么如何在拖动项目时检测容器?请使用纯 JS...

document.onmousedown = function(e) {

    var dragElement = e.target;

    if (!dragElement.classList.contains('draggable')) return;

    var coords, shiftX, shiftY, detectPage;

    startDrag(e.clientX, e.clientY);

    document.onmousemove = function(e) {
        moveAt(e.clientX, e.clientY);
    };

    wrapper.onmouseover = function(e) {
        detectPage = e.target;
        console.log(detectPage);
    };

    dragElement.onmouseup = function() {
        finishDrag();
    };

    function startDrag(clientX, clientY) {

        shiftX = clientX - dragElement.getBoundingClientRect().left;
        shiftY = clientY - dragElement.getBoundingClientRect().top;

        dragElement.style.position = 'fixed';

        document.body.appendChild(dragElement);

        moveAt(clientX, clientY);

    };

    function finishDrag() {

        dragElement.style.top = parseInt(dragElement.style.top) - wrapper.getBoundingClientRect().top + 'px';
        dragElement.style.position = 'absolute';

        wrapper.onmouseup = function(e) {
            var selectPage = e.target;
        }

        wrapper.appendChild(dragElement);

        document.onmousemove = null;
        dragElement.onmouseup = null;

    };

    function moveAt(clientX, clientY) {
        var newX = clientX - shiftX;
        var newY = clientY - shiftY;

        if (newX < 0) newX = 0;
        if (newX > wrapper.offsetWidth - dragElement.offsetWidth) {
            newX = wrapper.offsetWidth - dragElement.offsetWidth;
        }

        dragElement.style.left = newX + 'px';
        dragElement.style.top = newY + 'px';
    };

    return false;
};

最佳答案

好吧,没人帮忙。所以花了一天的时间来寻找解决方案。我所能找到的只是从 dragElement.onmouseup 中删除函数 finishDrag() 并将其更改为以下代码。

如果简而言之,当 onmouseup 出现时,dragElement 必须转到 display:none 现在我们可以访问附近的对象鼠标光标通过 elementFromPoint。完成后,我们可以轻松检测容器,将元素带回 display:block 并将其放入该容器...

希望对某人有帮助...

    dragElement.onmouseup = function(e) {

        dragElement.style.display = 'none';

        var selectPage = document.elementFromPoint(e.clientX, e.clientY);

        dragElement.style.display = 'block';

        dragElement.style.top = parseInt(dragElement.style.top) - selectPage.getBoundingClientRect().top + 'px';
        dragElement.style.position = 'absolute';

        selectPage.appendChild(dragElement);

        document.onmousemove = null;
        dragElement.onmouseup = null;

    };

关于javascript - 检测容器,同时在其上移动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625574/

相关文章:

javascript - Dragend 客户端在 Safari 和 Firefox 上的协调不连贯

jquery - 如果在此 div 之外且在其他可拖动项内部,则可拖动恢复(同时使用无效和有效恢复选项)

javascript - 新动态创建的 div 无法拖动

javascript - 拖动一个矩形并附加到相应的组

javascript - 无法在 shopify 库中获得可拖放的工作

javascript - 将 Interact.js 与 Angular 项目集成

javascript - 如何使用 jQuery 验证插件以编程方式检查表单是否有效

javascript - 使我的网页反色

javascript - 如何使用页面 URL 提取多个产品页面的元标记?

javascript - 两个 div 并排占据整个容器区域并处理调整大小事件