Encountering an issue with jQuery UI sortable/draggable where the element being dragged, when centered using margin:0 auto
, starts dragging from the left side of the container instead of the center where it is positioned.
Check out this demo to see the problem in action: http://codepen.io/anon/pen/YqWRvV. Just try dragging the red square.
If you eliminate margin:0 auto
from the '.drag' element, you'll notice that the dragging behaves normally.
Any suggestions on how to resolve this issue and ensure the element drags from its true center location when centered using margin:0 auto
?
This issue appears to be specific to Google Chrome.
$("#c").sortable();
`.drag {
margin: 0 auto;
width: 200px;
height: 200px;
background-color: red;
}`
Centering the element with CSS 'calc' does address the problem, but considering my element's width can change dynamically, 'calc' isn't as widely supported as 'margin'.
Although wrapping the draggable in a container resolves the issue, I'm seeking a solution that doesn't involve altering the HTML structure.
UPDATE: It turns out this isn't a jQuery bug but rather a bug within Google Chrome. It seems Chrome retrieves an incorrect position for the element, causing jQuery to initiate the drag from where Chrome inaccurately places it. I've outputted the left position of the element in the console which shows as 13px even though it isn't. Check it out here: http://codepen.io/anon/pen/YqWRvV. I wonder if this issue has been reported.
SOLUTION Discover the solution at: http://codepen.io/anon/pen/MyjeJP. Thank you Julien Grégoire. Please note: This solution may require a method to refresh the helper position.