Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery UI Draggable Snapping with margin

I'm working on a project where users can customize their own dashboard page. This means they can drag components onto the page and position them as they like.

To get a nicely layout page I want the components to snap to each other. At this moment I'm using the snap: true property, which will place the components right next to each other. However, I'd like to have some space in between the components after they snapped. So instead of snapping to the exact position of the other component, it must snap to the other component with a 5 pixel gap.

Is there any way to do this?

like image 507
Jan Korf Avatar asked Jul 07 '26 06:07

Jan Korf


1 Answers

A few options to think about:

  • Wrap each component in an enclosing element (e.g. div) with a padding of 2.5 pixels and a transparent background and make these enclosing elements the draggable ones. Thus when the enclosing elements snap together, your current elements will appear to have snapped together but with a 5 pixel gap to each other

  • Subscribe to the 'stop' event on the draggable items and use it to manually reposition them to a 5 pixel gap after the snapping has finished

  • Set up 'droppable' elements to accept the 'draggable' components, laying them out to ensure that a 5 pixel gap is retained between each

like image 131
Chamila Chulatunga Avatar answered Jul 10 '26 03:07

Chamila Chulatunga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!