4

Is there a native javascript code equivalent to jQuery's draggable? I don't need all the features but

  1. being able to drag a modal window from a handle.
  2. on start and on end events as I need to make an overlay layer while dragging to prevent the mouse being upon the iframe inside the modal window.
  3. there will be only one draggable item on the document.
6
  • html5doctor.com/native-drag-and-drop Why don't you want to use jquery? Commented Oct 17, 2011 at 1:32
  • sure, the equivalent is.. write it. or for 120k just include it to your website and go to lunch. Honestly, what is the question here? Can someone write me some great code which I already know where to find and is totally free but would rather not use? Commented Oct 17, 2011 at 1:32
  • No jQuery at all, or just no jQuery.draggable? It's going to be quite complex for full browser support with no jQuery at all; if you can use straight jQ, use .mousedown() and .mouseup() and .mousemove(). Store the start e.pageX and e.pageY values along with the starting dialog position, and use the deltas to move stuff around. Commented Oct 17, 2011 at 1:33
  • @Jere I'm injecting my script on a document I dont have any control on. It already has JQ loaded but draggable doesn't work with that version. So I tried to load JQ again and it breaks some existing scripts. Commented Oct 17, 2011 at 1:46
  • 1
    Don't you need jQuery UI in addition to jQuery for .draggable() to work (with jQuery, not native JS)? Commented Oct 17, 2011 at 2:10

4 Answers 4

2

Use the events dragstart and dragend. Tie the position of the modal relevant to the mouse's x and y coordinates.

Sign up to request clarification or add additional context in comments.

Comments

0

There is the HTML5 dnd api, but since nobody i've talked to liked it in any way and none of the older browsers support it,I would say you found a good solution in jQuery.

Comments

0

Use the HTML5 methods which seem cleaner than jQuery's:

<div draggable="true"
  ondragstart="event.dataTransfer.setData('text/plain', '12345')">
drag me
</div>

<div ondragover="return false;" 
  ondrop="this.innerHTML=event.dataTransfer.getData('text/plain')">
drop on me
</div>

Comments

-1

Although the question states native JavaScript, I'm going to respond given that @Moe Sweet commented that jQuery was enabled.

Drag and drop is a particular event chain. It's not obscenely difficult, but it can be challenging.

Simply put, drag and drop is the following steps/events:

  1. Mouse pressed down, dragging is enabled
  2. Mouse is moved, dragging continues
  3. Mouse is let up, dragging is disabled

The simple solution is to use those events: mousedown, mousemove, and mouseup

$(anElement).mousedown(foodown);

function foodown(){
  $(window).mousemove(foomove).mouseup(fooup);
  //stuff
}

function foomove(){
  //stuff
}

function fooup(){
  //stuff
  $(window).unbind('mousemove', foomove).unbind('mouseup', fooup);
}

A point that should be noticed is that the events following the mousedown need to be bound to the window so that they are caught if the mouse has left the original element while being held down.

The "stuff" part of the code is where you'll need to check the screen coords of the element vs the screen coords of the mouse and move things appropriately. Motion is only really necessary during the mousemove event as the mouse wont have moved for a mouseup or mousedown event.

There is a similar question where I posted my code for a jQuery.dragondrop plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.