home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 February / PCWorld_2003-02_cd.bin / Novinky / interval / img / 206 / dragdrop.js < prev    next >
Encoding:
JavaScript  |  2003-01-06  |  2.1 KB  |  91 lines

  1. /// Drag&Drop System
  2.  
  3.     var dragobj;
  4.     
  5.     function m_down(event, object)
  6.     {
  7.     
  8.         
  9.         dragobj = object;      
  10.         
  11.         dragobj.style.zIndex = "100";    
  12.         if(!dragobj.style.left) dragobj.style.left = "0px";
  13.         if(!dragobj.style.top) dragobj.style.top = "0px";
  14.             
  15.         // Zapamatuj pozici mysi pri klintuti
  16.         dragobj.clickX = event.clientX;  
  17.         dragobj.clickY = event.clientY;
  18.             
  19.         // Zapamatuj pozici objektu pri klintuti
  20.         dragobj.left = parseInt(dragobj.style.left); 
  21.         dragobj.top = parseInt(dragobj.style.top);
  22.  
  23.  
  24.          // Zacni sledovat pohyb mysi
  25.     if(IE)
  26.         {    
  27.             document.attachEvent('onmousemove', m_move); 
  28.             document.attachEvent('onmouseup', m_up);         
  29.         }
  30.     
  31.         if(NS || OPERA) 
  32.         {
  33.             document.addEventListener('mousemove', m_move, true);
  34.             document.addEventListener('mouseup', m_up, true);         
  35.         }
  36.  
  37. return stopEvent(event);
  38. }
  39.  
  40.  
  41. function m_move(event)
  42. {
  43.     // Spocti rozdil mezi puvodni pozici mysi pri stisku a nynejsi pozici a zavolej proceduru objektu pro presun 
  44.     dragobj.ondragdrop(event.clientX-dragobj.clickX,event.clientY-dragobj.clickY, event);
  45.     return stopEvent(event);
  46. }
  47.  
  48. function m_up(event)
  49. {
  50.     // Ukonci sledovani mysi
  51.     if(IE)
  52.     {
  53.         document.detachEvent('onmousemove', m_move);     
  54.         document.detachEvent('onmouseup', m_up); 
  55.         if(dragobj.ondragend)dragobj.ondragend(event);
  56.     }
  57.  
  58.     if(NS || OPERA)
  59.     {
  60.         document.removeEventListener('mousemove', m_move, true); 
  61.         document.removeEventListener('mouseup', m_up, true); 
  62.         if(dragobj.ondragend)dragobj.ondragend(event);
  63.     }
  64.  
  65.     return stopEvent(event);    
  66. }
  67.  
  68.  
  69. // Prirad objektu moznost presunu
  70. function startDrag(what)
  71. {
  72.     if(what.style.position!="relative" && what.style.position!="absolute") {what.style.position="relative";}
  73.     what.onmousedown = function(e) {if(e) {event=e;} m_down(event,what);}
  74.     what.drag = 1;
  75. }
  76.  
  77. // Odeber objektu moznost presunu 
  78. function stopDrag(what)
  79. {
  80.     what.onmousedown = null;
  81.     what.drag = 0;
  82. }
  83.  
  84. // Zastav sireni udalosti
  85. function stopEvent(event)
  86. {
  87.     if(IE) {event.cancelBubble = true; return false;}
  88.     if(NS || OPERA) {event.stopPropagation(); event.preventDefault(); return false;}
  89. }
  90.  
  91.