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

              
                <main>
  <section id="top-bar">
    <div id="clone-container">
      <div id="scroll-box">
        <div id="tile-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">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"></section>
  <div id="drop-panel">
    <img src="http://www.wloman.com/admin-panel/images/freshproduct/1418472463beefTom.jpg"/>
  </div>
</main>
              
            
!

CSS

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

* {
  box-sizing: border-box;
}

main {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  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;
}

#drop-panel {
  display: block;
  background: white;
  padding: 10px;
  margin-left: auto;
  margin-right: auto;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}

.content {
  flex: 1;
}

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

#tile-container {
  display: inline-flex;
}

.tile-wrapper {
  width: 80px;
  height: 80px;
  display: inline-flex;
  align-items: center;
  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;
  opacity: 0;
  visibility: hidden;
}
.tile.dropped {
  background: cornflowerblue;
}
              
            
!

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

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

var container  = $("#clone-container");
var topbar     = $("#top-bar");
var tcontainer = $("#tile-container");
var scrollBox  = $("#scroll-box");
var dropPanel  = $("#drop-panel");
var tiles      = $(".tile");
var threshold  = "60%";

tiles.each(function(index) {

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

  var scope = {
    clone      : element.clone().addClass("clone").prependTo(container),
    element    : element,
    wrapper    : wrapper,
    width      : wrapper.outerWidth(),
    dropped    : false,
    moved      : false,
    inSoep     : false,
    startIndex : index,
    get x() { return getPosition(wrapper, offset).x; },
    get y() { return getPosition(wrapper, offset).y; }
  };

  scope.draggable = createDraggable(scope);

  element.bind("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);
}

// 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 });
      }
    }
  }

  function dropTile() 
  {
    var className = undefined;

    console.log("letter dropped: " + letter.dropped);
    if (this.hitTest(dropPanel, threshold) && !letter.dropped) 
    {
        console.log("erin");
        dropPanel.append(wrapper);
        letter.inSoep = true;
        letter.dropped = true;
        className = "+=dropped";
    }

    if (this.hitTest(topbar, threshold) && letter.inSoep)
    {
      wrapper.insertBefore(tcontainer.children()[letter.startIndex]);
      
      letter.inSoep = false;
      letter.dropped = false;
    }
    moveBack(letter, className);
  }
}

// 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 });
  TweenLite.to(clone, 0.3, { scale: 1, autoAlpha: 1, x: letter.x, y: letter.y, onComplete: done });

  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(wrapper, offset) {

  var position1 = wrapper.offset();
  var position2 = container.offset();

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

Console