Building `Drag-and-Drop` DIVs: Polishing the Look and Feel - Adding elements on the fly: building DIV elements with the DOM
(Page 2 of 5 )
Making dragging DIVs look like regular windows requires performing some fine-tuning tasks, in order to control the appearance of their borders and create a realistic beveled effect. Inspired by an excellent script written by Scott Andrew, I’ll build a window-like DIV element by utilizing three different boxes, which when appropriately positioned, will look similar to a conventional window.
Before I begin creating the corresponding DIVs, a brief note is in order here: the approach that I’ll take uses the DOM and some presentational JavaScript for creating the pertinent DIVs. However, you can go through an alternative approximation and use regular CSS styles and (X)HTML markup, in order to build window-like dragging DIVs. That said, I’ll first define a simple JavaScript function, which utilizes the DOM for creating a DIV element. Below is the corresponding signature:
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;
}
As you can see, the function shown above takes a few input parameters, in order to build a DIV element, and adds some styles to it, specifying its width, height, top-level coordinates and background color. Finally, the function returns the styled DIV to the calling code. Indeed, this function is actually simple to understand.
At this point, you might be wondering…what’s the reason for writing this function? As I said before, the complete window-like dragging DIV will be comprised of three DIV elements, so I’ll use this function for constructing each one of them.
Having defined the “DOMDiv()” function, let’s move on and look at another one, “DOMWindow()”, which, as you’ll see in short, is responsible for creating the three DIV elements mentioned before, as well as for adding the proper styles to each of them, in order give the whole structure the look and feel of a regular window.
Next: Nesting and styling DIV elements: defining the “DOMWindow()” function >>
More JavaScript Articles
More By Alejandro Gervasio