Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery's draggable grid
    primarykey
    data
    text
    <p>It looks like that the 'grid' option in the constructor of <a href="http://jqueryui.com/demos/draggable/" rel="nofollow noreferrer">Draggable</a> is relatively bound to the original coordinates of the element being dragged - so simply put, if you have three draggable divs with their <strong>top</strong> set respectively to 100, 200, 254 pixels relative to their parent:</p> <pre><code>&lt;div class="parent-div" style="position: relative;"&gt; &lt;div id="div1" class="draggable" style="top: 100px; position: absolute;"&gt;&lt;/div&gt; &lt;div id="div2" class="draggable" style="top: 200px; position: absolute;"&gt;&lt;/div&gt; &lt;div id="div3" class="draggable" style="top: 254px; position: absolute;"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Adn all of them are getting enabled for dragging with 'grid' set to [1, 100]:</p> <pre><code>draggables = $('.draggable'); $.each(draggables, function(index, elem) { $(elem).draggable({ containment: $('#parent-div'), opacity: 0.7, revert: 'invalid', revertDuration: 300, grid: [1, 100], refreshPositions: true }); }); </code></pre> <p>Problem here is that as soon as you drag <strong>div3</strong>, say, down, it's top is increased by 100, moving it to <strong>354px</strong> instead of being increased by just mere <strong>46px</strong> (254 + 46 = 300), which would get it to the next stop in the grid - <strong>300px</strong>, if we are looking at the <strong>parent-div</strong> as a point of reference and "grid holder".</p> <p>I had a look at the draggable sources and it seem to be an in-built flaw - they just do all the calculations relative to the original position of the draggable element.</p> <p>I would like to avoid monkey-patching the code of draggable library and what I am really looking for here is the way how to make the Draggable calculate the grid positions relative to containing parent. However if monkey-patching is unavoidable, I guess I'll have to live with it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload