JavaScript
  Home arrow JavaScript arrow Page 5 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 - Gluing the pieces together: defining the complete DIV dragging script


    (Page 5 of 5 )

    As I mentioned earlier, here is the complete DIV dragging script, along with some sample “drag-and-drop” DIVs:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>BASIC DRAG-AND-DROP DIVS</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script language="javascript">
    // start dragging
    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;
    }
    // continue dragging
    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;
    }
    // stop dragging
    function stopDrag(){
     drag=false;
    }
    window.onload=function(){
     document.onmousedown=startDrag;
     document.onmouseup=stopDrag;
    }
    </script>
    <style type="text/css">
    .draggable {
     position: relative;
     width: 250px;
     height: 200px;
     background-color: #ccc;
     border: 1px solid #000;
    }
    </style>
    </head>
    <body>
    <div class="draggable">

     

        This is a dragging DIV element

    </div>
    <div class="draggable">

        This is a dragging DIV element

    </div>
    </body>
    </html>

    If you wish to use the above script and tweak its code, you can either copy it or download the sample file from here (which is also available at the beginning of this article). 

    Bottom line

    In this tutorial, I’ve provided you with a basic (yet functional) DIV dragging script, which can be easily included as part of your JavaScript applications. Of course, this is only a basic example, and it’s not intended to be used in production environments.

    However, all isn’t lost. If you’re looking for a more robust and flexible method for building “drag-and-drop” DIVs, you’ll have to wait until the next article, where I’ll use a powerful, cross-browser library, in order to encapsulate most of the complexities involved in creating a DIV dragging JavaScript program. You won’t want to miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway