Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div class="container">
   
   <div>
      <h3>Sortable List 1</h3>
      <ul id="list-1" class="sortable">
         <li id="item-1">Item 01 &nbsp;-&nbsp; <a href="#1">Link 01</a></li>
         <li id="item-2">Item 02 &nbsp;-&nbsp; <a href="#2">Link 02</a></li>
         <li id="item-3">Item 03 &nbsp;-&nbsp; <a href="#3">Link 03</a></li>
         <li id="item-4">Item 04 &nbsp;-&nbsp; <a href="#4">Link 04</a></li>
         <li id="item-5">Item 05 &nbsp;-&nbsp; <a href="#5">Link 05</a></li>
         <li id="item-6">Item 06 &nbsp;-&nbsp; <a href="#6">Link 06</a></li>
      </ul>
      <button id="sbtn-1" type="button">Serialize</button>
   </div>
   
   <div>
      <h3>Sortable List 2</h3>
      <ul id="list-2" class="sortable">
         <li id="item-7">Item 07 &nbsp;-&nbsp; <a href="#7">Link 07</a></li>
         <li id="item-8">Item 08 &nbsp;-&nbsp; <a href="#8">Link 08</a></li>
         <li id="item-9">Item 09 &nbsp;-&nbsp; <a href="#9">Link 09</a></li>
         <li id="item-10">Item 10 &nbsp;-&nbsp; <a href="#10">Link 10</a></li>
         <li id="item-11">Item 11 &nbsp;-&nbsp; <a href="#11">Link 11</a></li>
         <li id="item-12">Item 12 &nbsp;-&nbsp; <a href="#12">Link 12</a></li>
      </ul>
      <button id="sbtn-2" type="button">Serialize</button>
   </div>
   
</div>
              
            
!

CSS

              
                *, *:before, *:after {
   box-sizing: border-box;
   transition: 
      background-color 400ms ease-out, 
      border-color 400ms ease-out, 
      color 400ms ease-out;
}

html, body {
   display: block; 
   height: 100%; 
}

body {
   overflow: hidden; 
   overflow-y: scroll;
   font-family: Arial, sans-serif;
   background: rgb(252,255,244);
   background: radial-gradient( ellipse at center, rgba(252,255,244,1) 0%,rgba(223,229,215,1) 50%,rgba(179,190,173,1) 100% );
}

.container {
   display: flex;
   flex-direction: row;
   align-items: center;
   justify-content: space-around;
   width: 100%;
   height: 100%; 
   max-width: 800px; 
   margin: 0 auto;
   
   div {
      flex-basis: 40%; 
      
      h3 {
         font-weight: bold; 
         font-size: 150%; 
         line-height: 1em; 
      }
   }
}

ul.sortable {
   display: block; 
   list-style: none; 
   background: #e0e0e0;
   border-radius: 4px;
   box-shadow: 0 0 40px rgba(0,0,0,0.4);
   margin: 20px 0;
   padding: 5px;
   
   & > li {
      display: block;
      margin: 1px; 
      padding: 10px;
      border-radius: 4px;
      cursor: move;
      background: #fff;
      
      &:hover {
         background: #ffffe0;
      }
      &.active {
         background: #ccc;
      }
      &.dragging {
         background: #e0ffff;
         box-shadow: 0 4px 20px rgba(0,0,0,0.5);
         opacity: 0.5;
      }
   }
}
              
            
!

JS

              
                (function( name, factory ) {
   
   if( typeof window === "object" ) {
      
      // add to window 
      window[ name ] = factory();
      
      // add jquery plugin, if available  
      if( typeof jQuery === "object" ) {
         jQuery.fn[ name ] = function( options ) {
            return this.each( function() {
               new window[ name ]( this, options );
            });
         };
      }
   }
    
})( "Sortable", function() {
   
   var _w = window,
       _b = document.body,
       _d = document.documentElement;
   
   // get position of mouse/touch in relation to viewport 
   var getPoint = function( e )
   {
      var scrollX = Math.max( 0, _w.pageXOffset || _d.scrollLeft || _b.scrollLeft || 0 ) - ( _d.clientLeft || 0 ), 
          scrollY = Math.max( 0, _w.pageYOffset || _d.scrollTop || _b.scrollTop || 0 ) - ( _d.clientTop || 0 ), 
          pointX  = e ? ( Math.max( 0, e.pageX || e.clientX || 0 ) - scrollX ) : 0,
          pointY  = e ? ( Math.max( 0, e.pageY || e.clientY || 0 ) - scrollY ) : 0; 
      
      return { x: pointX, y: pointY }; 
   }; 
   
   // class constructor
   var Factory = function( container, options )
   {
      if( container && container instanceof Element )
      {
         this._container = container; 
         this._options   = options || {}; /* nothing atm */
         this._clickItem = null;
         this._dragItem  = null;
         this._hovItem   = null;
         this._sortLists = [];
         this._click     = {};
         this._dragging  = false;
         
         this._container.setAttribute( "data-is-sortable", 1 );
         this._container.style["position"] = "static";
         
         window.addEventListener( "mousedown", this._onPress.bind( this ), true );
         window.addEventListener( "touchstart", this._onPress.bind( this ), true );
         window.addEventListener( "mouseup", this._onRelease.bind( this ), true ); 
         window.addEventListener( "touchend", this._onRelease.bind( this ), true ); 
         window.addEventListener( "mousemove", this._onMove.bind( this ), true );
         window.addEventListener( "touchmove", this._onMove.bind( this ), true );
      }
   };
   
   // class prototype
   Factory.prototype = {
      constructor: Factory,
      
      // serialize order into array list 
      toArray: function( attr )
      {
         attr = attr || "id";
         
         var data = [], 
             item = null, 
             uniq = ""; 
         
         for( var i = 0; i < this._container.children.length; ++i )
         {
            item = this._container.children[ i ], 
            uniq = item.getAttribute( attr ) || "";
            uniq = uniq.replace( /[^0-9]+/gi, "" );
            data.push( uniq );
         }
         return data;
      }, 
      
      // serialize order array into a string 
      toString: function( attr, delimiter )
      {
         delimiter = delimiter || ":"; 
         return this.toArray( attr ).join( delimiter );
      }, 
      
      // checks if mouse x/y is on top of an item 
      _isOnTop: function( item, x, y )
      {
         var box = item.getBoundingClientRect(),
             isx = ( x > box.left && x < ( box.left + box.width ) ), 
             isy = ( y > box.top && y < ( box.top + box.height ) ); 
         return ( isx && isy );
      },
      
      // manipulate the className of an item (for browsers that lack classList support)
      _itemClass: function( item, task, cls )
      {
         var list  = item.className.split( /\s+/ ), 
             index = list.indexOf( cls );
         
         if( task === "add" && index == -1 )
         { 
            list.push( cls ); 
            item.className = list.join( " " ); 
         }
         else if( task === "remove" && index != -1 )
         {
            list.splice( index, 1 ); 
            item.className = list.join( " " ); 
         }
      }, 
      
      // swap position of two item in sortable list container 
      _swapItems: function( item1, item2 )
      {
         var parent1 = item1.parentNode, 
             parent2 = item2.parentNode;
         
         if( parent1 !== parent2 ) 
         {
            // move to new list 
            parent2.insertBefore( item1, item2 );
         }
         else { 
            // sort is same list 
            var temp = document.createElement( "div" ); 
            parent1.insertBefore( temp, item1 );
            parent2.insertBefore( item1, item2 );
            parent1.insertBefore( item2, temp );
            parent1.removeChild( temp );
         }
      },
      
      // update item position 
      _moveItem: function( item, x, y )
      {
         item.style["-webkit-transform"] = "translateX( "+ x +"px ) translateY( "+ y +"px )";
         item.style["-moz-transform"] = "translateX( "+ x +"px ) translateY( "+ y +"px )";
         item.style["-ms-transform"] = "translateX( "+ x +"px ) translateY( "+ y +"px )";
         item.style["transform"] = "translateX( "+ x +"px ) translateY( "+ y +"px )";
      },
      
      // make a temp fake item for dragging and add to container 
      _makeDragItem: function( item )
      {
         this._trashDragItem(); 
         this._sortLists = document.querySelectorAll( "[data-is-sortable]" );
         
         this._clickItem = item; 
         this._itemClass( this._clickItem, "add", "active" ); 

         this._dragItem = document.createElement( item.tagName );
         this._dragItem.className = "dragging"; 
         this._dragItem.innerHTML = item.innerHTML; 
         this._dragItem.style["position"] = "absolute";
         this._dragItem.style["z-index"] = "999";
         this._dragItem.style["left"] = ( item.offsetLeft || 0 ) + "px";
         this._dragItem.style["top"] = ( item.offsetTop || 0 ) + "px";
         this._dragItem.style["width"] = ( item.offsetWidth || 0 ) + "px";
         
         this._container.appendChild( this._dragItem ); 
      }, 
      
      // remove drag item that was added to container 
      _trashDragItem: function()
      {
         if( this._dragItem && this._clickItem )
         {
            this._itemClass( this._clickItem, "remove", "active" ); 
            this._clickItem = null; 
            
            this._container.removeChild( this._dragItem ); 
            this._dragItem = null; 
         }
      }, 
      
      // on item press/drag 
      _onPress: function( e )
      {
         if( e && e.target && e.target.parentNode === this._container )
         {
            e.preventDefault();
            
            this._dragging = true;
            this._click = getPoint( e ); 
            this._makeDragItem( e.target ); 
            this._onMove( e );
         }
      },
      
      // on item release/drop 
      _onRelease: function( e )
      {
         this._dragging = false;
         this._trashDragItem(); 
      },
      
      // on item drag/move
      _onMove: function( e )
      {
         if( this._dragItem && this._dragging ) 
         {
            e.preventDefault();
            
            var point     = getPoint( e ); 
            var container = this._container;

            // drag fake item 
            this._moveItem( this._dragItem, ( point.x - this._click.x ), ( point.y - this._click.y ) ); 
            
            // keep an eye for other sortable lists and switch over to it on hover 
            for( var a = 0; a < this._sortLists.length; ++a )
            {
               var subContainer = this._sortLists[ a ]; 
               
               if( this._isOnTop( subContainer, point.x, point.y ) ) 
               {
                  container = subContainer;
               }
            }
            
            // container is empty, move clicked item over to it on hover 
            if( this._isOnTop( container, point.x, point.y ) && container.children.length === 0 ) 
            {
               container.appendChild( this._clickItem ); 
               return; 
            }
            
            // check if current drag item is over another item and swap places 
            for( var b = 0; b < container.children.length; ++b )
            {
               var subItem = container.children[ b ]; 
               
               if( subItem === this._clickItem || subItem === this._dragItem )
               {
                  continue; 
               }
               if( this._isOnTop( subItem, point.x, point.y ) ) 
               {
                  this._hovItem = subItem; 
                  this._swapItems( this._clickItem, subItem ); 
               }
            }
         }
      },
      
   };

   // export
   return Factory;
});


// helper init function 
function initSortable( list, sbtn )
{
   var listObj  = document.getElementById( list ),
       sbtnObj  = document.getElementById( sbtn ),
       sortable = new Sortable( listObj ); 
   
   sbtnObj.addEventListener( "click", function( e )
   {
      e.preventDefault(); 
      alert( sortable.toString() ); 
   });
}

// init lists 
initSortable( "list-1", "sbtn-1" );
initSortable( "list-2", "sbtn-2" );
              
            
!
999px

Console