Home arrow JavaScript arrow Page 4 - Building `Drag-and-Drop` DIVs: Polishing the Look and Feel
JAVASCRIPT

Building `Drag-and-Drop` DIVs: Polishing the Look and Feel


This is the last tutorial of the series “Building drag-and-drop DIVs.” Welcome back. In three parts, this series introduces the key points for creating dragging DIV elements, for quick inclusion in web pages, emulating the behavior of many user interfaces found in current desktop applications.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
December 21, 2005
TABLE OF CONTENTS:
  1. · Building `Drag-and-Drop` DIVs: Polishing the Look and Feel
  2. · Adding elements on the fly: building DIV elements with the DOM
  3. · Nesting and styling DIV elements: defining the “DOMWindow()” function
  4. · Turning DOM-based DIVS into dragging elements: using the X library
  5. · Putting the pieces together: listing the full code for DOM-based dragging DIVs

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials