Tartalomjegyzék

< JavaScript

Drag and Drop

A HTML

A div2 dobozt szeretnénk mozgatni.

src/index.html
<!-- ... -->
      <div id="div1" 
        ondrop="drop(event)" 
        ondragover="allowDrop(event)"></div>
      <div id="div2" 
        draggable="true" 
        ondragstart="drag(event)"></div>
<!-- ... -->

Stílus

src/style.css
#div1 {
    width: 100px;
    height: 50px;
    background-color: aqua;
}
 
#div2 {
    width: 100px;
    height: 50px;
    background-color:burlywood;
}

JavaScript

src/app.js
function drag(event) {
    event.dataTransfer.setData('actual', event.target.id)
}
 
function drop(event) {
    const data = event.dataTransfer.getData('actual')
    event.target.appendChild(document.getElementById(data))
    console.log(data)
}
 
function allowDrop(event) {
    event.preventDefault()
}

Linkek