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>
  <form>
    <center>
      <label>Description</label>
      <input type="text" id="description" value="AA" />
      <input type="button" id="btnSubmit" value="Submit" /t>
      <center>

        <section id="top-bar">
          <div id="scroll-box">
            <div id="zone-1" class="drop-zone">
              
              <div class="tile" data-image-id="0" data-name="cat" >0</div>
              <div class="tile" data-image-id="1" data-name="dog" >1</div>
              <div class="tile" data-image-id="2" data-name="cow" >2</div>
              <div class="tile" data-image-id="3" data-name="pig" >3</div>
              <div class="tile" data-image-id="4" data-name="owl" >4</div>
              
            </div>
          </div>
        </section>

        <section id="bottom-panel">
          <div id="zone-3" class="drop-zone">
            
            <div class="tile" data-image-id="101" data-name="foo" data-x="60"  data-y="20">101</div>
            <div class="tile" data-image-id="102" data-name="bar" data-x="120" data-y="110">102</div>
            <div class="tile" data-image-id="103" data-name="baz" data-x="180" data-y="20">103</div>
            
          </div>
        </section>


      </center>
    </center>

    <section class="json-container">
      <pre></pre>
    </section>

  </form>
</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 auto;
  padding: 10px;
  background-color: white;
  min-height: 139px;
  height: 139px;
  width: 600px;
  border: 1px solid #aaa;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3);
}

#bottom-panel {
  background: white;
  display: block;
  min-height: 180px;
  margin: 10px auto;
  height: 280px;
  width: 500px;
  border: 1px solid #aaa;
  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 .drop-zone {
  display: inline-flex;
  flex-wrap: nowrap;
}

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

.tile, tile-dropped {
  // background-color: mediumvioletred;
  color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  position: relative;
  width: 60px;
  height: 90px;
  cursor: move;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.5);
  overflow: hidden;
  
  top: 0;
  left: 0;
}

.drop-zone {
  display: block;
  display: flex;
  flex-wrap: wrap;
  position: relative;
  height: 100%;   
  width: 100%;
  
  visibility: hidden;
}

#zone-1 {
   
  .tile {    
    position: relative;
    margin: 10px 10px 10px 0;
    background-color: mediumvioletred;
    
    &:first-child {
      margin-left: 10px;
    }
  } 
}

#zone-3 {
   
  .tile, .tile-dropped {    
    position: absolute;
    background: blue;
  } 
}

.tile.clone {
  position: absolute;
  top: 0;
  left: 0;
  background-color: green;
}

#btnSubmit{
  padding: 10px;
  margin-top: 50px;
}

.json-container {
  border: 1px solid black;
  margin: 15px;
}

pre {
  font-size: 13px;
  margin: 20px;  
}

              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

var body  = $("body")[0];
var ease  = Back.easeOut.config(0.5);
var tiles = [];
var zones = [];

var zoneData = [
  { id: "zone-1", grid: true,  color: "#C71585", threshold: "60%",  sortProp: "imageId" },
  { id: "zone-3", grid: false, color: "#6495ED", threshold: "100%", sortProp: null }
];

// Camel case names of data props to use
// ex. data-image-id => imageId
var tileData = ["x", "y", "imageId", "name"];

//
// DROP ZONE
// ========================================================================
class DropZone {
  
  constructor(config) {
        
    $.extend(this, config);
    
    this.tiles   = [];    
    this.element = $("#" + this.id)[0];
    this.parent  = this.element.parentNode;  
    
    $("#" + this.id + " .tile").toArray().forEach(element => this.initTile(element));    
    
    TweenLite.set(this.element, { autoAlpha: 1 });
  }
  
  get width()  { return this.element.offsetWidth;  }
  get height() { return this.element.offsetHeight; }
   
  initTile(element) {
        
    var config = tileData.reduce((tile, prop) => {      
      var value  = element.dataset[prop];
      var number = parseFloat(value);      
      tile[prop] = isNaN(number) ? value : number;  
      return tile;
    }, {});
    
    var tile = new Tile(element, this, config);
  }
    
  addTile(tile) {
    
    var index = this.tiles.indexOf(tile);
    if (index > -1) return;
    
    this.tiles.push(tile);
    this.element.appendChild(tile.element);
    this.updateTiles();
    tile.updatePosition(this.grid);
    this.layoutTiles();    
  }
  
  removeTile(tile) {
    
    var index = this.tiles.indexOf(tile);    
    if (index < 0) return;
    
    this.tiles.splice(index, 1);
    this.element.removeChild(tile.element);
    this.updateTiles();
    this.layoutTiles();
  }
    
  updateTiles() {
    
    if (this.sortProp) {
      this.sortTiles();
      this.tiles.forEach(tile => this.element.appendChild(tile.element));
    }
  }
  
  layoutTiles() {
    this.tiles.forEach(tile => tile.layout());
  }
  
  sortTiles() {
    
    this.tiles.sort((left, right) => {
      var a = left[this.sortProp];
      var b = right[this.sortProp];
      if (a !== b) {
        if (a > b || a === void 0) return 1;
        if (a < b || b === void 0) return -1;
      }
      return left.index - right.index;
    });
  } 
}

//
// TILE
// ========================================================================
class Tile {
   
  constructor(element, zone, config) {    
        
    this._x = 0;
    this._y = 0;
    
    this.xGrid = 0;
    this.yGrid = 0;
        
    this.xPercent = 0;
    this.yPercent = 0;
      
    this.element = element;
    this.clone   = this.element.cloneNode(true);        
    this.clone.classList.add("clone");
    
    this.zone     = zone;
    this.lastZone = this.zone; 
    this.isGrid   = this.zone.grid;
    
    $.extend(this, config);               
      
    this.transform = TweenLite.set(this.element, { x: "+=0" }).target._gsTransform;  
    
    this.draggable = new Draggable(this.clone, {
      onDrag: this.onDrag,
      onRelease: this.stopDraggable,
      callbackScope: this
    }).disable();
    
    this.zone.addTile(this);
        
    $(this.element).on("mousedown touchstart", this.startDraggable.bind(this));
  }
  
  get offsetX() { return this.element.offsetLeft; }
  get offsetY() { return this.element.offsetTop; }
    
  get left() { return this.element.style.left; }
  set left(left) { this.element.style.left = left; }
  
  get top() { return this.element.style.top; }
  set top(top) { this.element.style.top = top; }
  
  get x() { return this._x; }
  set x(x = 0) {
        
    if (!this.isGrid) {
      var width = (this.zone.width || this.lastZone.width);
      this.left = this.xPercent = (x / width * 100) + "%";      
    } else {
      this.left = this.xPercent = 0;
    }
    
    this.element.dataset.x = this._x = x;
  }
  
  
  get y() { return this._y; }
  set y(y = 0) {
    
    if (!this.isGrid) {      
      var height = (this.zone.height || this.lastZone.height);      
      this.top = this.yPercent = (y / height * 100) + "%";      
    } else {
      this.top = this.yPercent = 0;
    }
    
    this.element.dataset.y = this._y = y;
  }
  
  startDraggable(event) {    
    event.preventDefault();
        
    var position = getPosition(this.element);   
    
    body.appendChild(this.clone);             
    TweenLite.set(this.clone, { x: position.x, y: position.y, backgroundColor: this.zone.color });         
    TweenLite.set(this.element, { autoAlpha: 0, zIndex: Draggable.zIndex + 1 });
            
    this.draggable.enable();
    this.draggable.update();
    this.draggable.startDrag(event);
  }
  
  stopDraggable(event) {
    
    this.draggable.disable();    
    this.left = this.top = 0;
    
    var orphaned = zones.every(zone => !this.hitTest(zone));       
    if (orphaned && !this.isGrid) {
      this.left = this.xPercent;
      this.top  = this.yPercent;
    }
    
    if (!this.zone) {     
      
      this.zone = this.lastZone;
      
      zones.forEach(zone => {        
        if (this.hitTest(zone)) this.zone = zone;
      });      
    }
       
    this.zone.addTile(this);    
    
    var offset = this.getOffset();        
    var config = {
      backgroundColor: this.zone.color,
      onComplete: () => {
        body.removeChild(this.clone);
        TweenLite.set(this.element, { autoAlpha: 1 });
      }
    };
        
    if (!this.isGrid && !orphaned) {    
      
      this.x = offset.x;
      this.y = offset.y;    
      
    } else {
           
      config.x = "-=" + offset.x;
      config.y = "-=" + offset.y;     
      
      if (this.isGrid) {
        this.x = this.y = 0;
      }
    }
            
    TweenLite.to(this.clone, 0.3, config);
  }
    
  layout() {
    
    if (!this.isGrid) return;
        
    var lastX = this.gridX;
    var lastY = this.gridY;
    
    this.gridX = this.offsetX;
    this.gridY = this.offsetY;
    
    var dx = this.transform.x + lastX - this.gridX;
    var dy = this.transform.y + lastY - this.gridY; 
    
    TweenLite.fromTo(this.element, 0.5, { x: dx, y: dy }, { x: 0, y: 0, ease: ease });
  }
  
  onDrag() {
    
    if (this.zone) {
      
      if (this.zone && !this.hitTest(this.zone, 0)) {
        this.lastZone = this.zone;
        this.zone.removeTile(this);
        this.zone = null;
      }
    }    
  }
   
  getOffset() {
    
    var position1 = getPosition(this.clone);
    var position2 = getPosition(this.zone.element);
    
    return {
      x: position1.x - position2.x - this.offsetX,
      y: position1.y - position2.y - this.offsetY
    };
  }
  
  hitTest(zone, threshold) {
    var overlap = threshold != null ? threshold : zone.threshold;
    return Draggable.hitTest(this.clone, zone.parent, overlap);
  }
  
  updatePosition(isGrid) {
        
    this.isGrid = isGrid;
    this.gridX  = this.offsetX;
    this.gridY  = this.offsetY;    
  }
}

//
// GET POSITION
// ========================================================================
function getPosition(element) {
    
  if (element.length) element = element[0];
  
  var rect = element.getBoundingClientRect();
  var doc  = document.documentElement;
  
  var scrollLeft = window.scrollX || doc.scrollLeft || body.scrollLeft || 0;
  var scrollTop  = window.scrollY || doc.scrollTop  || body.scrollTop  || 0;
  
  var clientLeft = doc.clientLeft || body.clientLeft || 0;
  var clientTop  = doc.clientTop  || body.clientTop  || 0;
  
  return {
    x: rect.left + scrollLeft - clientLeft,
    y: rect.top  + scrollTop  - clientTop
  };
}

//
// RUN
// ========================================================================
zones = zoneData.map(zone => new DropZone(zone));

function toJSON(description) {
  
  var zone = zones[1];
    
  var data = {
    description: $("input#description").val(),
    total: zone.tiles.length,
    tiles: zone.tiles.map(tile => {
      return {
        imageId: tile.imageId,
        name: tile.name,        
        x: tile.x,
        y: tile.y
      };
    })
  };
  
  $("pre").text(JSON.stringify(data, null, 2));
}

$("#btnSubmit").click(toJSON);

$("___#btnSubmit").click(function() {
  var description = $("input#description").val();
  if (description == '') {
    alert('Please fill in a description!');
    $('input#description').focus();
  } else {
    var html = '<form action="" method="post">';
    html += '<input type="text" name="description" value="' + description + '" />';
    var offsettl = $("#topleft").offset();
    html += '<input type="text" name="topleft" value="' + offsettl.left + ';' + offsettl.top + '" />'
    var counter = 0;
    $('#zone-3').children(".tile").each(function() {
      counter++;
      var id = $(this).attr('data-id');
      alert(id);
      var offset = $(this).offset();
      html += '<input type="text" name="' + id + '" value="' + offset.left + ';' + offset.top + '" />'
    });
    html += '</form>';
    var form = $(html);
    // $('body').append(form);
    alert("You dropped " + counter + " item(s)");
  }
  //form.submit()
});





              
            
!
999px

Console