JavaScript
  Home arrow JavaScript arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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: 4 stars4 stars4 stars4 stars4 stars / 8
    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 - Nesting and styling DIV elements: defining the “DOMWindow()” function


    (Page 3 of 5 )

    Having at my disposal the “DOMDiv()” function, which is tasked with building each block element that comprises the whole dragging structure, what I’ll do next is define another useful function, “DOMWindow()”, that simply will create the DIV elements corresponding to each section of the DOM-based window. So, let’s first have a look at its signature, and then see how it works:

    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);
    }

    The above function might look overly complicated, but it’s actually fairly simple. In order to understand it better, let’s dissect its code into simpler pieces and see how they work. The first fragment of code calls up the “DOMDiv()” function, in order to create the general containing DIV, which will hold the box corresponding to the window’s title bar, as well as the DIV element used as the window’s content area.

    Once this general container, which I called “winbody”, has been created, it’s properly styled by applying to it a 2px border and specifying the shape of the mouse cursor as well. The next task the function performs is creating the window’s title bar, by using a similar method to the one used to build the general containing DIV. In this case, in order to get a beveled border, the title bar is absolutely positioned by specifying a left-top offset of 4px from the position of the general containing DIV, and it’s also appropriately styled.

    Finally, the function creates the DIV that composes the window’s content area, by following the same sequence of steps performed for building up the previous DIVs, and finally inserts these elements into the structure of the web document.

    Of course, a picture is worth a thousand words, so below I’ve included a screenshot that illustrates quite accurately the final look of the DOM-based window. Take a look:

    Definitely you’ll agree that the look and feel of the window-like DIVs have been considerably improved, taking into account that the above structure has been constructed with no background images at all. It’s only three nested DIVs and pure presentational JavaScript code.

    At this stage, I think I have enough building blocks to actually start turning DOM-based DIVs into drag-and-drop elements. Bearing this in mind, let’s leap forward and see how this task is carried out. Trust me, it’s not difficult at all.

    More JavaScript Articles
    More By Alejandro Gervasio


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

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







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