SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Killer Drag Drop
By Scott Isaacs

This code adds Drag-Drop to any absolutely positioned element on your page without requiring any extra scripting. Nested positioned elements can even be dragged independently.

Rearrange this page! Click on this element and drag and it will move. For a cool demo, move this block of text, then move the Drag Drop code box. You will notice that both the text and the code section below move together, or the code can be dragged independently.

You can control the layering so elements can go behind or on top of other elements. For a clear example, place this text and then the code block on top of the yellow heading.

This code is generic and requires no modification to use on your web-page. If you want to be able to drag an element, position it with absolute position and supply the custom attribute "dragEnabled". For example:

  <DIV STYLE="positon: absolute; cursor: hand; top: 60; left: 20" dragEnabled>
   Elements defined this way can be dragged.
  </DIV>

Drag Drop Code

  // Copyright 1997-1998 insideDHTML.com, LLC.
  // For more information, see www.insideDHTML.com
  // This code can be reused as long as the copyright notice is not removed.

  // drag-drop code
  // This code allows any absolutely positioned element to be dragged.

  var elDragged = null  // Track current item.

  function doMouseMove() {
    // Check if mouse button is down and if an element is being dragged
    if ((1 == event.button) && (elDragged != null)) {
      // Move the element
      // Where the mouse is in the document
      var intTop = event.clientY+document.body.scrollTop; 
      var intLeft = event.clientX + document.body.scrollLeft;
      // Calculate what element the mouse is really in
      var intLessTop  = 0; var intLessLeft = 0;
      var elCurrent = elDragged.offsetParent;
      while (elCurrent.offsetParent!=null) {
        intLessTop+=elCurrent.offsetTop;
        intLessLeft+=elCurrent.offsetLeft;
        elCurrent = elCurrent.offsetParent;
      }
      // Set new position
      elDragged.style.pixelTop = intTop  - intLessTop - elDragged.y;
      elDragged.style.pixelLeft = intLeft - intLessLeft  - elDragged.x;
      event.returnValue = false;
    };
  };

  function checkDrag(elCheck) {
    // Check if the clicked inside an element that supports dragging
    while (elCheck!=null) {
      if (null!=elCheck.getAttribute("dragEnabled")) 
        return elCheck
      elCheck = elCheck.parentElement
    }      
    return null
  }

  function doMouseDown() {
    // Store element on mousedown.  Called from click handler in code below .
    // All elements that have a "dragEnabled" attribute and are positioned
    // can be dragged.
    var elCurrent = checkDrag(event.srcElement)
    if (null!=elCurrent) {
        elDragged = elCurrent;
        // Determine where the mouse is in the element
        elDragged.x = event.offsetX
        elDragged.y = event.offsetY
        var op = event.srcElement
        // Find real location in respect to element being dragged.
        if ((elDragged!=op.offsetParent) && (elDragged!=event.srcElement)) {
          while (op!=elDragged) {
            elDragged.x+=op.offsetLeft
            elDragged.y+=op.offsetTop
            op=op.offsetParent
          }
        }
    }
  }

  function doSelectStart() {
    // Don't start text selections in dragged elements.
    return (null==checkDrag(event.srcElement) && (elDragged!=null))
  }
  // Process mousemove.
  document.onmousedown = doMouseDown;
  document.onmousemove = doMouseMove;
  // Reset element on mouseup.
  document.onmouseup = new Function("elDragged=null");
  document.onselectstart = doSelectStart;
Discuss and Rate this Article