home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 May / CHIPCD200305.iso / super / altn / md_en.exe / DRAGDROP.JS < prev    next >
Encoding:
Text File  |  2002-01-24  |  3.0 KB  |  105 lines

  1.     currentX = currentY = 0;
  2.     whichEl = null;
  3.     
  4.     function grabEl(e) {
  5.         if (IE4) {
  6.             whichEl = event.srcElement;
  7.     
  8.             while (whichEl.id.indexOf("DRAG") == -1) {
  9.                 whichEl = whichEl.parentElement;
  10.                 if (whichEl == null) { return }
  11.             }
  12.         }
  13.         else {
  14.             mouseX = e.pageX;
  15.             mouseY = e.pageY;
  16.     
  17.             for ( i=0; i<document.layers.length; i++ ) {
  18.             tempLayer = document.layers[i];
  19.                 if ( tempLayer.id.indexOf("DRAG") == -1 ) { continue }
  20.                 if ( (mouseX > tempLayer.left) && (mouseX < (tempLayer.left + tempLayer.clip.width)) && (mouseY > tempLayer.top) && (mouseY < (tempLayer.top + tempLayer.clip.height)) ) {
  21.                     whichEl = tempLayer;
  22.                 }
  23.             } 
  24.     
  25.             if (whichEl == null) { return}
  26.         }
  27.     
  28.         if (whichEl != activeEl) {
  29.             if (IE4) { whichEl.style.zIndex = activeEl.style.zIndex + 1 }
  30.                 else { whichEl.moveAbove(activeEl) };
  31.                 activeEl = whichEl;
  32.         }
  33.     
  34.         if (IE4) {
  35.             whichEl.style.pixelLeft = whichEl.offsetLeft;
  36.             whichEl.style.pixelTop = whichEl.offsetTop;
  37.     
  38.             currentX = (event.clientX + document.body.scrollLeft);
  39.             currentY = (event.clientY + document.body.scrollTop); 
  40.     
  41.         }
  42.         else {
  43.             currentX = e.pageX;
  44.             currentY = e.pageY;
  45.     
  46.             document.captureEvents(Event.MOUSEMOVE);
  47.             document.onmousemove = moveEl;
  48.         }
  49.     }
  50.     
  51.     function moveEl(e) {
  52.         if (whichEl == null) { return };
  53.     
  54.         if (IE4) {
  55.             newX = (event.clientX + document.body.scrollLeft);
  56.             newY = (event.clientY + document.body.scrollTop);
  57.         }
  58.         else {
  59.             newX = e.pageX;
  60.             newY = e.pageY;
  61.         }
  62.  
  63.         distanceX = (newX - currentX);
  64.         distanceY = (newY - currentY);
  65.         currentX = newX;
  66.         currentY = newY;
  67.     
  68.         if (IE4) {
  69.             whichEl.style.pixelLeft += distanceX;
  70.             whichEl.style.pixelTop += distanceY;
  71.             event.returnValue = false;
  72.         }
  73.         else { whichEl.moveBy(distanceX,distanceY) }
  74.     }
  75.     
  76.     function checkEl() {
  77.         if (whichEl!=null) { return false }
  78.     }
  79.     
  80.     function dropEl() {
  81.         if (NS4) { document.releaseEvents(Event.MOUSEMOVE) }
  82.         whichEl = null;
  83.     }
  84.     
  85.     function cursEl() {
  86.         if (event.srcElement.id.indexOf("DRAG") != -1) {
  87.             event.srcElement.style.cursor = "move"
  88.         }
  89.     }
  90.     
  91.     if (ver4) {
  92.         if (NS4) {
  93.             document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
  94.         }
  95.         else {
  96.             document.onmousemove = moveEl;
  97.             document.onselectstart = checkEl;
  98.             document.onmouseover = cursEl;
  99.         }
  100.     
  101.         document.onmousedown = grabEl;
  102.         document.onmouseup = dropEl;
  103.     }
  104.     
  105.