JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - Moving DIVs around the web document: defining the “dragDiv()” function


    (Page 4 of 5 )

    Having explained how the “startDrag()” function calculates the X,Y coordinates for moving a DIV element, I’ll first show how the “dragDiv()” function looks, and next explain what it does. Here’s the source code for this function:

    function dragDiv(e){
     if(!drag){return};
     if(!e){var e=window.event};
     var targ=e.target?e.target:e.srcElement;
     // move div element
       targ.style.left=coordX+e.clientX-offsetX+'px';
       targ.style.top=coordY+e.clientY-offsetY+'px';
       return false;
    }

    Here, the function actually performs a extremely simple task. As illustrated above, as long as the user moves the mouse over the selected DIV, its coordinates are continuously updated by the following lines:

    targ.style.left=coordX+e.clientX-offsetX+'px';
    targ.style.top=coordY+e.clientY-offsetY+'px';

    However, it seems that I’ve skipped over the first statements of the function. In fact, I haven’t. The line below:

    if(!drag){return};

    checks to see if the “drag” flag variable is either true or false. The first case means the user has started dragging the mouse on the DIV element, while the second one means the user has stopped dragging on it. As you may have realized, this flag variable provides us with a very convenient and simple method for determining when to update the top-left coordinates of the corresponding DIV, in response to the mouse event triggered by the user.

    Finally, the lines of code responsible for getting the triggered event, as well as the source DIV element, really look redundant. Well, there is a valid justification for doing this. Internet Explorer is sometimes pretty slow for tracking some mouse events, particularly if they’re triggered over and over again at short time intervals. By including these lines, I make sure that most browsers will properly track the fired event, and determine correctly the corresponding source element.

    After having defined the logic for both the “startDrag()” and “dragDiv()” functions, I’m pretty sure you’ve guessed how the “stopDrag()” function works. Haven’t you yet? All right, let me give you a clue. Since this function is called up for a “mouseup” event, all it has to do is set the “drag” flag to false, in this way indicating that the user has released the mouse’ s left button. Considering this, here’s the signature for this function:

    function stopDrag(){
     drag=false;
    }

    That’s it. If you feel inclined to code the function above as one-liner, go ahead. Here, for the sake of clarity I’ve used a multi-line definition.

    Now that I’ve demonstrated how each JavaScript function does its thing to achieve a realistic “drag-and-drop” effect on DIVs, it’s time to put all the pieces together and assemble the complete DIV dragging script. Keep reading to see how each function fits into the complete JavaScript application.

    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 4 hosted by Hostway
    Stay green...Green IT