Skip to content

Commit 2d03839

Browse files
committed
Draggable: Account for margins when snapping
Fixes #9724
1 parent 54004c8 commit 2d03839

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

tests/unit/draggable/draggable_options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,10 @@ test( "snap, snapMode, and snapTolerance", function() {
961961
}),
962962
element2 = $( "#draggable2" ).draggable();
963963

964+
// http://bugs.jqueryui.com/ticket/9724
965+
// Draggable: Snapping coordinates thrown off by margin on draggable
966+
element.css( "margin", "3px" );
967+
964968
element.offset({
965969
top: 1,
966970
left: 1

ui/draggable.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,9 @@ $.ui.plugin.add("draggable", "snap", {
916916

917917
for (i = inst.snapElements.length - 1; i >= 0; i--){
918918

919-
l = inst.snapElements[i].left;
919+
l = inst.snapElements[i].left - inst.margins.left;
920920
r = l + inst.snapElements[i].width;
921-
t = inst.snapElements[i].top;
921+
t = inst.snapElements[i].top - inst.margins.top;
922922
b = t + inst.snapElements[i].height;
923923

924924
if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) {
@@ -935,16 +935,16 @@ $.ui.plugin.add("draggable", "snap", {
935935
ls = Math.abs(l - x2) <= d;
936936
rs = Math.abs(r - x1) <= d;
937937
if (ts) {
938-
ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
938+
ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top;
939939
}
940940
if (bs) {
941-
ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
941+
ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top;
942942
}
943943
if (ls) {
944-
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
944+
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left;
945945
}
946946
if (rs) {
947-
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
947+
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left;
948948
}
949949
}
950950

@@ -956,16 +956,16 @@ $.ui.plugin.add("draggable", "snap", {
956956
ls = Math.abs(l - x1) <= d;
957957
rs = Math.abs(r - x2) <= d;
958958
if (ts) {
959-
ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
959+
ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top;
960960
}
961961
if (bs) {
962-
ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
962+
ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top;
963963
}
964964
if (ls) {
965-
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
965+
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left;
966966
}
967967
if (rs) {
968-
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
968+
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left;
969969
}
970970
}
971971

0 commit comments

Comments
 (0)