JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - Turning DOM-based DIVS into dragging elements: using the X library


    (Page 4 of 5 )

    You may remember that in the previous tutorial I used the functionality of Michael Foster’s X library, for quickly creating dragging DIVs. Don’t worry if this doesn’t ring any bells for you. It’s something that I glossed over when I first explained the advantages of utilizing this powerful package. In the simplest sense, this library allowed me turning regular DIVs into true dragging elements, with only a few simple JavaScript functions. This came in handy for my purposes, since I didn’t have to deal directly with complex cross-browser DHTML routines.

    It’s precisely for that reason that I’ll use this package for converting the brand new DOM-based DIVs into dragging boxes. Here’s an example of how you can create these kind of elements:

    <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">
    // DOMDiv() & DOMWindow() functions go here
    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 dragging 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.');
      // turn 'domdiv' into a dragging element
      initDomDiv();
     }
    }

    As shown above, first I’ve included the corresponding source files “x_core.js”, “x_events.js” and “x_drag.js” from the X library, required to perform the dragging process on the DOM-based DIV element, and finally defined the relevant “initDomDiv()”,”domdivOnDragStart()” and “domdivOnDrag()” functions, which are responsible for moving the DIV around the web page whenever the mouse is dragged on it.

    Also, as you can appreciate, the “domdivOnDragStart()” function is in fact doing nothing (its definition is empty), because I haven’t specified a particular task to be performed at the start of the dragging process. You may want to change this and define this function to have it do something more useful.

    Finally, after the page has been loaded, the script above creates a new DOM-based DIV element, and executes the “initDomDiv()” function, which, as you know, turns this element into a drag-and-drop box.

    So far, the script that you just saw is now capable of building a window-like DIV, which is also a dragging element. I think this structure might be something really useful to use on web-based interfaces, aimed at emulating the behavior of desktop applications. Quite good, right?

    Now that you know how to create dragging DIV elements to be included in your web pages, in the last section of this tutorial, I’ll show you the complete source code for the dragging DIV script, so you can copy it and use it in your own JavaScript applications.

    More JavaScript Articles
    More By Alejandro Gervasio


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