title: How to move HTML element above or below another element using an animation
body: <p>I know I can move an element to above or below another element with a fairly simple JS function. But I would like to make that action animated. Is there an easy way to do that with vanilla JS or jQuery or CSS?</p>
  <p>As it is, the action is instantaneous, which is not very obvious and a little jarring.</p>
  <p>Consider the code below (or <a href="https://jsfiddle.net/jakemclelland/d13vm4o2/4/" rel="nofollow noreferrer">demo fiddle here</a>). Most of this gets generated from MVC and razor. But I don't want to distract from the real issue, so I've stripped all that out, down to what gets rendered at the end of the day as a working example. (Sorry it's a bit ugly without the rest of the css and other things there!)</p>
  <h1>Code</h1>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function moveUp(rowId) {
  
    var rowSelector = "#" + rowId;
  
    var myRow = $(rowSelector).closest(".item-row");
    var prev = myRow.prev(".item-row");
  
    if (myRow.attr("id") != $(".item-row:first").attr("id")) {
      myRow.remove();
      myRow.insertBefore(prev);
    }
  }
  
  function moveDown(rowId) {
  
    var rowSelector = "#" + rowId;
  
    var myRow = $(rowSelector).closest(".item-row");
    var next = myRow.next(".item-row");
  
    if (myRow.attr("id") != $(".item-row:last").attr("id")) {
      myRow.remove();
      myRow.insertAfter(next);
    }
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.item-row {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    margin-bottom: 1rem;
    word-wrap: break-word;
    border-radius: 0;
    box-shadow: none;
    padding: 1rem 0 1rem 1rem;
    border: 2px solid #d3d3d3;
    border-left: 6px solid #006400;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" /&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;html&gt;
  
  &lt;body&gt;
    &lt;div id="item-panel"&gt;
      &lt;div&gt;
        &lt;p&gt;
          Items
        &lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="item-row" id="one"&gt;
        &lt;div class="row"&gt;
  
          &lt;div class="col-md-2"&gt;
            &lt;div onclick="moveUp('one')" title="move up" class="move-up btn"&gt;
              &lt;i class="fas fa-sort-up"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;div onclick="moveDown('one')" title="move down" class="move-down btn"&gt;
              &lt;i class="fas fa-sort-down"&gt;&lt;/i&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-8"&gt;
            &lt;p&gt;
              One! Some data and other things
            &lt;/p&gt;
          &lt;/div&gt;
  
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="item-row" id="two"&gt;
        &lt;div class="row"&gt;
  
          &lt;div class="col-md-2"&gt;
            &lt;div onclick="moveUp('two')" title="move up" class="move-up"&gt;
              &lt;i class="fas fa-sort-up"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;div onclick="moveDown('two')" title="move down" class="move-down"&gt;
              &lt;i class="fas fa-sort-down"&gt;&lt;/i&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-8"&gt;
            &lt;p&gt;
              Two! more data.
            &lt;/p&gt;
          &lt;/div&gt;
  
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="item-row" id="three"&gt;
        &lt;div class="row"&gt;
  
          &lt;div class="col-md-2"&gt;
            &lt;div onclick="moveUp('three')" title="move up" class="move-up"&gt;
              &lt;i class="fas fa-sort-up"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;div onclick="moveDown('three')" title="move down" class="move-down"&gt;
              &lt;i class="fas fa-sort-down"&gt;&lt;/i&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-8"&gt;
            &lt;p&gt;
              Three! And still some more things.
            &lt;/p&gt;
          &lt;/div&gt;
  
        &lt;/div&gt;
      &lt;/div&gt;
  
    &lt;/div&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>