JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - Setting up the basics: emulating drag-and-drop triggers


    (Page 2 of 5 )

    In most cases, drag-and-drop triggering effects can be achieved by a combination of simple events, which when fired up in the appropriate sequence, emulate a real dragging behavior on the elements of a web document. Outlined through a simple step-by-step format, the overall process for simulating a drag-and-drop effect can be described by the steps below:

    1. The user left-clicks on the selected element (the event is trapped by an “onclick” event handler).
    2. The user moves the mouse over the element (the event is trapped by an “onmousemove” event handler).
    3. The user releases the mouse button on the selected element (the event is handled by the “onmouseup” event handler).

    As you can see, a basic dragging effect on most web page elements involves handling programmatically the three events listed above in the correct sequence, which simply means that whenever a “click” event is triggered by the source element, mouse movements must be tracked on and the corresponding X,Y coordinates should be assigned to the pertinent element. Finally, the dragging process should be terminated when the mouse’s left button is released.

    Even when the sequence described above is fairly easy to outline, it’s not always simple to translate into raw JavaScript code. So, let’s stay away from the underlying theory for a while, and define the appropriate event handlers that control the whole dragging process. In its simplest incarnation, drag-and-drop elements can be created with the following piece of JavaScript code:

    window.onload=function(){
     document.onmousedown=startDrag;
     document.onmouseup=stopDrag;
    }

    As shown above, the dragging process is achieved by assigning respectively the “onmousedown” and “onmouseup” event handlers to the document object, and firing up a specific function in accordance with the event triggered. In this case, to be illustrative enough, I’ve assigned the “startDrag()” function to the first event, while the “stopDrag()” function has been attached to the second event. At this point probably you’re asking yourself…where has the “onmousemove” handler been implemented? Well, if you reread the sequence of steps that I described right at the beginning of this section, then you’ll realize that this handler is encapsulated within the “startDrag()” function, which hopefully will result in a quite realistic dragging effect on the page element that originally triggered the “mousedown” event.

    Now, using highly generic code, I’ve explained how a drag-and-drop effect can be simulated on selected elements. Assuming that you’ve grasped the script’s driving logic, what I’ll do next is show you the definition of both “startDrag()” and “stopDrag()” functions, which will be attached to DIV elements. Join me into the next explanation to figure out how this is done.

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