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

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 / 15
December 21, 2005
  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

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">
<script type="text/javascript" src="path-to-
<script type="text/javascript" src="path-to-
<script type="text/javascript" src="path-to-
<script type="text/javascript">
// create div element
function DOMDiv(x,y,w,h,col){
 var div=document.createElement('div');
   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.style.border='2px outset #aaa';
 // create window title bar
 var titleBar=new DOMDiv(4,4,w-14,18,'#00c');
 titleBar.style.font='bold 10pt Arial';
 // create window 'content area'
 var contentArea=new DOMDiv(4,26,w-10,h-40,'#fff');
 contentArea.style.border='1px inset #ccc';
 contentArea.style.padding='0px 2px 0px 4px';
 contentArea.style.font='normal 10pt Arial';
 // append title to title bar
 // append text to content area
 // append title bar
 // append content area
 // append window to document tree
// create 'domdiv' element and assign events
function initDomDiv(){
 var domdiv=xGetElementById('domdiv');
function domdivOnDragStart(){}
// move 'domdiv' element
function domdivOnDrag(ele,mdx,mdy){
// display drag-and-drop div when page is loaded
  // 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

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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


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