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

              
                <div id="demo">
  <div id="letter-box">
    <div id="container">
      <div id="scroll-box">      
        <div class="letter"><div>A</div></div>
        <div class="letter"><div>B</div></div>
        <div class="letter"><div>C</div></div>
        <div class="letter"><div>D</div></div>
        <div class="letter"><div>E</div></div>
        <div class="letter"><div>F</div></div>
        <div class="letter"><div>G</div></div>
        <div class="letter"><div>H</div></div>
        <div class="letter"><div>I</div></div>
        <div class="letter"><div>J</div></div>
        <div class="letter"><div>K</div></div>
        <div class="letter"><div>L</div></div>
        <div class="letter"><div>M</div></div>
        <div class="letter"><div>N</div></div>
        <div class="letter"><div>O</div></div>
        <div class="letter"><div>P</div></div>
        <div class="letter"><div>Q</div></div>
        <div class="letter"><div>R</div></div>
        <div class="letter"><div>S</div></div>
        <div class="letter"><div>T</div></div>
        <div class="letter"><div>U</div></div>
        <div class="letter"><div>V</div></div>
        <div class="letter"><div>W</div></div>
        <div class="letter"><div>X</div></div>
        <div class="letter"><div>Y</div></div>
        <div class="letter"><div>Z</div></div>      
      </div>
    </div>
  </div>
  <div class="middle"></div>
  <div id="end-zone">
    <header>DRAG ITEMS HERE</header>
  </div>
</div>


              
            
!

CSS

              
                body{
  background-color: #eee;
  height: 100vh;
  margin: 0;
  position: relative;
  overflow: hidden;
}

* { box-sizing: border-box; }

#demo {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  flex-direction: column;  
  user-select: none;
}

#end-zone {
  min-height: 200px;
  background: white;
  padding: 10px;
  margin: 10px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
  
  & header {
    padding-left: 10px;
    font-size: 12px;
  }
}

.middle {
  flex: 1;
}

#letter-box {
  margin: 10px;
  padding: 10px;
  background-color: white;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);  
  min-height: 119px;
}

#container {
  border: 1px solid #ccc;
  position: relative;  
}

#scroll-box {
  white-space: nowrap;
  overflow-x: scroll;  
}

.letter {
  width: 80px;
  height: 80px;  
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.letter > div,
.cloned-letter {
  background-color: mediumvioletred;
  color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;  
  position: relative;
  width: 60px;
  height: 60px;
  cursor: move;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.5);
  
  &.placed {
    background: cornflowerblue;
  }
}

.cloned-letter {
  position: absolute;
}
              
            
!

JS

              
                NodeList.prototype.forEach = Array.prototype.forEach;

var container = document.getElementById("container");
var endZone   = document.getElementById("end-zone");
var letters   = document.querySelectorAll(".letter > div");
var width     = document.querySelector(".letter").offsetWidth;
var bounds    = container.getBoundingClientRect();
var threshold = "50%";

// Create clones of each letter
letters.forEach(function(element, index) {
  
  var clone = element.cloneNode(true);
  var scope = { 
    wrapper : element.parentNode, 
    element : element, 
    clone   : clone,     
    placed  : false, 
    moved   : false 
  };
    
  clone.className = "cloned-letter";
  element.onmousedown = startDraggable;  
  
  TweenLite.set(clone, { autoAlpha: 0 });  
  container.insertBefore(clone, container.firstChild);    
    
  // Move the clone to the proper position and start draggable
  function startDraggable(event) {
    var position  = element.getBoundingClientRect();
    var draggable = scope.draggable || createDraggable(scope);  
        
    scope.x = position.left - bounds.left - 1;
    scope.y = position.top  - bounds.top  - 1;
    
    TweenLite.set(element, { scale: 0, autoAlpha: 0 });
    TweenLite.set(clone, { x: scope.x, y: scope.y, zIndex: 1000, autoAlpha: 1 });
    
  	draggable.startDrag(event);
  }
});

function createDraggable(scope) {
    
  var clone   = scope.clone;
  var wrapper = scope.wrapper;  
  
  scope.draggable = new Draggable(clone, {
    onPress   : changeStyle,
    onDrag    : checkPosition,
    onRelease : snapPosition
  });
  
  return scope.draggable;
  
  function changeStyle() {
    TweenLite.to(clone, 0.15, { scale: 1.2, autoAlpha: 0.75 });
  }
  
  function checkPosition() {
    if (!scope.moved) {
      if (!this.hitTest(wrapper)) {
        scope.moved = true;
        TweenLite.to(wrapper, 0.3, { width: 0 });
      }
    }
  }
  
  function snapPosition() {
    this.hitTest(endZone, threshold) && !scope.placed ? addToZone(scope) : moveBack(scope);
  }
}

function addToZone(scope) {  
  scope.placed = true;
  
  var clone   = scope.clone;
  var wrapper = scope.wrapper;
  var element = scope.element;
    
  endZone.appendChild(wrapper);
  TweenLite.set(wrapper, { width: width });
        
  var clonePosition   = clone.getBoundingClientRect();
  var wrapperPosition = wrapper.getBoundingClientRect();  
  var x = clonePosition.left - wrapperPosition.left;
  var y = clonePosition.top  - wrapperPosition.top;
  
  TweenLite.set(clone, { autoAlpha: 0, scale: 1 });  
  TweenLite.set(element, { x: x, y: y, scale: 1.2, autoAlpha: 0.75 });  
  TweenLite.to([element, clone], 0.3, { className: "+=placed" });
  TweenLite.to(element, 0.3, { scale: 1, autoAlpha: 1, x: 0, y: 0, onComplete: done });
  
  function done() {
    scope.moved = false;
  }
}

function moveBack(scope) {
    
  var clone   = scope.clone;
  var element = scope.element;
  var wrapper = scope.wrapper;  
  
  TweenLite.to(wrapper, 0.2, { width: width });
  TweenLite.to(clone, 0.3, { scale: 1, autoAlpha: 1, x: scope.x, y: scope.y, onComplete: done });
  
  function done() {
    scope.moved = false;
    TweenLite.set(clone, { autoAlpha: 0, scale: 1 });
    TweenLite.set(element, { scale: 1, autoAlpha: 1 });
  }
}





              
            
!
999px

Console