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

              
                <div id="demo">
  <div class="box dragger1">Drag 1</div>
  <div class="box dragger2">Drag 2</div>
  
    <div id="target1" class="target">Target 1</div>
    <div id="target2" class="target">Target 2</div>
    <div id="target3" class="target">Target 3</div>
</div>

              
            
!

CSS

              
                body{
 font-family:sans-serif;
}
h1 {
  margin: 5px 0;
}
h3 {
  margin:0;
  font-size:16px;
}
.box {
	background-color: #91e600;
	text-align: center;
	font-family: Asap, Avenir, Arial, sans-serif;
	width: 100px;
	height: 50px;
	line-height: 50px;
	color: black;
	position: absolute;
	border-radius: 10px;
}

.dragger1{
	  top:10px;
  left:10px;
}
.dragger2{
	  top:70px;
  left:10px;
}

.target {
	background-color: #CCC;
	text-align: center;
	font-family: Asap, Avenir, Arial, sans-serif;
	width: 100px;
	height: 50px;
	line-height: 50px;
	color: black;
	position: absolute;
	border-radius: 10px;
}

#target1{
	  top:10px;
  left:300px;
}



#target2{
	  top:160px;
  left:340px;
}


#target3{
	  top:260px;
  left:140px;
}


.show-over{
  background-color: red;
}


#demo {
  width:800px;
  height:400px;
  background-color:black;
  position:relative;
}


              
            
!

JS

              
                const targets = document.querySelectorAll(".target");
const overlapThreshold = "50%";

// go through all the draggable objects and store their starting positions
// so can return to them when dragged off the targets

const boxes = document.querySelectorAll(".box");
boxes.forEach(function (box, index) {
  box.originalLeft = box.offsetLeft;
  box.originalTop = box.offsetTop;
});

Draggable.create(".box", {
  bounds: "#demo",
  edgeResistance: 0.65,
  type: "x,y",
  throwProps: true,
  onDragStart: function (e) {},

  // changes the colour of the hotspots whilst dragging to give feedback that a
  // dragged object is going to snap to it
  onDrag: function (e) {
    for (let i = 0; i < targets.length; i++) {
      if (this.hitTest(targets[i], overlapThreshold)) {
        targets[i].classList.add("show-over");
      } else {
        targets[i].classList.remove("show-over");
      }
    }
  },

  onDragEnd: function (e) {
    let snapMade = false;
    for (let i = 0; i < targets.length; i++) {
      if (this.hitTest(targets[i], overlapThreshold)) {
        // only snap to an available hotspot, i.e.
        // one without a class of "occupied"
        if (!targets[i].classList.contains("occupied")) {
          // add a class of occupied to hotspot to stop other items
          // being snapped to it
          targets[i].classList.add("occupied");

          // tween onto hotspot
          TweenLite.to(e.target, 0.1, {
            left: targets[i].offsetLeft,
            top: targets[i].offsetTop,
            x: 0,
            y: 0,
          });

          // is a property called targetAttachedTo directly on the dragged item.
          // this stores the target we have snapped to.  Allows us to free up
          // the target if we drag it off it

          // before we update that property first check that we haven't dragged
          // from one target straight to another as this would balls it up
          if (
            e.target.targetAttachedTo !== targets[i] &&
            e.target.targetAttachedTo !== undefined
          ) {
            e.target.targetAttachedTo.classList.remove("occupied");
          }

          // now store new target in targetAttachedTo property
          e.target.targetAttachedTo = targets[i];
          snapMade = true;
        }
      }
    }

    // if the dragged item isn't over a target send it back to its
    // start position
    if (!snapMade) {
      if (e.target.targetAttachedTo !== undefined) {
        e.target.targetAttachedTo.classList.remove("occupied");
        e.target.targetAttachedTo = undefined;
      }
      TweenLite.to(e.target, 0.1, {
        x: 0,
        y: 0,
        top: e.target.originalTop,
        left: e.target.originalLeft,
      });
    }
  },
});

              
            
!
999px

Console