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

Auto Save

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>
  <section id="top-bar" drop-zone="main" data-sorted="true">
    <div id="clone-container" class="clone-container">
      <div id="scroll-box">
        <div id="tile-container" class="letter-container">

          <div class="tile-wrapper">
            <div class="tile">A</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">B</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">C</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">D</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">E</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">F</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">G</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">H</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">I</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">J</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">K</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">L</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">M</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">N</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">O</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">P</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">Q</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">R</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">S</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">T</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">U</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">V</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">W</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">X</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">Y</div>
          </div>
          <div class="tile-wrapper">
            <div class="tile">Z</div>
          </div>
        </div>
      </div>
    </div>
  </section>

  <section class="content">
    <div id="center-panel" drop-zone="center">
      <div class="clone-container">
        <div class="letter-container"></div>
      </div>
    </div>
  </section>

  <section id="bottom-panel" drop-zone="bottom" data-sorted="true">
    <div class="clone-container">
      <div class="letter-container"></div>
    </div>
  </section>
</main>

              
            
!

CSS

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

* {
  box-sizing: border-box;
}

main {
  height: 100%;
  display: flex;
  flex-direction: column;
  user-select: none;
}

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

#center-panel {
  background: white;
  min-height: 180px;
  min-width: 180px;
  max-width: 420px;
  padding: 10px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}

.content {
  flex: 1;
  display: flex;
  align-items: center;
  flex-direction: row;
  justify-content: center;
}


#bottom-panel {
  background: white;
  padding: 10px;
  display: block;
  min-height: 180px;
  margin: 10px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}

#scroll-box {
  border: 1px solid #ccc;
  white-space: nowrap;
  overflow-x: scroll;
}

#scroll-box .letter-container {
  display: inline-flex;
  flex-wrap: nowrap;
}

.letter-container {
  display: block;
  display: flex;
  flex-wrap: wrap;
}

.tile-wrapper {
  width: 80px;
  height: 80px;
  align-items: center;
  display: flex;
  justify-content: center;
  position: relative;
}

.tile {
  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);
  overflow: hidden;
}

.tile[clone] {
  position: absolute;
  visibility: hidden;
  opacity: 0;
}

.tile.bottom {
  background: cornflowerblue;
}

.tile.center {
  background-color: seagreen;
}

              
            
!

JS

              
                /*
///////////////////////////////////////////////////////////////////////////////////
// DRAGGING AN ELEMENT OUTSIDE A SCROLLABLE CONTAINER
///////////////////////////////////////////////////////////////////////////////////

HOW IT WORKS...

The app starts off by creating clones for each tile using the scope listed below.
When the user clicks on a tile, startDraggable is called, which enables the 
draggable instance to be dragged. When this happens, the real tile is hidden, 
with the clone taking its place. Because the clone is not actually inside the 
overflow container, it can be dragged anywhere. When the dragging stops, the clone
goes back to a hidden state.

SCOPE
- element: the tile element located in the #scroll-box
- wrapper: the element's parent, used to animate the space collapsing around a tile
- clone: a clone of the element that gets appended to the #clone-container
- dropped: is true when the tile is appended to the #drop-panel
- moved: is true when the tile has been dragged outside of its wrapper
- draggable: the draggable instance used by the clone
- x,y: getters that return the start position of the element 
- width: the width of the wrapper

START DRAGGABLE 
- moves the clone to the tile's position
- toggles the visibility between the element and clone
- starts the draggable instance by passing in the pointer event to its startDrag method

ON DRAG
- checks if the clone is outside of the wrapper using hitTest
- if true, it animates the space collapsing where the tile used to be

ON RELEASE
- checks if the clone is inside the drop panel using hitTest
- if it's inside and not already dropped, the wrapper is appended to the panel

MOVE BACK
- animates the wrapper space expanding
- animates the clone moving back to its starting position
- toggles the visibility between the clone and tile




///////////////////////////////////////////////////////////////////////////////////
// CHANGES 
///////////////////////////////////////////////////////////////////////////////////

ATTRIBUTES
Added attributes to elements to create drop zones, which are added to the
dropZones obejct.

<div drop-zone="name" sorted="true>
  <div class="clone-container"">
    <div class="letter-container"></div>
  </div>
</div>

SCOPE
All the letter scopes are added to the letters array. The getPrevious function 
uses the array to return a filtered list of all the letters that are in the same
zone and have a lower index. The array is reverse order, so the first letter
returned in the filterd list will be the previous element. If no results are
returned, then it's either the first element or the zone is empty. If sorted 
is set to false, the getPrevious just returns values to do a normal append to
the letter container.

///////////////////////////////////////////////////////////////////////////////////
*/

var threshold = "60%";
var dropZones = {};
var letters   = [];

$("[drop-zone]").each(function() {

  var zone = $(this);
  var name = zone.attr("drop-zone");

  dropZones[name] = {
    element : zone,
    name    : name,
    sorted  : zone.data("sorted"),
    clones  : zone.find(".clone-container"),
    letters : zone.find(".letter-container")
  };
});

$(".tile").each(function(index) {

  var element = $(this);
  var wrapper = element.parent();
  var offset  = element.position();
  var zone    = dropZones.main;

  var scope = {
    clone   : element.clone().attr("clone", "").prependTo(zone.clones),
    element : element,
    wrapper : wrapper,
    width   : wrapper.outerWidth(),
    height  : wrapper.outerHeight(),
    moved   : false,
    index   : index,
    zone    : zone,
    get x() { return getPosition(wrapper, this.zone.clones, offset).x; },
    get y() { return getPosition(wrapper, this.zone.clones, offset).y; },
    get cloneX() { return getPosition(this.clone, this.zone.clones).x; },
    get cloneY() { return getPosition(this.clone, this.zone.clones).y; },
    get previous() { return getPrevious(this, this.zone); }
  };

  scope.draggable = createDraggable(scope);

  // reversed order
  letters.unshift(scope);

  element.on("mousedown touchstart", scope, startDraggable);
});


// START DRAGGABLE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function startDraggable(event) {

  var letter = event.data;

  // Maak element onzichtbaar
  // Maak kloon zichtbaar en verplaats deze naar de coordinaten van het element
  TweenLite.set(letter.element, { autoAlpha: 0 });
  TweenLite.set(letter.clone, { x: letter.x, y: letter.y, autoAlpha: 1 });

  letter.draggable.startDrag(event.originalEvent);
}

// GET PREVIOUS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getPrevious(letter, zone) {

  if (!zone.sorted) {
    return { target: zone.letters, insert: "appendTo"};
  }

  var values = letters.filter(function(value) {
    return value.zone === letter.zone && value.index < letter.index;
  });

  return {
    target: values[0] ? values[0].wrapper : zone.letters,
    insert: values[0] ? "insertAfter" : "prependTo"
  };
}

// CREATE DRAGGABLE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createDraggable(letter) {

  var clone   = letter.clone;
  var wrapper = letter.wrapper;

  letter.draggable = new Draggable(clone, {
    onPress   : setActive,
    onDrag    : collapseSpace,
    onRelease : dropTile
  });

  return letter.draggable;
  ///////

  function setActive() {
    TweenLite.to(clone, 0.15, { scale: 1.2, autoAlpha: 0.75 });
  }

  function collapseSpace() {
    if (!letter.moved) {
      if (!this.hitTest(wrapper)) {
        letter.moved = true;
        TweenLite.to(wrapper, 0.3, { width: 0, height: 0 });
      }
    }
  }

  function dropTile() {

    var name = undefined;
    var self = this;

    $.each(dropZones, function(key, zone) {

      if (self.hitTest(zone.element, threshold) && letter.zone !== zone) {

        name = "tile " + zone.name;
        letter.zone = zone;

        // Get the previous element and the insert method
        var previous = letter.previous;
        wrapper[previous.insert](previous.target);

        // Position the clone inside its new container
        TweenLite.set(clone, { x: letter.cloneX, y: letter.cloneY });
        zone.clones.prepend(clone);
      }
    });

    moveBack(letter, name);
  }
}

// MOVE BACK ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function moveBack(letter, className) {

  var clone   = letter.clone;
  var element = letter.element;
  var wrapper = letter.wrapper;

  TweenLite.to(wrapper, 0.2, { width: letter.width, height: letter.height });
  TweenLite.to(clone, 0.3, { scale: 1, autoAlpha: 1, x: letter.x, y: letter.y, onComplete: done, delay: 0.02 });
  
  if (className) TweenLite.to([element, clone], 0.3, { className: className });

  function done() {
    letter.moved = false;
    TweenLite.set(clone, { autoAlpha: 0 });
    TweenLite.set(element, { autoAlpha: 1 });
  }
}

// GET POSITION :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getPosition(target1, target2, offset) {

  var position1 = target1.offset();
  var position2 = target2.offset();

  offset = offset || { left: 0, top: 0 };

  return {
    x: position1.left - position2.left + offset.left,
    y: position1.top  - position2.top  + offset.top
  };
}






              
            
!
999px

Console