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 class="page">
  <div class="interactive-area">
    <div class="response__wrapper">

      <div class="response">

        <div class="response__label font__body--m">
          <p>Drop Spot 3</p>

        </div>

        <div class="drop-spot"></div>

      </div>

      <div class="response">

        <div class="response__label font__body--m">
          <p>Drop spot 2</p>

        </div>

        <div class="drop-spot"></div>

      </div>

      <div class="response">

        <div class="response__label font__body--m">
          <p>Drop spot 3</p>

        </div>

        <div class="drop-spot"></div>

      </div>

    </div>

    <div class="drag-item__wrapper" data-ui="dragItemBank">

      <div class="drag-item">
        Drag Item 1
        <div class="box"></div>
      </div>

      <div class="drag-item">
        Drag Item 2
        <div class="box"></div>
      </div>

    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  background: #d4d4d4;
}

.page {
  position: relative;
}

.interactive-area {
  display: flex;
    width: 60rem;
    margin: 2.3rem auto;
}

.response__wrapper {
  width: 86%;
}

.response {
  display: flex;
  margin: 1rem;
}

.response__label {
  margin-right: 20px;
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 4.5rem;
  background: white;
}

p:last-child {
  margin-bottom: 0;
}

.drop-spot {
  min-width: 160px;
  height: 4.5rem;
  border: 4px dashed #c0c0c0;
  background: white;
}

.drop-spot.is--over {
   background:orange;
}



.drag-item__wrapper {
  margin: 0;
    padding: 0;
    border: 0;
    box-sizing: border-box;
    font-weight: inherit;
    width: 160px;
    margin-top: 1rem;
}

.drag-item-placeholder,
.drag-item {
      position: relative;
      margin-bottom: 1rem;
      color: #fff;
      background-color: #2E2E38;
      width: 160px;
      height: 4.5rem;
      display: flex;
      align-items: center;
      justify-content: center;    
  }

.drag-item-placeholder {
  opacity: 0.3;
}



.drag-item.is--placed {
  top: -4px !important;
  left: -4px !important;
}

.box {
  width: 50px;
  height: 50px;
  background: dodgerblue;
}
              
            
!

JS

              
                console.clear();

(function() {
    
  this.$pageEl = $(".page");
  this.defaultDuration = 0.3;

  this.onDragPress = function(e) {
    console.log("onDragPress", arguments);
  };

  // - all dragging and movement happens inside this.$pageEl with absolute positioning (temporarily)
  // - onDragStart, set $dragItem to absolute positioning and move into this.$pageEl at the same coordinates so it doesn't jump
  // - if starting at the bank, insert a temporary placeholder item to maintain its space
  // - if sending back to bank, get coordinates of placeholder relative to this.$pageEl, animate to those coords,
  //   insert $dragItem back into bank at placeholder's position, remove placeholder and set positioning back to relative

  onDragStart = function(target, e) {
    console.log("onDragStart", arguments);
    if (!$(e.target).hasClass("drag-item")) {
      console.warn(
        "Drag item did not have class drag-item. Classes present:",
        $(e.target).attr("class")
      );
      // return;
    }

    // this.$currentDragItem = $(e.target);
    this.$currentDragItem = $(target);

    var coords = this.localToLocal(this.$currentDragItem, this.$pageEl);

    // if in the bank, insert a placeholder
    if (!this.$currentDragItem.data("placeholder"))
      this.insertBankPlaceholder(this.$currentDragItem);

    this.clearData(this.$currentDragItem);

    TweenLite.set(this.$currentDragItem, {
      position: "absolute",
      left: coords.x,
      top: coords.y
    });
    Draggable.get(this.$currentDragItem).update();
    this.$pageEl.append(this.$currentDragItem);

    Draggable.get(this.$currentDragItem).update();
  };

  this.onDrag = function(e) {
    $(".drop-spot").removeClass("is--over");
    this.$currentDropSpot = this.getDropSpotMostOver();

    if (this.$currentDropSpot) this.$currentDropSpot.addClass("is--over");
  };

  this.onDragEnd = function() {
    console.log("onDragEnd", arguments);
    $(".drop-spots").removeClass("is--over");
    if (this.$currentDropSpot) {
      this.sendDragItemToDropSpot(this.$currentDragItem, this.$currentDropSpot);
    } else this.sendDragItemBackToBank(this.$currentDragItem);
  };

  // send item back to dropspot
  this.sendDragItemBackToBank = function($dragItem, duration) {
    duration = _.defaultTo(duration, this.defaultDuration);

    var $placeholder = $dragItem.data("placeholder");
    var placeholderCoords = this.localToLocal($placeholder, this.$pageEl);
    var dragItemCoords = this.localToLocal($dragItem, this.$pageEl);

    TweenLite.set(this.$currentDragItem, {
      position: "absolute",
      left: dragItemCoords.x,
      top: dragItemCoords.y
    });
    this.$pageEl.append($dragItem);
    TweenLite.fromTo(
      $dragItem,
      duration,
      { left: dragItemCoords.x, top: dragItemCoords.y },
      {
        left: placeholderCoords.x,
        top: placeholderCoords.y,
        callbackScope: this,
        onComplete: function() {
          TweenLite.set($dragItem, {
            position: "relative",
            clearProps: "left,top"
          });
          $dragItem.insertBefore($placeholder);
          $placeholder.remove();
          $dragItem.removeData("placeholder");

          this.clearData($dragItem);
          // this.manageSubmitBtn();
        }
      }
    );
  };

  this.sendDragItemToDropSpot = function($dragItem, $dropSpot, duration) {
    duration = _.defaultTo(duration, this.defaultDuration);
    var coords = this.localToLocal($dragItem, $dropSpot);

    this.clearData($dragItem);

    // might want to save this in models instead?
    $dragItem.data("$dropSpot", $dropSpot);
    $dragItem.addClass("is--placed");

    // might want to save this in models instead?
    $dropSpot.data("$dragItem", $dragItem);
    $dropSpot.addClass("is--populated");

    // this.manageSubmitBtn();

    $dropSpot.append($dragItem);
    TweenLite.fromTo(
      $dragItem,
      duration,
      { position: "relative", left: coords.x, top: coords.y },
      { left: 0, top: 0 }
    );
  };

  this.insertBankPlaceholder = function($dragItem) {
    var $placeholder = $dragItem.clone();
    $placeholder.css("cursor", "default");

    $placeholder
      .removeClass("drag-item")
      .addClass("drag-item-placeholder placeholder");
    $placeholder.insertBefore($dragItem);

    $dragItem.data("placeholder", $placeholder);
  };

  // utility functions
  this.getDropSpotMostOver = function() {
    var overlapArea;
    var maxOverlapArea = 0;
    var winningEl;
    $(".drop-spot").each(
      _.bind(function(index, el) {
        if ($(el).data("$dragItem")) return;

        overlapArea = this.getOverlapArea(el, this.$currentDragItem);
        if (overlapArea > 0 && overlapArea >= maxOverlapArea) {
          maxOverlapArea = overlapArea;
          winningEl = el;
        }
      }, this)
    );

    return winningEl ? $(winningEl) : undefined;
  };

  this.getOverlapArea = function(element1, element2) {
    if (element1.jquery) element1 = element1[0]; // convert from jquery to dom element
    if (element2.jquery) element2 = element2[0]; // convert from jquery to dom element

    var rect1 = element1.getBoundingClientRect();
    var rect2 = element2.getBoundingClientRect();

    var xOverlap = Math.max(
      0,
      Math.min(rect1.right, rect2.right) - Math.max(rect1.left, rect2.left)
    );
    var yOverlap = Math.max(
      0,
      Math.min(rect1.bottom, rect2.bottom) - Math.max(rect1.top, rect2.top)
    );

    return xOverlap * yOverlap;
  };

  localToLocal = function(element1, element2) {
    if (element1.jquery) element1 = element1[0]; // convert from jquery to dom element
    if (element2.jquery) element2 = element2[0]; // convert from jquery to dom element

    var element1Rect = element1.getBoundingClientRect();
    var element2Rect = element2.getBoundingClientRect();

    var xVal = element1Rect.left - element2Rect.left;
    var yVal = element1Rect.top - element2Rect.top;
    return { x: xVal, y: yVal };
  };

  (this.clearData = function($dragItem) {
    var $curDropSpot = $dragItem.data("$dropSpot");
    if ($curDropSpot) {
      $curDropSpot.removeData("$dragItem").removeClass("is--populated");
    }

    $dragItem.removeData("$dropSpot").removeClass("is--placed");
  })
    // (this.draggables = Draggable.create(".drag-item", {
    //   type: "top,left",
    //   bounds: document.getElementsByTagName("body")[0],
    //   onPress: this.onDragPress,
    //   onRelease: this.onDragRelease,
    //   onDragStart: this.onDragStart,
    //   onDragEnd: this.onDragEnd,
    //   onDrag: this.onDrag,
    //   callbackScope: this,
    //   allowContextMenu: true
    // }));
  
  var dragItems = Array.from(document.querySelectorAll(".drag-item"));
    
  this.draggables = dragItems.map((item) => {
    
    return new Draggable(item, {
      type: "top,left", 
      bounds: document.body,
      onPress: this.onDragPress.bind(this, item),
      // onRelease: this.onDragRelease.bind(this, item),
      onDragStart: this.onDragStart.bind(this, item),
      onDragEnd: this.onDragEnd.bind(this, item),
      onDrag: this.onDrag.bind(this, item),
      allowContextMenu: true
    });
  });
  
})();

              
            
!
999px

Console