JavaScript
  Home arrow JavaScript arrow Page 3 - Building `Drag-and-Drop` DIVs: Developing ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building `Drag-and-Drop` DIVs: Developing a Basic Script
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 31
    2005-12-07

    Table of Contents:
  • Building `Drag-and-Drop` DIVs: Developing a Basic Script
  • Setting up the basics: emulating drag-and-drop triggers
  • Dragging DIV elements: defining the “startDrag()” function
  • Moving DIVs around the web document: defining the “dragDiv()” function
  • Gluing the pieces together: defining the complete DIV dragging script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building `Drag-and-Drop` DIVs: Developing a Basic Script - Dragging DIV elements: defining the “startDrag()” function


    (Page 3 of 5 )

    Since I previously attached the “startDrag()” function to the “onmousedown” event handler, which was tied to the document object, I’ll define the signature for this function, so you can understand how it works. Its source code is as follows:

    function startDrag(e){
     // determine event object
     if(!e){var e=window.event};
     // determine target element
     var targ=e.target?e.target:e.srcElement;
     if(targ.className!='draggable'){return};
     // calculate event X,Y coordinates
         offsetX=e.clientX;
         offsetY=e.clientY;
     // assign default values for top and left properties
     if(!targ.style.left){targ.style.left='0px'};
     if(!targ.style.top){targ.style.top='0px'};
     // calculate integer values for top and left properties
         coordX=parseInt(targ.style.left);
         coordY=parseInt(targ.style.top);
         drag=true;
     // move div element
         document.onmousemove=dragDiv;
    }

    As listed above, the “startDrag()” function takes an event object as the only incoming parameter, and then performs a simple checking process, in order to resolve the incompatibilities between Internet Explorer and the other browsers for obtaining the event fired by the corresponding DIV element. The verifying line shown below implements a cross-browser approximation for determining which event was triggered:

    // determine event object
    if(!e){var e=window.event};

    In a similar way, the function next determines, in a cross-browser fashion, the source element that fired up the “mousedown” event, as follows:

    // determine target element
    var targ=e.target?e.target:e.srcElement;

    After the event object and the source DIV element have been appropriately tracked, the function carries out two crucial operations: first it checks whether the DIV class name is “draggable,” and then it calculates the X,Y coordinates at which the mouse was clicked on, along with the top and left position of the DIV box. The first checking process obeys a simple reason: I don’t want all the DIVs included on the web page to be dragged by the user. The script should target only those that have a “draggable” class name, therefore if the selected element doesn’t have such a CSS class, the function simply does nothing.

    Now, returning to the function flow, the calculations for assigning the new X,Y coordinates to the selected DIV element each time the user left-clicks on it, are very important in order to achieve a decent “dragging” effect.

    As you can see, the logic of the function is quite simple to understand. However, there’s one thing worth noting: notice how I use a flag variable (drag) in order to indicate that the corresponding DIV box is currently being dragged. The last task the function performs is to tie the “onmousemove” event handler to the document object and trigger the “dragDiv()” function, so the DIV element can be properly moved around the web page.

    Now that you know how the “startDrag()” functions does its business, it’s time to have a look at the “dragDiv()” function, which, as you’ll see in a moment, is responsible for moving DIV boxes around. So, let’s move on and see how this function is appropriately defined.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This first tutorial goes through the implementation of a basic script for building...
       · This is a nice intro to developing draggable interfaces. There is a bug in the demo...
       · Thank you for the kind comments on my article. Yes, there's a small bug in the...
       · I can only say "Woooooowww..."Thank you Alejandro !Ecco
       · Thank you Ecco for your kind words on this tutorial. I feel extremely safisfied of...
       · Great script !!!I don't know if you still work on this but ...I need to make...
       · Thanks for commenting on this article, as well as for the kind words about the...
       · I have messed a bit obout with it and made some changes ... it now works perfectly,...
       · I'm glad to know you got it working. And of course, your feedback is truly...
       · One of the problems with this design occurs when you start adding content to the...
       · Thank you for commenting on my JavaScript article. Also, I’d like to thank you for...
       · First of all, thanks for a great tutorial. Nice structure and to the point!In an...
       · it was a very lucid style of explanation.... thanks a lot... this helped me very...
       · Thank you for the kind words on my JavaScript article, and of course I feel glad to...
       · This is a great article for something I was needing to do on in a short time frame....
       · Thank you for the kind comments on my article. Regarding your question, you can...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT