Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with a solution that seems to work pretty well. It's a proof of concept, so you'll probably have to modify it a bit to work better for your specific case. Also, I only tested it in Firefox, but I don't see any reason why this wouldn't work in all the browsers. Anyway, here it is:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('li').click(function() { // the clicked LI var clicked = $(this); // all the LIs above the clicked one var previousAll = clicked.prevAll(); // only proceed if it's not already on top (no previous siblings) if(previousAll.length &gt; 0) { // top LI var top = $(previousAll[previousAll.length - 1]); // immediately previous LI var previous = $(previousAll[0]); // how far up do we need to move the clicked LI? var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop'); // how far down do we need to move the previous siblings? var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight()); // let's move stuff clicked.css('position', 'relative'); previousAll.css('position', 'relative'); clicked.animate({'top': -moveUp}); previousAll.animate({'top': moveDown}, {complete: function() { // rearrange the DOM and restore positioning when we're done moving clicked.parent().prepend(clicked); clicked.css({'position': 'static', 'top': 0}); previousAll.css({'position': 'static', 'top': 0}); }}); } }); }); &lt;/script&gt; &lt;ul&gt; &lt;li&gt;&lt;a&gt;Hank&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a&gt;Alice&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a&gt;Tom&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a&gt;Ashlee&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>It calculates the difference in offsets between the clicked <code>LI</code> and first <code>LI</code> and moves the clicked one up to the top by setting its <code>position</code> to <code>relative</code> and animating the <code>top</code> property. Similarly, it calculates how much space was left behind by the clicked <code>LI</code> and moves all the previous ones down accordingly. When it's done with the animations, it rearranges the DOM to match the new order and restores the positioning styles.</p> <p>Hope that helps!</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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