Skip to content

Commit 94f8c4d

Browse files
committed
Draggable: apply axis options to position instead of style. Fixes #7251 - Draggable: constrained axis option returns incorrect position.
1 parent 9ca0a19 commit 94f8c4d

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

tests/unit/draggable/draggable_options.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,28 @@ test( "{ axis: false }, default", function() {
106106
});
107107

108108
test( "{ axis: 'x' }", function() {
109-
expect( 2 );
110-
var element = $( "#draggable2" ).draggable({ axis: "x" });
109+
expect( 3 );
110+
var element = $( "#draggable2" ).draggable({
111+
axis: "x",
112+
// TODO: remove the stop callback when all TestHelpers.draggable.testDrag bugs are fixed
113+
stop: function( event, ui ) {
114+
var expectedPosition = { left: ui.originalPosition.left + 50, top: ui.originalPosition.top };
115+
deepEqual( ui.position, expectedPosition, "position dragged[50,0] for axis: x" );
116+
}
117+
});
111118
TestHelpers.draggable.testDrag( element, element, 50, 50, 50, 0, "axis: x" );
112119
});
113120

114121
test( "{ axis: 'y' }", function() {
115-
expect( 2 );
116-
var element = $( "#draggable2" ).draggable({ axis: "y" });
122+
expect( 3 );
123+
var element = $( "#draggable2" ).draggable({
124+
axis: "y",
125+
// TODO: remove the stop callback when all TestHelpers.draggable.testDrag bugs are fixed
126+
stop: function( event, ui ) {
127+
var expectedPosition = { left: ui.originalPosition.left, top: ui.originalPosition.top + 50 };
128+
deepEqual( ui.position, expectedPosition, "position dragged[0,50] for axis: y" );
129+
}
130+
});
117131
TestHelpers.draggable.testDrag( element, element, 50, 50, 0, 50, "axis: y" );
118132
});
119133

ui/jquery.ui.draggable.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,9 @@ $.widget("ui.draggable", $.ui.mouse, {
221221
this.position = ui.position;
222222
}
223223

224-
if(!this.options.axis || this.options.axis !== "y") {
225-
this.helper[0].style.left = this.position.left+"px";
226-
}
227-
if(!this.options.axis || this.options.axis !== "x") {
228-
this.helper[0].style.top = this.position.top+"px";
229-
}
224+
this.helper[ 0 ].style.left = this.position.left + "px";
225+
this.helper[ 0 ].style.top = this.position.top + "px";
226+
230227
if($.ui.ddmanager) {
231228
$.ui.ddmanager.drag(this, event);
232229
}
@@ -554,6 +551,13 @@ $.widget("ui.draggable", $.ui.mouse, {
554551
pageX = containment ? ((left - this.offset.click.left >= containment[0] || left - this.offset.click.left > containment[2]) ? left : ((left - this.offset.click.left >= containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left;
555552
}
556553

554+
if ( o.axis === "y" ) {
555+
pageX = this.originalPageX;
556+
}
557+
558+
if ( o.axis === "x" ) {
559+
pageY = this.originalPageY;
560+
}
557561
}
558562

559563
return {

0 commit comments

Comments
 (0)