如何将图像拖放到特定的div

时间:2019-06-14 08:46:44

标签: javascript drag-and-drop

{{3}}处有一些代码尝试

我想做的是从页面顶部选择一个图像,并将其拖放到旁边的盒子里,盒子里有很多等待图像放到那里的盒子。这样做的方式是什么? 谢谢!

for (const image of images) {
    image.addEventListener('dragstart', dragStart);
    image.addEventListener('dragend', dragEnd);
}

for (var k = 0; k < empties.length; k++) {
    empties[k].addEventListener('dragover', dragOver);
    empties[k].addEventListener('dragenter', dragEnter);
    empties[k].addEventListener('dragleave', dragLeave);
    empties[k].addEventListener('drop', dragDrop);
}

function dragStart() {
    console.log("dragstart");
    this.className += 'fill';
    setTimeout(() => (this.className = 'invisible'), 0);
}

function dragEnd() {
    this.className = 'fill';
}

function dragOver(event) {
    event.preventDefault();
}

function dragEnter(e) {
    console.log("dragenter");
    e.preventDefault();
    this.className += 'hovered';
}

function dragLeave() {
    this.className = 'empty';
}

function dragDrop() {
    this.className = 'empty';
    this.append(images[5]);
}

0 个答案:

没有答案