css 拖动带边框的项目后边框仍然存在

i2loujxw  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(152)

下面是可拖动项目的列表。悬停时显示虚线边框。如果项目被拖走,其边框将显示在取代其位置的项目周围。我希望在拖动项目后隐藏边框。

function listItemDragged(e) {
    e.target.classList.add('dragging');
    let dropTarget =
        document.elementFromPoint(e.clientX, e.clientY) === null
            ? e.target
            : document.elementFromPoint(e.clientX, e.clientY);

    if (e.target.parentNode === dropTarget.parentNode) {
        dropTarget =
            dropTarget !== e.target.nextSibling
                ? dropTarget
                : dropTarget.nextSibling;
        e.target.parentNode.insertBefore(e.target, dropTarget);
    }
}

function listItemDropped(e) {
    e.target.classList.remove('dragging');
}

function onLoad() {
    let listItems = document.querySelectorAll('.draggable');
    Array.prototype.map.call(listItems, (option) => {
        option.ondrag = listItemDragged;
        option.ondragend = listItemDropped;
    });
}
onLoad();
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Roboto', sans-serif
}

body {
    background-color: #2b3035;
}

.draggable {
    display: flex;
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 5px;
    border: 1px solid #5c636a;
    margin-right: 5px;
    background-color: #212529;
    cursor: grab;
    color: #ffffff;
    touch-action: none
}

.draggable:hover {
    border: 1px dashed #ff0000;
}

.dragging {
    cursor: grabbing;
    background: transparent;
    color: transparent;
    border: none !important;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<ul class='sortable-list list-unstyled'>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 1
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 2
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 3
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 4
</li>
</ul>
v8wbuo2f

v8wbuo2f1#

您可以使用以下CSS来禁用拖动任何可拖动项目时的悬停效果。

ul:not(:has(.dragging)) .draggable:hover {
  border: 1px dashed #ff0000;
}
function listItemDragged(e) {
    e.target.classList.add('dragging');
    let dropTarget =
        document.elementFromPoint(e.clientX, e.clientY) === null
            ? e.target
            : document.elementFromPoint(e.clientX, e.clientY);

    if (e.target.parentNode === dropTarget.parentNode) {
        dropTarget =
            dropTarget !== e.target.nextSibling
                ? dropTarget
                : dropTarget.nextSibling;
        e.target.parentNode.insertBefore(e.target, dropTarget);
    }
}

function listItemDropped(e) {
    e.target.classList.remove('dragging');
}

function onLoad() {
    let listItems = document.querySelectorAll('.draggable');
    Array.prototype.map.call(listItems, (option) => {
        option.ondrag = listItemDragged;
        option.ondragend = listItemDropped;
    });
}
onLoad();
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Roboto', sans-serif
}

body {
    background-color: #2b3035;
}

.draggable {
    display: flex;
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 5px;
    border: 1px solid #5c636a;
    margin-right: 5px;
    background-color: #212529;
    cursor: grab;
    color: #ffffff;
    touch-action: none
}

ul:not(:has(.dragging)) .draggable:hover {
    border: 1px dashed #ff0000;
}

.dragging {
    cursor: grabbing;
    background: transparent;
    color: transparent;
    border: none !important;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<ul class='sortable-list list-unstyled'>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 1
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 2
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 3
</li>
<li class='draggable' draggable='true'>
    Lorem ipsum dolor sit amet 4
</li>
</ul>

相关问题