Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

              
                <section class="container">
  
  <div class="list-item" id="alpha">
    <div class="item-content">
      <span class="order">1</span> Alpha
    </div>
  </div>
  
  <div class="list-item" id="bravo">
    <div class="item-content">
      <span class="order">2</span> Bravo
    </div>
  </div>
  
  <div class="list-item" id="charlie">
    <div class="item-content">
      <span class="order">3</span> Charlie
    </div>
  </div>
  
  <div class="list-item" id="delta">
    <div class="item-content">
      <span class="order">4</span> Delta
    </div>
  </div>
   
  <div class="list-item" id="echo">
    <div class="item-content">
      <span class="order">5</span> Echo
    </div>
  </div>
  
  <div class="list-item" id="foxtrot">
    <div class="item-content">
      <span class="order">6</span> Foxtrot
    </div>
  </div>
  
  <div class="list-item" id="gulf">
    <div class="item-content">
      <span class="order">7</span> Gulf
    </div>
  </div>
  
  <div class="list-item" id="hotel">
    <div class="item-content">
      <span class="order">8</span> Hotel
    </div>
  </div>
  
  <div class="list-item" id="india">
    <div class="item-content">
      <span class="order">9</span> India
    </div>
  </div>
  
</section>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  cursor: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/cursor.png") 32 32, auto;
  background: rgb(238,238,238);
  overflow: hidden;  
}

*, *:before, *:after {
  box-sizing: border-box;
}

.container {
  position: relative;  
  top: 50%;
  left: 50%;  
  width: 750px;
  height: 450px;  
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, -50%);
}

.list-item {  
  position: absolute;
  top: 0;
  left: 0;  
  height: 140px;
  width: 240px;
}

.item-content {
  height: 100%;
  border: 0px solid rgba(123, 123, 123, 0.498039);
  border-radius: 4px;
  color: rgb(153, 153, 153);
  line-height: 140px;
  padding-left: 32px;
  font-size: 24px;
  font-weight: 400;
  background-color: rgb(255, 255, 255);
  box-shadow: rgba(0,0,0,0.2) 0px 1px 2px 0px;
}



              
            
!

JS

              
                // List version
// https://codepen.io/osublake/pen/jrqjdy/

var rowSize   = 150;
var colSize   = 250;
var totalRows = 3;
var totalCols = 3;

var cells = [];

// Map cell locations to array
for (var row = 0; row < totalRows; row++) {
  for (var col = 0; col < totalCols; col++) {
    cells.push({
      row: row,
      col: col,
      x: col * colSize,
      y: row * rowSize
    });
  }
}

var container = document.querySelector(".container");
var listItems = Array.from(document.querySelectorAll(".list-item")); // Array of elements
var sortables = listItems.map(Sortable); // Array of sortables
var total     = sortables.length;

TweenLite.to(container, 0.5, { autoAlpha: 1 });

function changeIndex(item, to, sameRow, sameCol) {
    
  // Check if adjacent to new position
  if ((sameRow && !sameCol) || (!sameRow && sameCol)) {
    
    // Swap positions in array
    var temp = sortables[to];
    sortables[to] = item;
    sortables[item.index] = temp;
    
  } else {
    
    // Change position in array
    arrayMove(sortables, item.index, to);
  }
    
  // Simple, but not optimized way to change element's position in DOM. Not always necessary. 
  sortables.forEach(sortable => container.appendChild(sortable.element)); 
    
  // Set index for each sortable
  sortables.forEach((sortable, index) => sortable.setIndex(index));
}

function Sortable(element, index) {
    
  var content = element.querySelector(".item-content");
  var order   = element.querySelector(".order");
  
  var animation = TweenLite.to(content, 0.3, {
    boxShadow: "rgba(0,0,0,0.2) 0px 16px 32px 0px",
    force3D: true,
    scale: 1.1,
    paused: true
  });
  
  var dragger = new Draggable(element, {        
    onDragStart: downAction,
    onRelease: upAction,
    onDrag: dragAction,
    cursor: "inherit",
    bounds: ".container"
  });
  
  var position = element._gsTransform;
  
  // Public properties and methods
  var sortable = {
    cell:     cells[index],
    dragger:  dragger,
    element:  element,
    index:    index,
    setIndex: setIndex
  };
  
  TweenLite.set(element, { 
    x: sortable.cell.x, 
    y: sortable.cell.y, 
  });    
  
  function setIndex(index) {
    
    var cell  = cells[index];
    var dirty = position.x !== cell.x || position.y !== cell.y;
    
    sortable.cell = cell;
    sortable.index = index;    
    order.textContent = index + 1;
        
    // Don't layout if you're dragging
    if (!dragger.isDragging && dirty) layout();
  }
    
  function downAction() {
    animation.play();
    this.update();
  }
  
  function dragAction() {
    
    var col = clamp(Math.round(this.x / colSize), 0, totalCols - 1);
    var row = clamp(Math.round(this.y / rowSize), 0, totalRows - 1);
       
    var cell = sortable.cell;    
    var sameCol = col === cell.col;
    var sameRow = row === cell.row;
    
    // Check if position has changed
    if (!sameRow || !sameCol) {
      
      // Calculate the new index
      var index = totalCols * row + col;
      
      // Update the model
      changeIndex(sortable, index, sameRow, sameCol);
    }
  }
    
  function upAction() {
    animation.reverse();
    layout();
  }
  
  function layout() {    
    TweenLite.to(element, 0.3, { 
      x: sortable.cell.x, 
      y: sortable.cell.y
    });  
  }
      
  return sortable;
}

// Changes an elements's position in array
function arrayMove(array, from, to) {
  array.splice(to, 0, array.splice(from, 1)[0]);
}

// Clamps a value to a min/max
function clamp(value, a, b) {
  return value < a ? a : (value > b ? b : value);
}







              
            
!
999px

Console