Drag & Drop in Navigator 4: Bringing Drag Element to Top | WebReference

Drag & Drop in Navigator 4: Bringing Drag Element to Top


Drag & Drop in Navigator 4:
keeping the dragged element on top

We have avoided discussing the z-index property of positioned elements in our columns so far, and guess what? We're not going to discuss it this time either.

Instead, for simplicity's sake, we will create a variable activeEl, which will store the element that is currently on top of other elements. The last line in our code should be...

    activeEl = document.elDRAGFour

...if elDRAGFour hides other elements upon page load, or if it is already on top by virtue of its being defined last, or just because we want it to be considered the top element.

In grabEl, we check to see if the element-to-be-dragged is on top. That is, if it is activeEl. If it isn't we use the JavaScript 1.2 method moveAbove(), which takes one argument: the element we should move above. Once our element is above activeEl, it itself is assigned to activeEl, for the next time an element is dragged.

    function grabEl(e) { mouseX = e.pageX; mouseY = e.pageY; for ( i=0; i<document.layers.length; i++ ) { tempLayer = document.layers[i]; if ( tempLayer.id.indexOf("DRAG") == -1 ) { continue } if ( (mouseX > tempLayer.left) && (mouseX < (tempLayer.left + tempLayer.clip.width)) && (mouseY > tempLayer.top) && (mouseY < (tempLayer.top + tempLayer.clip.height)) ) { whichEl = tempLayer; } } if (whichEl == null) { return };
if (whichEl != activeEl) { whichEl.moveAbove(activeEl); activeEl = whichEl; } currentX = e.pageX; currentY = e.pageY; document.captureEvents(Event | MOUSEMOVE); document.onmousemove = moveEl; }

Now try the final code:

My God! What Have We Done?

Well, we have...

Now let's take a look at that code again!

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: 10/08/97
Revised: 10/23/97

URL: http://www.webreference.com/dhtml/column6/dragTop.html

Justtechjobs.comFind a programming school near you

Online Campus Both