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

              
                <main>
  
  <header class="main-header">
    <button id="check-button">Check</button>
    <button id="reset-button">Reset</button>
  </header>
  
  <section class="board">
  
    <div class="layer drop-layer">
      <div class="tile drop-tile" data-value="alpha">Alpha</div>
      <div class="tile drop-tile" data-value="bravo">Bravo</div>
      <div class="tile drop-tile" data-value="charlie">Charlie</div>
      <div class="tile drop-tile" data-value="delta">Delta</div>
    </div>

    <div class="layer drop-layer">
      <div class="tile drag-tile" data-value="alpha">Alpha</div>
      <div class="tile drag-tile" data-value="bravo">Bravo</div>
      <div class="tile drag-tile" data-value="charlie">Charlie</div>
      <div class="tile drag-tile" data-value="delta">Delta</div>
    </div>

  </section>
  
</main>

              
            
!

CSS

              
                * {
  box-sizing: border-box;
  position: relative;
}

body {
  background: #222;
}

button {
  padding: 8px 10px;
  margin: 12px 0 12px 12px;
  cursor: pointer;
}

main {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.main-header {
  background: #333;
  z-index: 1;
}

.board {
  width: 100%;
  display: flex;
  flex: 1;
  z-index: 0;
}

.tile {
  width: 160px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  
  background: dodgerblue;
  color: rgba(255,255,255,0.9);
  font-weight: 700;
  
  // margin: 10px;
  position: absolute;
  border-radius: 2px;
  border: 6px solid transparent;
}

.drag-tile {
  background: #1976d2;
  box-shadow: 
    0 2px 4px -1px rgba(0,0,0,.2), 
    0 4px 5px 0 rgba(0,0,0,.14), 
    0 1px 10px 0 rgba(0,0,0,.12);
}

.drop-tile {
  background: #555;
}

.layer {
  position: absolute;
  top: 0;
  left: 0;
}

.drop-layer {
  z-index: 0;
}

.drag-layer {
  z-index: 1;
}

.hitting {
  border-color: rgba(255,255,255,0.35);
}

.correct {
  background: #4caf50;
}

.wrong {
  background: #c62828;
}
              
            
!

JS

              
                console.clear();

var pad = 20;
var threshold = "50%";

var checkButton = document.querySelector("#check-button");
var resetButton = document.querySelector("#reset-button");

var dragElements = document.querySelectorAll(".drag-tile");
var dropElements = document.querySelectorAll(".drop-tile");

var dragTiles = Array.prototype.map.call(dragElements, createDragTile);
var dropTiles = Array.prototype.map.call(dropElements, createDropTile);

checkButton.addEventListener("click", checkTiles);
resetButton.addEventListener("click", resetTiles);

function checkTiles() {
  
  for (var i = 0; i < dragTiles.length; i++) {
    
    var tile = dragTiles[i];
    
    if (!tile.parent) {
      continue;
    }
    
    var className = tile.value === tile.parent.value ? "correct" : "wrong";    
    tile.element.classList.add(className);    
  }
}

function resetTiles() {
  
  for (var i = 0; i < dragTiles.length; i++) {
    
    var tile = dragTiles[i];
    
    if (tile.parent) {
      tile.parent = tile.parent.child = null;
    }
    
    tile.element.classList.remove("correct", "wrong", "hitting");
    
    TweenLite.to(tile.element, 0.3, {
      x: 0,
      y: 0
    });
  }
}

function createDragTile(element, index) {
  
  TweenLite.set(element, {
    left: pad,
    top: pad + index * (pad + element.offsetHeight)
  });
  
  var draggable = new Draggable(element, {
    bounds: ".board",
    onDragStart: onDragStart,
    onDrag: onDrag,
    onDragEnd: onDragEnd
  });
  
  var tile = {
    element: element,
    parent: null,   
    value: element.dataset.value
  };
  
  function onDragStart() {    
    element.classList.remove("correct", "wrong");
  }
  
  function onDrag() {
    
    var parent = tile.parent;
        
    if (parent) {
            
      if (this.hitTest(parent.element, threshold)) {
        
        // exit the function
        // tile is still hitting parent, so no need to proceed any further.
        return;
      }
      
      // tile is no longer hitting parent, so clear any references between the two
      parent = tile.parent = parent.child = null;      
    }
    
    for (var i = 0; i < dropTiles.length; i++) {
      
      var dropTile = dropTiles[i];
      
      if (dropTile.child) {
        
        // continue to next loop iteration
        // drop tile already has a child, so no need to proceed any further
        continue;
      }
      
      if (this.hitTest(dropTile.element, threshold)) {
        
        // we hit an empty drop tile, so link the two together and exit the function
        tile.parent = dropTile;
        dropTile.child = tile;
        element.classList.add("hitting");
        return;
      }
    }
    
    // if we made it this far, we're not hitting an empty drop tile
    element.classList.remove("hitting");
  }
  
  function onDragEnd() {
    
    var x = 0;
    var y = 0;
    
    // move to parent
    if (tile.parent) {
      
      var rect1 = element.getBoundingClientRect();
      var rect2 = tile.parent.element.getBoundingClientRect();
      
      x = "+=" + (rect2.left - rect1.left);
      y = "+=" + (rect2.top - rect1.top);
    }
    
    TweenLite.to(element, 0.3, {
      x: x,
      y: y
    });
    
  }
  
  return tile;
}

function createDropTile(element, index) {
  
  TweenLite.set(element, {
    left: pad + 3 * element.offsetWidth,
    top: pad + index * (pad + element.offsetHeight)
  });
  
  var tile = {
    element: element,
    child: null,   
    value: element.dataset.value
  };
  
  return tile;
}

              
            
!
999px

Console