2012-05-15 116 views
0

我在製作sticky note system,我希望粘滯便箋可以通過javascript拖動。我在這個site上找到了一個拖放腳本,這對我來說非常有用。但是我在粘滯便箋div裏面有一個「拖拽欄」,它應該是唯一可以拖動特定粘滯便箋的地方。我只拖動了光標所點擊的腳本,我希望它拖動「.dragbar」div的父元素「.parent」。使用Javascript進行拖放

現在,當您點擊.parent時,腳本被設置爲拖動.parent。如何在此代碼中選擇「.dragbar」的父級「div」?

如果我讓腳本選擇.dragbar,它只是在.parent內移動拖動條。注意:腳本是原始代碼,只是第87和95行中的選擇器名稱從「拖動」更改爲「父級」。

<!DOCTYPE html> 
<html> 

    <head> 
     <style type="text/css"> 
      .pagecontent { 
       width:98%; 
       height:96%; 
       overflow:hidden; 
       background:lightyellow; 
       border:1px solid black; 
       padding:10px; 
      } 
      .parent { 
       position:relative; 
       height:300px; 
       width:300px; 
       background-color:gray; 
       border:1px solid black; 
       float:left; 
       margin:0 10px 0 0; 
       overflow:auto; 
      } 
      .dragbar { 
       height:50px; 
       width:300px; 
       background:lightgray; 
       cursor:move; 
       position:relative; 
      } 
     </style> 
    </head> 

    <body> 
     <div class="pagecontent"> 
      <h1>Dragging Practice</h1> 
      <h3>Drag and drop the parent div by using the child dragbar</h3> 
      <pre id="debug">mouse up</pre> 
      <div class="parent"> 
       <div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is 
       .parent</div> 
      <div class="parent"> 
       <div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is 
       .parent</div> 
      <script language="JavaScript" type="text/javascript"> 
       <!-- 
       // this is simply a shortcut for the eyes and fingers 
       function $(id) { 
        return document.getElementById(id); 
       } 
       var _startX = 0; // mouse starting positions 
       var _startY = 0; 
       var _offsetX = 0; // current element offset 
       var _offsetY = 0; 
       var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove 
       var _oldZIndex = 0; // we temporarily increase the z-index during drag 
       var _debug = $('debug'); // makes life easier 
       InitDragDrop(); 

       function InitDragDrop() { 
        document.onmousedown = OnMouseDown; 
        document.onmouseup = OnMouseUp; 
       } 

       function OnMouseDown(e) { 
        // IE is retarded and doesn't pass the event object 
        if (e == null) e = window.event; 
        // IE uses srcElement, others use target 
        var target = e.target != null ? e.target : e.srcElement; 
        _debug.innerHTML = target.className == 'parent' //Selector 
        ? 
        'draggable element clicked' : 'NON-draggable element clicked'; 
        // for IE, left click == 1 
        // for Firefox, left click == 0 
        if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'parent') //Selector 
        { 
         // grab the mouse position 
         _startX = e.clientX; 
         _startY = e.clientY; 
         // grab the clicked element's position 
         _offsetX = ExtractNumber(target.style.left); 
         _offsetY = ExtractNumber(target.style.top); 
         // bring the clicked element to the front while it is being dragged 
         _oldZIndex = target.style.zIndex; 
         target.style.zIndex = 10000; 
         // we need to access the element in OnMouseMove 
         _dragElement = target; 
         // tell our code to start moving the element with the mouse 
         document.onmousemove = OnMouseMove; 
         // cancel out any text selections 
         document.body.focus(); 
         // prevent text selection in IE 
         document.onselectstart = function() { 
          return false; 
         }; 
         // prevent IE from trying to drag an image 
         target.ondragstart = function() { 
          return false; 
         }; 
         // prevent text selection (except IE) 
         return false; 
        } 
       } 

       function ExtractNumber(value) { 
        var n = parseInt(value); 
        return n == null || isNaN(n) ? 0 : n; 
       } 

       function OnMouseMove(e) { 
        if (e == null) var e = window.event; 
        // this is the actual "drag code" 
        _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px'; 
        _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px'; 
        _debug.innerHTML = '(' + _dragElement.style.left + ', ' + _dragElement.style.top + ')'; 
       } 

       function OnMouseUp(e) { 
        if (_dragElement != null) { 
         _dragElement.style.zIndex = _oldZIndex; 
         // we're done with these events until the next OnMouseDown 
         document.onmousemove = null; 
         document.onselectstart = null; 
         _dragElement.ondragstart = null; 
         // this is how we know we're not dragging 
         _dragElement = null; 
         _debug.innerHTML = 'mouse up'; 
        } 
       } 
       //--> 
      </script> 
     </div> 
    </body> 

</html> 
+0

我不會使用包含註釋的代碼,例如「IE被延遲」。這樣的評論意味着一個編碼人員顯然與「高級瀏覽器」的「奢侈」生活了很長時間,並且永遠不會意識到IE9和IE10正在取得的進展。考慮到編碼器,然後繼續使用「innerHTML」 - 微軟發明的一個屬性(!) - 他/她變成了一個完整的僞君子。也就是說,我不使用其他人的代碼,我自己寫代碼XD –

+0

我自己完全沒有對IE的反對,並且我非常喜歡它,因爲它在IE8和IE9中的最新進展。我想我只是忘記了這個評論,但我不會寬恕使用這種語言的任何形式的談話。謝謝閱讀! – jcrowley

+0

我並沒有真正批評你,更多的是首先編寫代碼的人。 –

回答

3

我勸你看看jQuery UI's draggabledroppable。這些無疑比你鏈接的腳本更強大,你可以使用jQuery強大的選擇器設置元素爲可拖動的,並且只能在特定元素中放置。

+0

非常感謝! – jcrowley