2017-09-14 89 views
0

當我設置可拖動對象捕捉到一個範圍(只做內部捕捉)時,我發現它經常返回相鄰的捕捉,儘管顯然是在它們之外。注意SnapMode,因爲它是最重要的部分:jQuery UI Snappable不能僅返回內部捕捉與捕捉模式設置

$(".draggable").draggable({ 
    snap: ".snap", 
    snapMode: "inner", 
    stop: function(event, ui) { 
     /* Get the possible snap targets: */ 
     var snapped = $(this).data('draggable').snapElements; 

     /* Pull out only the snap targets that are "snapping": */ 
     var snappedTo = $.map(snapped, function(element) { 
      return element.snapping ? element.item : null; 
     }); 

     /* Display the results: */ 
     var result= ''; 
     $.each(snappedTo, function(idx, item) { 
      result += $(item).text() + ", "; 
     }); 

     $("#results").html("Snapped to: " + (result === '' ? "Nothing!" : result)); 
    } 
}); 

這原本是基於這樣一個問題:How to find out about the "snapped to" element for jQuery UI draggable elements on snap

我需要「snappable」區爲每設計我彼此相鄰「M試圖建立對,就像這樣: drag and drop UI example

我的jsfiddle證明這個問題是在這裏:http://jsfiddle.net/myingling/dx54dapr/1/

(嘗試捕捉到捕捉2,你會看到它說它捕捉到捕捉1和捕捉2!)

+0

我認爲你需要把兩個卡領域 – codtex

+0

之間的一些空間爲什麼,如果我使用'SNAPMODE:「內部」'?不應該只檢測丟棄區域內的物體? – Bing

回答

0

捕捉模式檢查元素是否應該捕捉到其他元素。 snapMode確定可拖動對象將捕捉到的捕捉元素的哪些邊緣。所以你需要在盒子之間放一些間隙,你也可以使用snapTolerance

$(".draggable").draggable({ 
 
    snap: ".snap", 
 
    snapMode: "inner", 
 
    stop: function(event, ui) { 
 
     /* Get the possible snap targets: */ 
 
     var snapped = $(this).data('ui-draggable').snapElements; 
 
     
 
     /* Pull out only the snap targets that are "snapping": */ 
 
     var snappedTo = $.map(snapped, function(element) { 
 
      return element.snapping ? element.item : null; 
 
     }); 
 
     
 
     /* Display the results: */ 
 
     var result= ''; 
 
     $.each(snappedTo, function(idx, item) { 
 
      result += $(item).text() + ", "; 
 
     }); 
 
     
 
     $("#results").html("Snapped to: " + (result === '' ? "Nothing!" : result)); 
 
    } 
 
});
.draggable { 
 
    width: 90px; 
 
    height: 80px; 
 
    padding: 5px; 
 
    font-size: .9em; 
 
} 
 

 
.snap { 
 
    width: 300px; 
 
    height: 100px; 
 
    margin-top: 50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script 
 
    src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" 
 
    integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw=" 
 
    crossorigin="anonymous"></script> 
 
<link href="https://ajax.microsoft.com/ajax/jquery.ui/1.8.7/themes/black-tie/jquery-ui.css" rel="stylesheet"/> 
 

 
<div id="snap-one" class="snap ui-widget-header">Snap 1</div> 
 
<div id="snap-two" class="snap ui-widget-header">Snap 2</div> 
 

 
<div class="demo"> 
 
    <br clear="both" /> 
 
    
 
    <div id="draggable" class="draggable ui-widget-content"> 
 
     <p>Oh Snap!</p> 
 
    </div> 
 
    <div id="results"></div> 
 
</div>

+0

在我試圖設計的頁面上,我創建了許多這些元素的行,因此在它們之間放置大量邊距不起作用。嘲笑我的真實世界的用例可以在這裏找到:https://stackoverflow.com/questions/46142843/css-position-absolute-on-one-axis-only-to-prevent-collision-overlap-without -wa – Bing

+0

我用屏幕截圖更新了我的問題,顯示了更好的問題。如果間距已經發揮作用,那麼我所鏈接的原始問題/答案就足夠了。對不起,我最初的貧窮的解釋。 – Bing