I've been trying to develop my own version of these draggable
layers and I have been limiting myself to IE6...for now. I have
looked at some other examples to get ideas of creating an alternative
to pop-up windows in a web page. The code I have works (sort of).
The problem is that I can move these layers around when I move the
mouse slowly, however, if I move it fast the web browser looses track
of the motion and leaves the layer behind. If anyone has some ideas
or suggestions it would be much appreciated.
TIA
Here's my code:
=============== ========TEST2.H TM============= ===============
<HTML>
<HEAD>
</HEAD>
<script language="JavaS cript" src="DivLayer.j s"></script>
<script language="JavaS cript" src="DateTime.j s"></script>
<!-- BEGIN FLOATING LAYER CODE //-->
<div id="Test1" style="position :absolute;width :250px;left:50; top:50;visibili ty:hidden">
<!-- External table (i.e. the border) -->
<table border=0 width=250 bgcolor=Navy cellspacing=0
cellpadding=5>
<tr>
<td width=100%>
<table border=0 width=100% cellspacing=0 cellpadding=0
height=36>
<tr>
<!-- Title Bar -->
<td id="titleBar" style="cursor:m ove"
width="100%"
onMouseDown="di vTest1.MouseDow n();"
onMouseMove="di vTest1.MouseMov e();"
onMouseUp="divT est1.MouseUp(); "
onMouseOut="div Test1.MouseOut( );">
<font face=Arial color=#FFFFFF>< B><I>Test
1</I></B></font>
</td>
<!-- The X in the upper right-hand corner to
close -->
<td id="closeX" style="cursor:h and" valign="top"
onClick="divTes t1.ChangeState( 'hidden');">
<font color=#ffffff size=2 face=arial>
<B>X</B>
</font>
</td>
</tr>
<tr>
<!-- Window Contents -->
<td id="contents" width="100%" bgcolor="#FFFFF F"
style="padding: 4px" colspan="2">
<!-- PLACE YOUR CONTENT HERE -->
This is where your content goes.<br>
It can be any html code or text.<br>
<input type="checkbox" name="myBox"
value="blah">Bl ah<BR>
<!-- END OF CONTENT AREA -->
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div id="Test2" style="position :absolute;width :400;left:100;t op:100;visibili ty:hidden">
<!-- Title Bar -->
<div id="titleBar"
style="cursor:m ove;padding:1;p osition:absolut e;height:15;wid th:385;left:0;t op:0;background-color:Navy"
onMouseDown="di vTest2.MouseDow n();"
onMouseMove="di vTest2.MouseMov e();"
onMouseUp="divT est2.MouseUp(); "
onMouseOut="div Test2.MouseOut( );">
<font color=#ffffff size=1 face=arial>
<B><I>Test 2</I></B>
</font>
</div>
<!-- The X in the upper right-hand corner to close -->
<div id="closeX"
style="cursor:h and;padding:1;p osition:absolut e;height:15;wid th:15;left:385; top:0;backgroun d-color:Navy"
onClick="divTes t2.ChangeState( 'hidden');">
<font color=#ffffff size=1 face=arial >
<CENTER><B>X</B></CENTER>
</font>
</div>
<!-- Window Contents -->
<div id="contents"
style="position :absolute;width :400;left:0;top :15;padding:2;b ackground-color:White;bor der-width:0
1 1 1;border-color:black;bor der-style:solid">
<!-- PLACE YOUR CONTENT HERE -->
<select name="lst_Conta ctList" size="1">
<option>DEFAULT </option>
<option>NOBOD Y</option>
<option>SOMEBOD Y</option>
<option>EVERYBO DY</option>
<option>ANYBODY </option>
</select>
<BR>
Blah! Blah!<BR>
<!-- END OF CONTENT AREA -->
</div>
</div>
<div id="Test3" style="position :absolute;width :400;left:150;t op:125;visibili ty:hidden">
<!-- Title Bar -->
<div id="titleBar"
style="cursor:m ove;padding:1;p osition:absolut e;height:18;wid th:382;left:0;t op:0;background-color:Navy"
onMouseDown="di vTest3.MouseDow n();"
onMouseMove="di vTest3.MouseMov e();"
onMouseUp="divT est3.MouseUp(); "
onMouseOut="div Test3.MouseOut( );">
<font color=#ffffff size=2 face=arial>
<B><I>Test 3</I></B>
</font>
</div>
<!-- The X in the upper right-hand corner to close -->
<div id="closeX"
style="cursor:h and;padding:1;p osition:absolut e;height:18;wid th:18;left:382; top:0;backgroun d-color:Navy"
onClick="divTes t3.ChangeState( 'hidden');">
<font color=#ffffff size=2 face=arial style="font-weight:bold"
<center>X</center>
</font>
</div>
<!-- Window Contents -->
<div id="contents"
style="position :absolute;width :400;left:0;top :18;padding:2;b ackground-color:White;bor der-width:0
1 1 1;border-color:black;bor der-style:solid">
<!-- PLACE YOUR CONTENT HERE -->
Main Entry: <b>byte</b> <BR>
Function: <i>noun</i> <BR>
A group of eight binary digits processed as a unit by a
computer and used especially to
represent an alphanumeric character.<br>
<!-- END OF CONTENT AREA -->
</div>
</div>
<div id="Test4" style="position :absolute;width :400;left:200;t op:150;visibili ty:hidden">
<!-- Title Bar -->
<div id="titleBar"
style="cursor:m ove;padding:1;p osition:absolut e;height:20;wid th:380;left:0;t op:0;background-color:Navy"
onMouseDown="di vTest4.MouseDow n();"
onMouseMove="di vTest4.MouseMov e();"
onMouseUp="divT est4.MouseUp(); "
onMouseOut="div Test4.MouseOut( );">
<font color=#ffffff size=3 face=arial>
<B><I>Test 4</I></B>
</font>
</div>
<!-- The X in the upper right-hand corner to close -->
<div id="closeX"
style="cursor:h and;padding:1;p osition:absolut e;height:20;wid th:20;left:380; top:0;backgroun d-color:Navy"
onClick="divTes t4.ChangeState( 'hidden');">
<font color=#ffffff size=3 face=arial style="font-weight:bold" <center>X</center>
</font>
</div>
<!-- Window Contents -->
<div id="contents"
style="position :absolute;width :400;left:0;top :20;padding:2;b ackground-color:White;bor der-width:0
1 1 1;border-color:black;bor der-style:solid">
<!-- PLACE YOUR CONTENT HERE -->
Main Entry: <b>byte me</b><BR>
Function: <i>intransiti ve verb</i> <BR>
An expression used by peaved computers (i.e. the host) where
the current line of queries by other
computers (i.e. the client) leads to annoying the host
computer (e.g. network server).<br>
<!-- END OF CONTENT AREA -->
</div>
</div>
<div id="DateTime" style="position :absolute;width :450;left:200;t op:200;visibili ty:hidden">
<!-- Title Bar -->
<div id="titleBar"
style="cursor:m ove;padding:1;p osition:absolut e;height:18;wid th:432;left:0;t op:0;background-color:Navy"
onMouseDown="di vDateTime.Mouse Down();"
onMouseMove="di vDateTime.Mouse Move();"
onMouseUp="divD ateTime.MouseUp ();"
onMouseOut="div DateTime.MouseO ut();">
<font color=#ffffff size=2 face=arial>
<B><I>Pick Time & Date</I></B>
</font>
</div>
<!-- The X in the upper right-hand corner to close -->
<div id="closeX"
style="cursor:h and;padding:1;p osition:absolut e;height:18;wid th:18;left:432; top:0;backgroun d-color:Navy"
onClick="divDat eTime.ChangeSta te('hidden');">
<font color=#ffffff size=2 face=arial style="font-weight:bold" <center>X</center>
</font>
</div>
<!-- Window Contents -->
<div id="contents"
style="position :absolute;width :450;left:0;top :18;padding:2;b ackground-color:White;bor der-width:0
1 1 1;border-color:black;bor der-style:solid">
<!-- PLACE YOUR CONTENT HERE -->
<center>
<form name='frm_DateT imeSelect'>
<table border='0'>
<tr>
<td width='50%'>
<p align='center'>
<select name='lst_Month ' size='1'></select>
<select name='lst_Date' size='1'></select>
/
<select name='lst_Year' size='1'></select>
</p>
</td>
<td width='25%'>
<p align='center'>
<select name='lst_Hour' size='1'></select>
:
<select name='lst_Minut e' size='1'></select>
</p>
</td>
<td width='25%'>
<p align='center'>
<input type='radio' checked name='chk_AMPM'
value='AM'>AM
<input type='radio' name='chk_AMPM' value='PM'>PM
</p>
</td>
</tr>
<tr>
<td colspan='3'>
<p align='center'>
<input type='button' name='btn_OK' value=' OK
' onClick="divDat eTime.ChangeSta te('hidden');sa veDateTime('frm _DateTimeSelect ');">
</p>
</td>
</tr>
</table>
</form>
</center>
<!-- END OF CONTENT AREA -->
</div>
</div>
<div id="Date" style="position :absolute;width :350;left:150;t op:225;visibili ty:hidden">
<!-- Title Bar -->
<div id="titleBar"
style="cursor:m ove;padding:1;p osition:absolut e;height:18;wid th:332;left:0;t op:0;background-color:Navy"
onMouseDown="di vDate.MouseDown ();"
onMouseMove="di vDate.MouseMove ();"
onMouseUp="divD ate.MouseUp();"
onMouseOut="div Date.MouseOut() ;">
<font color=#ffffff size=2 face=arial>
<B><I>Pick Date</I></B>
</font>
</div>
<!-- The X in the upper right-hand corner to close -->
<div id="closeX"
style="cursor:h and;padding:1;p osition:absolut e;height:18;wid th:18;left:332; top:0;backgroun d-color:Navy"
onClick="divDat e.ChangeState(' hidden');">
<font color=#ffffff size=2 face=arial style="font-weight:bold"
<center>X</center>
</font>
</div>
<!-- Window Contents -->
<div id="contents"
style="position :absolute;width :350;left:0;top :18;padding:2;b ackground-color:White;bor der-width:0
1 1 1;border-color:black;bor der-style:solid">
<!-- PLACE YOUR CONTENT HERE -->
<center>
<form name='frm_DateS elect'>
<table border='0'>
<tr>
<td width='50%'>
<p align='center'>
<select name='lst_Month ' size='1'></select>
<select name='lst_Date' size='1'></select>
/
<select name='lst_Year' size='1'></select>
</p>
</td>
</tr>
<tr>
<td colspan='3'>
<p align='center'>
<input type='button' name='btn_OK' value=' OK
' onClick="divDat e.ChangeState(' hidden');saveDa te('frm_DateSel ect');">
</p>
</td>
</tr>
</table>
</form>
</center>
<!-- END OF CONTENT AREA -->
</div>
</div>
<!-- END FLOATING LAYER CODE -->
<script language="JavaS cript">
<!--
//Initialize the objects
divTest1 = new DivLayer('Test1 ');
divTest2 = new DivLayer('Test2 ');
divTest3 = new DivLayer('Test3 ');
divTest4 = new DivLayer('Test4 ');
divDateTime = new DivLayer('DateT ime');
divDate = new DivLayer('Date' );
function ArrayElements()
{
for(i = 0; i < document.all.le ngth; i++)
{
top.ifrm_test.d ocument.writeln ("Item: "+ i +"<BR>");
top.ifrm_test.d ocument.writeln ("name: "+ document.all[i].name
+"<BR>");
top.ifrm_test.d ocument.writeln ("id: "+ document.all[i].id
+"<BR>");
top.ifrm_test.d ocument.writeln ("className: "+
document.all[i].className +"<BR>");
top.ifrm_test.d ocument.writeln ("title: "+
document.all[i].title +"<BR>");
top.ifrm_test.d ocument.writeln ("style: "+
document.all[i].style +"<BR>");
top.ifrm_test.d ocument.writeln ("tagName: "+
document.all[i].tagName +"<BR>");
top.ifrm_test.d ocument.writeln ("srcElement : "+
document.all[i].srcElement +"<BR>");
top.ifrm_test.d ocument.writeln ("parentElement : "+
document.all[i].parentElement +"<BR>");
top.ifrm_test.d ocument.writeln ("<BR>");
top.ifrm_test.d ocument.writeln ("<BR>");
}
}
//-->
</script>
<BODY>
<a href="javascrip t:divTest1.Chan geState('visibl e');">Show Test 1
Window</a><BR>
<a href="javascrip t:divTest1.Chan geState('hidden ');">Hide Test 1
Window</a><BR>
<BR>
<a href="javascrip t:divTest2.Chan geState('visibl e');">Show Test 2
Window</a><BR>
<a href="javascrip t:divTest2.Chan geState('hidden ');">Hide Test 2
Window</a><BR>
<BR>
<a href="javascrip t:divTest3.Chan geState('visibl e');">Show Test 3
Window</a><BR>
<a href="javascrip t:divTest3.Chan geState('hidden ');">Hide Test 3
Window</a><BR>
<BR>
<a href="javascrip t:divTest4.Chan geState('visibl e');">Show Test 4
Window</a><BR>
<a href="javascrip t:divTest4.Chan geState('hidden ');">Hide Test 4
Window</a><BR>
<form name="frm_Test" >
<p><a href="javascrip t:divDateTime.C hangeState('vis ible');populate DateTimeLists(' frm_DateTimeSel ect');setDateOu tput('frm_Test. txt_Test1');">E nter
Time & Date: </a>
<input type="text" name="txt_Test1 " size=30></p><BR>
<p><a href="javascrip t:divDate.Chang eState('visible ');populateDate Lists('frm_Date Select');setDat eOutput('frm_Te st.txt_Test2'); ">Enter
Date: </a>
<input type="text" name="txt_Test2 " size=30></p><BR>
</form>
<BR>
<BR>
<BR>
<BR>
<BR>
<a href="javascrip t:ArrayElements ()">HTML Elements</a><BR>
<BR>
<iframe name="ifrm_test "></iframe>
</BODY>
</HTML>
=============== =======DIVLAYER .JS============ ==============
<!--
//Constructor for DivLayer object
function DivLayer(objNam e)
{
this.obj = eval("document. all." + objName); //div layer object
name
this.DivDragEna bled = false; //Boolean
this.zIndex = this.obj.style. zIndex; //zIndex property
this.ChangeStat e = DivLayer_change State; //Methods
this.ResetZInde x = DivLayer_resetZ Index;
this.OnTop = DivLayer_onTop;
this.MouseDown = DivLayer_mouseD own;
this.MouseMove = DivLayer_mouseM ove;
this.MouseUp = DivLayer_mouseU p;
this.MouseOut = DivLayer_mouseO ut;
}
function DivLayer_resetZ Index()
{
this.obj.style. zIndex = 0;
}
function DivLayer_change State(state)
{
this.obj.style. visibility = state;
this.ResetZInde x();
}
function DivLayer_onTop( )
{
max = 0;
for(i = 0; i < document.all.le ngth; i++)
{
if (document.all[i].tagName == "DIV")
max = (max < document.all[i].style.zIndex) ?
document.all[i].style.zIndex : max;
}
max++;
this.obj.style. zIndex = max;
}
function DivLayer_mouseD own(e)
{
//object which generated event
Src = event.srcElemen t;
//not titleBar object then re-assign srcElement = parentElement
(i.e. do nothing)
while (Src.id != "titleBar" && Src.tagName!= "BODY")
{
Src = Src.parentEleme nt;
}
//selected object is titleBar then do the move!
if (Src.id == "titleBar")
{
offsetX = event.clientX;
offsetY = event.clientY;
nowX = parseInt(this.o bj.style.left);
nowY = parseInt(this.o bj.style.top);
this.OnTop(); //to bring
DivLayer on top of others
this.DivDragEna bled = true; //to enable
dragging
}
}
function DivLayer_mouseM ove(e)
{
if (!this.DivDragE nabled) return;
this.obj.style. left = nowX + event.clientX - offsetX;
this.obj.style. top = nowY + event.clientY - offsetY;
}
function DivLayer_mouseU p(e)
{
this.DivDragEna bled = false;
}
function DivLayer_mouseO ut(e)
{
this.DivDragEna bled = false;
}
//-->
=============== =======DATETIME .JS============ ==============
<!--
//global variable
oDateOutput = null;
function populateDateTim eLists(myForm)
{
//fill in values for drop-down lists.
var frm = eval("document. "+ myForm);
var today = new Date(); //get time right now!!
//fill in year
var k = 0;
var min = today.getFullYe ar() - 5;
for(i = 0; i < 10; i++)
{//year array for -5/+5 years around current year.
k = min + i;
frm.lst_Year.op tions[i] = new Option(k, k);
}
frm.lst_Year.op tions[5].selected = true;
//fill in month
var Mnth = new Array('January' , 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September', 'October', 'November',
'December');
for(i = 0; i < Mnth.length; i++)
{
frm.lst_Month.o ptions[i] = new Option(Mnth[i], i);
}
frm.lst_Month.o ptions[today.getMonth( )].selected = true;
//fill in Day of Month
for(i = 0; i < 31; i++)
{
frm.lst_Date.op tions[i] = new Option(i + 1, i + 1);
}
frm.lst_Date.op tions[today.getDate() - 1].selected = true;
//fill in hour
for(i = 0; i < 12; i++)
{
frm.lst_Hour.op tions[i] = new Option(i + 1, i + 1);
}
k = parseInt(today. getHours());
if (k == 0)
{
frm.chk_AMPM[0].checked = true; //12 AM
frm.lst_Hour.op tions[11].selected = true;
}
if ((k >= 1) && (k <= 11))
{
frm.chk_AMPM[0].checked = true; //morning
frm.lst_Hour.op tions[k - 1].selected = true;
}
if (k == 12)
{
frm.chk_AMPM[1].checked = true; //12 PM
frm.lst_Hour.op tions[k - 1].selected = true;
}
if ((k >= 13) && (k <= 23))
{
frm.chk_AMPM[1].checked = true; //PM
frm.lst_Hour.op tions[k - 13].selected = true;
}
//fill in minutes
var Min = new Array(today.get Minutes(), 0, 5, 10, 15, 20, 25, 30,
35, 40, 45, 50, 55);
for(i = 0; i < Min.length; i++)
{
frm.lst_Minute. options[i] = new Option(Min[i], Min[i]);
}
frm.lst_Minute. options[0].selected = true;
}
function populateDateLis ts(myForm)
{
//fill in values for drop-down lists.
var frm = eval("document. "+ myForm);
var today = new Date(); //get time right now!!
//fill in year
var k = 0;
var min = today.getFullYe ar() - 5;
//fill in year
for(i = 0; i < 10; i++)
{//year array for -5/+5 years around current year.
k = min + i;
frm.lst_Year.op tions[i] = new Option(k, k);
}
frm.lst_Year.op tions[5].selected = true;
//fill in month
var Mnth = new Array('January' , 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September', 'October', 'November',
'December');
for(i = 0; i < Mnth.length; i++)
{
frm.lst_Month.o ptions[i] = new Option(Mnth[i], i);
}
frm.lst_Month.o ptions[today.getMonth( )].selected = true;
//fill in Day of Month
for(i = 0; i < 31; i++)
{
frm.lst_Date.op tions[i] = new Option(i + 1, i + 1);
}
frm.lst_Date.op tions[today.getDate() - 1].selected = true;
}
function getDaysOfMonth( m, y)
{//use quotes for case statements...
switch(m)
{//month = 0 to 11
case '0': //January
return 31;
case '1': //Feb
if ((y % 4) == 0)
{
if ((y % 100) == 0 && (y % 400) != 0)
{
return 28;
}
return 29;
}
else
{
return 28;
}
case '2': //March
return 31;
case '3': //April
return 30;
case '4': //May
return 31;
case '5': //June
return 30;
case '6': //July
return 31;
case '7': //August
return 31;
case '8': //September
return 30;
case '9': //October
return 31;
case '10': //Nobember
return 30;
case '11': //December
return 31;
default: //invalid month
return 99;
}
}
function checkDoM(myForm )
{//check Day of Month
var frm = eval("document. "+ myForm);
var m = frm.lst_Month.v alue;
var y = frm.lst_Year.va lue;
var max = getDaysOfMonth( m, y);
//Re-set selected date to proper last day-of-month if beyond the
maximum.
if (frm.lst_Date.v alue > max)
frm.lst_Date.op tions[max - 1].selected = true;
}
function setDateOutput(o bjName)
{
oDateOutput = eval("document. "+ objName);
}
function saveDateTime(my Form)
{
var frm = eval("document. "+ myForm);
checkDoM(myForm );
var tmp = parseInt(frm.ls t_Month.value) + 1;
tmp = tmp.toString();
if (tmp.length == 1)
{//pad with a zero
tmp = '0' + tmp;
}
var selectedDate = tmp + '/';
tmp = frm.lst_Date.va lue;
if (tmp.length == 1)
{//pad with a zero
tmp = '0' + tmp;
}
selectedDate = selectedDate + tmp + '/' + frm.lst_Year.va lue + '
';
tmp = frm.lst_Hour.va lue;
selectedDate = selectedDate + tmp + ':';
tmp = frm.lst_Minute. value;
if (tmp.length == 1)
{//pad with a zero
tmp = '0' + tmp;
}
if(frm.chk_AMPM[0].checked == true)
selectedDate = selectedDate + tmp + ':00 ' +
frm.chk_AMPM[0].value;
else
selectedDate = selectedDate + tmp + ':00 ' +
frm.chk_AMPM[1].value;
//save to output
oDateOutput.val ue = selectedDate;
}
function saveDate(myForm )
{
var frm = eval("document. "+ myForm);
checkDoM(myForm );
var tmp = parseInt(frm.ls t_Month.value) + 1;
tmp = tmp.toString();
if (tmp.length == 1)
{
tmp = '0' + tmp;
}
var selectedDate = tmp + '/';
tmp = frm.lst_Date.va lue;
if (tmp.length == 1)
{
tmp = '0' + tmp;
}
selectedDate = selectedDate + tmp + '/' + frm.lst_Year.va lue;
//save to output
oDateOutput.val ue = selectedDate;
}
//-->