Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
香草JavaScript拖放-克隆元素_Javascript_Html_Css_Drag And Drop_Dom Events - Fatal编程技术网

香草JavaScript拖放-克隆元素

香草JavaScript拖放-克隆元素,javascript,html,css,drag-and-drop,dom-events,Javascript,Html,Css,Drag And Drop,Dom Events,如何编辑此Javascript代码以拖放元素并在原始容器中保留副本 ... containers.forEach(container => { container.addEventListener('dragover', e => { e.preventDefault() const afterElement = getDragAfterElement(container, e.clientY) const draggable

如何编辑此Javascript代码以拖放元素并在原始容器中保留副本

...

containers.forEach(container => {
    container.addEventListener('dragover', e => {
        e.preventDefault()
        const afterElement = getDragAfterElement(container, e.clientY)
        const draggable = document.querySelector('.dragging')
        if (afterElement == null) {
            container.appendChild(draggable)
        } else {
            container.insertBefore(draggable, afterElement)
        }
    })
})

function getDragAfterElement(container, y) {
    const draggableElements = [...container.querySelectorAll('.draggable:not(.dragging)')]

    return draggableElements.reduce((closest, child) => {
        const box = child.getBoundingClientRect()
        const offset = y - box.top - box.height / 2
        if (offset < 0 && offset > closest.offset) {
            return { offset: offset, element: child }
        } else {
            return closest
        }
    }, { offset: Number.NEGATIVE_INFINITY }).element
}
。。。
containers.forEach(container=>{
container.addEventListener('dragover',e=>{
e、 预防默认值()
const afterElement=getDragAfterElement(容器,e.clientY)
const draggable=document.querySelector(“.draggable”)
if(afterElement==null){
container.appendChild(可拖动)
}否则{
容器。插入前(可拖动,后元素)
}
})
})
函数getDragAfterElement(容器,y){
const draggableElements=[…container.queryselectoral('.draggable:not(.draggable)]
返回draggableElements.reduce((最近,子项)=>{
const box=child.getBoundingClientRect()
常数偏移=y-box.top-box.height/2
if(偏移量<0&&offset>最近的.offset){
返回{offset:offset,元素:child}
}否则{
返回最近的
}
},{offset:Number.NEGATIVE_INFINITY}).element
}

一切正常,我只需要留下一份被删除元素的副本。

由于此功能,它无法工作:

    function getDragAfterElement(container, y) {
    const draggableElements = [...container.querySelectorAll('.draggable:not(.dragging)')]

    return draggableElements.reduce((closest, child) => {
        const box = child.getBoundingClientRect()
        const offset = y - box.top - box.height / 2
        if (offset < 0 && offset > closest.offset) {
            return { offset: offset, element: child }
        } else {
            return closest
        }
    }, { offset: Number.NEGATIVE_INFINITY }).element
}
函数getDragAfterElement(容器,y){ const draggableElements=[…container.queryselectoral('.draggable:not(.draggable)] 返回draggableElements.reduce((最近,子项)=>{ const box=child.getBoundingClientRect() 常数偏移=y-box.top-box.height/2 if(偏移量<0&&offset>最近的.offset){ 返回{offset:offset,元素:child} }否则{ 返回最近的 } },{offset:Number.NEGATIVE_INFINITY}).element } 尝试其他方法。

请解释为什么“由于此功能,它无法工作”,并建议一种可能的替代方法,而不是“尝试其他方法”。否则,这不是一个有效的答案。