JavaScript
  Home arrow JavaScript arrow Page 5 - Building `Drag-and-Drop` DIVs: Polishing t...
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  
Moblin 
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: Polishing the Look and Feel
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2005-12-21

    Table of Contents:
  • Building `Drag-and-Drop` DIVs: Polishing the Look and Feel
  • Adding elements on the fly: building DIV elements with the DOM
  • Nesting and styling DIV elements: defining the “DOMWindow()” function
  • Turning DOM-based DIVS into dragging elements: using the X library
  • Putting the pieces together: listing the full code for DOM-based dragging DIVs

  • 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: Polishing the Look and Feel - Putting the pieces together: listing the full code for DOM-based dragging DIVs


    (Page 5 of 5 )

    To summarize, here’s the full source code for building DOM-based dragging DIV elements:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>DRAG-AND-DROP DOM-BASED WINDOWS</title>
    <script type="text/javascript" src="path-to-
    library/x_core.js"></script>
    <script type="text/javascript" src="path-to-
    library/x_event.js"></script>
    <script type="text/javascript" src="path-to-
    library/x_drag.js"></script>
    <script type="text/javascript">
    // create div element
    function DOMDiv(x,y,w,h,col){
     var div=document.createElement('div');
       div.style.position='absolute';
       div.style.left=x+'px';
       div.style.top=y+'px';
       div.style.width=w+'px';
       div.style.height=h+'px';
       div.style.backgroundColor=col;
       div.style.padding='0';
       return div
    }
    // create DOM window
    function DOMWindow(x,y,w,h,title,content){
     // create window structure
     var winBody=new DOMDiv(x,y,w,h,'#ccc');
     // assign ID attribute
     winBody.setAttribute('id','domdiv');
     winBody.style.border='2px outset #aaa';
     winBody.style.cursor='default';
     // create window title bar
     var titleBar=new DOMDiv(4,4,w-14,18,'#00c');
     titleBar.style.font='bold 10pt Arial';
     titleBar.style.color='#fff';
     titleBar.style.paddingLeft='4px';
     // create window 'content area'
     var contentArea=new DOMDiv(4,26,w-10,h-40,'#fff');
     contentArea.style.width=document.all?(parseInt
    (contentArea.style.width)-4)+'px':(parseInt
    (contentArea.style.width)-7)+'px';
     contentArea.style.border='1px inset #ccc';
     contentArea.style.overflow='auto';
     contentArea.style.padding='0px 2px 0px 4px';
     contentArea.style.font='normal 10pt Arial';
     // append title to title bar
       titleBar.appendChild(document.createTextNode(title));
     // append text to content area
     contentArea.appendChild(document.createTextNode(content));
     // append title bar
     winBody.appendChild(titleBar);
     // append content area
       winBody.appendChild(contentArea);
     // append window to document tree
     document.getElementsByTagName('body')[0].appendChild(winBody);
    }
    // create 'domdiv' element and assign events
    function initDomDiv(){
     var domdiv=xGetElementById('domdiv');
       xMoveTo(domdiv,100,100);
       xEnableDrag(domdiv,domdivOnDragStart,domdivOnDrag);
       xShow(domdiv);
    }
    function domdivOnDragStart(){}
    // move 'domdiv' element
    function domdivOnDrag(ele,mdx,mdy){
       xMoveTo(ele,xLeft(ele)+mdx,xTop(ele)+mdy);
    }
    // display drag-and-drop div when page is loaded
    window.onload=function(){
     if(document.createElement&&document.
    getElementById&&document.getElementsByTagName){
      // display 'domdiv'
         domWin=new DOMWindow(100,100,250,200,'Drag-and-drop DOM
    Div','This dragging DIV element has been created with the DOM.');
      // make 'domdiv' a dragging element
      initDomDiv();
     }
    }
    </script>
    </head>
    <body>
    </body>
    </html>

    Bottom line

    Before I wrap up this tutorial, a final note: the example I wrote earlier uses presentational JavaScript for styling each dragging DIV element, something that was discussed at the beginning of the article. However, you may want to utilize a CSS-based approach, in order to make your design stick a little closer to Web standards. The result will be nearly identical.

    Now, it’s time to say goodbye. In this three-part series, I progressively went through the process for building drag-and-drop DIVs, ranging from writing a rather basic script to coding an advanced approach, by using the robust X library that  Michael Foster allowed me to include in the corresponding examples. Thus, you have the knowledge and the tools for spicing up your web-based user interfaces utilizing true drag-and-drop DIVs. Wait are you waiting for? Go for 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.

       · In this last part of the series, the powerful functionality of the X library is...
     

    JAVASCRIPT ARTICLES

    - 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
    - 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






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