Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple JavaScript drag and drop witout the help of a library

I am simply looking for a way of using drag and drop without jquery or any other library. If a dragged object is dropped on another element the later element should start an event (in FF - better would be browser independent).

I know that drag and drop for JavaScript was discussed sometimes before but the previous postings didn't help me.

Although I found some examples it is not clear to me if there is a "drop" or "dragdrop" events exist but these things don't work:

<p ondrop='alert("It worked");'>Text</p>
<p ondragdrop='alert("It worked");'>Text</p>

How could this be done?

Many thanks in advance.

like image 539
user107123 Avatar asked May 14 '09 15:05

user107123


People also ask

What is draggable JavaScript?

Draggable is a modular drag & drop library, allowing you to start small and build up with the features you need. At its most basic, Draggable gives you drag & drop functionality, fast DOM reordering, accessible markup, and a bundle of events to grab on to.

What is the name of the JavaScript library we use for draggable function?

GitHub - xenova/draggable: A lightweight JavaScript library for creating draggable HTML elements - using CSS Transform!


3 Answers

I agree with the other answers. A library will save you a lot of time and headache. This is coming from someone who just recently created a drag-and-drop control from scratch.

If you insist though this is what you'll need to do:

  1. Bind a onmousedown event to the div you want to drag (div.onmousedown).
  2. Change the div's position style to absolute (div.style.position = 'absolute')
  3. Begin capturing mouse movement (document.onmousemove).
  4. On mouse move update the div's position (div.style.top|left = '[location]px')
  5. On the div's onmouseup event (or the document's) unbind all the handlers and do any other cleanup (null out position changes, etc).

Some problems a library will probably solve:

  1. While dragging you will select text on the page (looks ugly).
  2. Binding to events is different between browsers.
  3. You have to calculate the size of the element being dragged if you want to show placeholders and to make it not "pop" when you begin dragging the control (since changing to absolute positioning will remove the element from flow).
  4. You will probably want your dragged element to move fluidly so you will have to store some mouse offset when selecting the element or automatically center the element to the mouse.
  5. If you want to drag an item in a list you'll have to write a ton more custom code for that list to accept the dragged item.
  6. You'll have to take into consideration dragging when the window is scrolled and possibly dragging inside other elements that are positioned strangely.
like image 74
Dave L Avatar answered Nov 05 '22 07:11

Dave L


I am simply looking for a way of using drag and drop without jquery or any other library.

I'm sorry, but there are no such thing as simply drag and drop without any library. You can write it all yourself, but that will be a lot of JS to make it work in all browsers.

like image 25
Thorbjørn Hermansen Avatar answered Nov 05 '22 08:11

Thorbjørn Hermansen


Hmm. It's probably not that simple that you'd want to do it yourself, but I would look at Peter Michaux's FORK Javascript drag and drop library -- unlike JQuery or all those big libraries, FORK's modules are decoupled from each other, and are simple enough that you could probably look at Peter's source code and figure out the bits you need. (edit: I'd just use FORK.Drag as it's really small: 7.6KB total minified)

like image 40
Jason S Avatar answered Nov 05 '22 06:11

Jason S