Horje

Tips (Total 8)


# Tips-1) What is HTML Drag and Drop API

In HTML, any element can be dragged and dropped.

It is meant that You can move the element from one place to another place.

HTML Drag and Drop interfaces enable applications to use drag-and-drop features in browsers.

The user may select draggable elements with a mouse, drag those elements to a droppable element, and drop them by releasing the mouse button. A translucent representation of the draggable elements follows the pointer during the drag operation.

You can customize which elements can become draggable, the type of feedback the draggable elements produce, and the droppable elements.

This overview of HTML Drag and Drop includes a description of the interfaces, basic steps to add drag-and-drop support to an application, and an interoperability summary of the interfaces.


# Tips-2) What browsers will support HTML Drag and Drop

Drag and drop is a very common feature. It is when you "grab" an object and drag it to a different location.

The numbers in the table specify the first browser version that fully supports Drag and Drop.


# Tips-3) How to create HTML Drag and Drop Example

The example below is a simple drag and drop example:

Example of HTML Drag and Drop Example

The example below is a simple drag and drop example: Drag the Horje Profile image into the rectangle:
index.html
Example: HTML
<style>
#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
</style>
<script>
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
</script>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="https://horje.com/avatar.png" draggable="true" ondragstart="drag(event)" width="336" height="69">

Output should be:

Example of HTML Drag and Drop Example

# Tips-4) How to Make an Element Draggable

First of all: To make an element draggable, set the draggable attribute to true:

Example of Making an Element Draggable

index.html
Example: HTML
<img draggable="true">

Output should be:

Example of Making an Element Draggable

# Tips-5) What to Drag - ondragstart and setData()

Then, specify what should happen when the element is dragged.

In the example above, the ondragstart attribute calls a function, drag(event), that specifies what data to be dragged.

The dataTransfer.setData() method sets the data type and the value of the dragged data:

Example of ondragstart and setData()

In this case, the data type is "text" and the value is the id of the draggable element ("drag1").
index.html
Example: HTML
<script>
function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
} 
</script>

Output should be:

Example of ondragstart and setData()

# Tips-6) Where to Drop - ondragover

The ondragover event specifies where the dragged data can be dropped.

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.

This is done by calling the event.preventDefault() method for the ondragover event:

Example of Drop - ondragover

This is done by calling the event.preventDefault() method for the ondragover event:
index.html
Example: HTML
<script>
 event.preventDefault()
</script>

Output should be:

Example of Drop - ondragover

# Tips-7) How to Do the Drop - ondrop

When the dragged data is dropped, a drop event occurs.

In the example above, the ondrop attribute calls a function, drop(event):

Example of Doing the Drop - ondrop

index.html
Example: HTML
<script>
function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
</script>

Output should be:

Example of Doing the Drop - ondrop

Code explained: (Doing the Drop - ondrop)


# Tips-8) How to drag and drop an image from left to right

Here is an example to drag drop an image from left to right

Example of drag and drop an image from left to right

Follow the Example
index.html
Example: HTML
<style>
#div1, #div2 {
  float: left;
  width: 100px;
  height: 35px;
  margin: 10px;
  padding: 10px;
  border: 1px solid black;
}
</style>
<script>
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
</script>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img src="https://horje.com/avatar.png" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
</div>

<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

Output should be:

Example of drag and drop an image from left to right