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

              
                <link href='https://fonts.googleapis.com/css?family=Signika+Negative:300,400,700' rel='stylesheet' type='text/css'>

<h1>Draggable: Drop on target only</h1>
<p>The boxes below can only be dragged and dropped on Drop Area. If boxes are dropped and do not overlap the dropArea entirely they will return to starting position. When boxes are eligible to be dropped in Drop Area their border will turn yellow.</p>
<div id="container">
  <div id="box1" class="box">box1</div>
  <div id="box2" class="box">box2</div>
  <div id="box3" class="box">box3</div>
</div>

<div id="dropArea">Drop Area</div>
              
            
!

CSS

              
                body {
  background-color:black;
  color: #ccc;
	font-family: Signika Negative, Asap, sans-serif;
	font-weight: 300;
  padding: 10px;
}
h1 {
  font-size:40px;
  font-weight: 300;
  color:white;
  margin: 0;
}
#container {
  position:relative;
}
.box {
  position:absolute;
  width: 200px;
  height: 80px;
  text-align: center;
  line-height: 80px;
  font-size: 20px;
  color: white;
  border-radius:10px;
  border: 2px solid black;
}
#box1 {
	background-color: red;
  top:0px;
}
#box2 {
	background-color: blue;
  top:88px;
}
#box3 {
  background-color:green;
  top:176px;
}
a {
  color: white;
}
.highlight {
  border: 4px solid yellow;
  width: 196px;
  height: 76px;
  line-height: 76px;
}
code {
  color: white;
  font-size:1.2em;
}
p {
  width:600px;
  margin-top: 8px;
}

#dropArea {
  width:200px;
  height:200px;
  background:#ccc;
  position:absolute;
  left:600px;
  color:black;
  padding:20px;
}
              
            
!

JS

              
                //see https://www.greensock.com/draggable/ for more details.

var droppables = $(".box");
var dropArea = $("#dropArea");

//the overlapThreshold can be a percentage ("50%", for example, would only trigger when 50% or more of the surface area of either element overlaps) or a number of pixels (20 would only trigger when 20 pixels or more overlap), or 0 will trigger when any part of the two elements overlap.
var overlapThreshold = "50%";

Draggable.create(droppables, {
  bounds: window,
  onDrag: function(e) {
    if (this.hitTest(dropArea, overlapThreshold)) {
      $(this.target).addClass("highlight");
    } else {
      $(this.target).removeClass("highlight");
    }
  },
  onDragEnd: function(e) {
    //instead of doing hitTest again, just see if it has the highligh class.
    var targetContainer = null;
    if (!$(this.target).hasClass("highlight")) {
      targetContainer = container;
    } else {
      targetContainer = dropArea[0];
    }

    var rect = this.target.getBoundingClientRect();
    targetContainer.appendChild(this.target);

    TweenMax.set(this.target, { x: 0, y: 0 });
    var newRect = this.target.getBoundingClientRect();

    TweenMax.from(this.target, 1, {
      x: rect.left - newRect.left,
      y: rect.top - newRect.top
    });
  }
});

              
            
!
999px

Console