views:

25

answers:

1

I was puzzled when I saw jQuery "Snap to element". Does anyone know how can I implement it without jQuery, with just raw javascript.

Thanks

A: 

Assuming you've built a drag and drop implementation without jQuery UI to have it snap to another element you need to now do the following:

  • Determine the position and dimensions of the dragging object and other objects to which you want the element to be able to snap to
  • While an object is being dragged you need to determine if the object being dragged is within your arbitrary threshold distance of an object it can snap to at that point you bump the dragged elements left/top to match the left/top/bottom/right position of the "snappable" element
JaredM
but in my case, the draggable element can be snapped to any element whose dimensions and position is not known to me.
Meher Ranjan
I might write up a loop that checks for all the nodes and check if my element is bumping any of them, but this would be very inefficient. OR is jQuery doing the same thing?
Meher Ranjan
jQuery is checking it's location whenever the drag event occurs on the draggable element.
JaredM