当元素被拖放时,删除被拖动元素的克隆

发布于 2025-01-15 12:17:39 字数 2830 浏览 0 评论 0原文

在下面的代码中,有时当我将列表项拖动到列表中的新位置然后释放时,由拖动创建的元素的克隆会滑回到其原始位置,然后再转到我拖动到的位置。 此处有一个 GIF 显示了该问题。

关于如何解决这个问题有什么想法吗?

var ul = document.querySelector("ul");

function getNextElement(y) {
    var next = [...document.querySelectorAll("li")].map(element => {
        var rect = element.getBoundingClientRect();
        return {
            element: element,
            top: rect.top
        }
    }).reduce((a, b) => Math.abs(b.top - y) < Math.abs(a.top - y) ? b : a).element;
    next == null ? next = ul.firstElementChild : next;
    return next;
}

document.querySelectorAll("li").forEach(draggable => {
    draggable.draggable = true;

    draggable.ondragstart = event => ul.appendChild(document.createElement("li"));
    draggable.ondrag = event => draggable.classList.add("dragging");

    draggable.ondragend = event => {
        draggable.classList.remove("dragging")
        ul.removeChild(ul.lastElementChild)
    };

    draggable.ondragover = event => {
        event.preventDefault();
        var next = getNextElement(event.clientY);
        var dragging = document.querySelector('.dragging');
        if (dragging !== null) {
            if (next === null) {
                try {
                    ul.appendChild(dragging);
                } catch (error) {
                    console.log(dragging);
                }
            } else {
                try {
                    ul.insertBefore(dragging, next);
                } catch (error) {
                    console.log(dragging)
                }
            }
        }
    };
});
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400&display=swap');

#container {
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translate(-50%, 0%);
    width: 200px;
}

ul {
    list-style: none;
    margin: 0;
    font-size: 30px;
    font-family: "Inter";
    padding-left: 0;
    line-height: 40px;
    font-size: 16px;
}

li {
    width: 200px;
    padding-left: 15px;
    user-select: none;
    overflow: hidden;
    cursor: default;
}

li:hover {
    background-color: #f6f6f6;
    border-radius: 9px;
}

.dragging {
    color: transparent;
    border: 2px dashed #ddd;
    height: 36px;
    border-radius: 9px;
    background-color: white !important;
}
<div id="container">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
    </ul>
</div>

In my code below, sometimes when I drag a list item to a new position in the list then release, the clone of the element that is made by the drag, glides back to it's original position before going to where I dragged to. There is a GIF here that shows the issue.

Any ideas on how to fix this?

var ul = document.querySelector("ul");

function getNextElement(y) {
    var next = [...document.querySelectorAll("li")].map(element => {
        var rect = element.getBoundingClientRect();
        return {
            element: element,
            top: rect.top
        }
    }).reduce((a, b) => Math.abs(b.top - y) < Math.abs(a.top - y) ? b : a).element;
    next == null ? next = ul.firstElementChild : next;
    return next;
}

document.querySelectorAll("li").forEach(draggable => {
    draggable.draggable = true;

    draggable.ondragstart = event => ul.appendChild(document.createElement("li"));
    draggable.ondrag = event => draggable.classList.add("dragging");

    draggable.ondragend = event => {
        draggable.classList.remove("dragging")
        ul.removeChild(ul.lastElementChild)
    };

    draggable.ondragover = event => {
        event.preventDefault();
        var next = getNextElement(event.clientY);
        var dragging = document.querySelector('.dragging');
        if (dragging !== null) {
            if (next === null) {
                try {
                    ul.appendChild(dragging);
                } catch (error) {
                    console.log(dragging);
                }
            } else {
                try {
                    ul.insertBefore(dragging, next);
                } catch (error) {
                    console.log(dragging)
                }
            }
        }
    };
});
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400&display=swap');

#container {
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translate(-50%, 0%);
    width: 200px;
}

ul {
    list-style: none;
    margin: 0;
    font-size: 30px;
    font-family: "Inter";
    padding-left: 0;
    line-height: 40px;
    font-size: 16px;
}

li {
    width: 200px;
    padding-left: 15px;
    user-select: none;
    overflow: hidden;
    cursor: default;
}

li:hover {
    background-color: #f6f6f6;
    border-radius: 9px;
}

.dragging {
    color: transparent;
    border: 2px dashed #ddd;
    height: 36px;
    border-radius: 9px;
    background-color: white !important;
}
<div id="container">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
    </ul>
</div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文