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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <link href='https://fonts.googleapis.com/css?family=Signika+Negative:300,400' rel='stylesheet' type='text/css'>
<h1>Draggable with direction sensing</h1>
<div class="dataDisplay">this.getDirection(<strong>"start"</strong>): <span id="directionStart">-</span></div>
<div class="dataDisplay">this.getDirection(<strong>"velocity"</strong>): <span id="directionVelocity">-</span></div>
<div class="dataDisplay">this.getDirection(<strong>logoElement</strong>): <span id="directionObject">-</span></div>
<div id="container">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/greensock-man-512x512.png" id="logoElement" width="150" height="150"/>
		<div class="box" id="original" style="background-color:#555;">Start position</div>
		<div class="box" id="box1">Drag me</div>
</div>

              
            
!

CSS

              
                body {
  background:#1d1d1d;
  font-family: Signika Negative, Asap, sans-serif;
  color: #999;
  padding: 14px;
}

h1 {
  font-weight: 400;
  margin: 0 0 5px 0;
  color: white;
}

.dataDisplay {
	font-size: 20px;
  font-weight: 300;
  font-family: monospace;
  color: #ccc;
  margin: 10px 0;
}

p span {
  color: white;
  font-weight: 400;
}

#container {
  background:black;
  height:440px; 
  width: 100%;
  overflow:visible; 
  padding:0; 
  position:relative;
  border: 1px solid #555;
}

.box {
  background-color: #91e600;
  text-align: center;
  width: 196px;
  height: 100px;
  line-height: 100px;
  color: black;
  position: absolute;
  top:0;
  font-size: 18px;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
}
a, a:hover, a:visited {
  color:#71B200;
}

#logoElement {
  position:absolute;
  left:50%;
  top:50%;
  border: solid 1px #555;
}

.dataDisplay strong {
  color:#d0782a;
  font-weight:400;
}

#directionStart, #directionVelocity, #directionObject {
  color: #91e600;
  font-weight: 400;
}
              
            
!

JS

              
                /*
This demo illustrates direction sensing capabilities in Draggable

Watch a video explaining all the details: https://greensock.com/gsap-1-16

The getDirection() method can determine
 - direction from where the drag started
 - momementary direction 
 - direction relative to any elment

*/
var directionStart = document.getElementById("directionStart"),
    directionVelocity = document.getElementById("directionVelocity"),
    directionObject = document.getElementById("directionObject"),
    original = document.getElementById("original"),
		logoElement = document.getElementById("logoElement");

Draggable.create("#box1", {
  bounds:"#container",
  edgeResistance:0.65,
  inertia:true,
  autoScroll:1,
  onDrag:updateDirections,
  onThrowUpdate:updateDirections,
  onThrowComplete:function() {
    //move the original marker to end position
    gsap.to(original, {duration:1, x:this.x, y:this.y});
  }
});

function updateDirections() {
    //getDirection() can return 3 types of direction...
    directionStart.innerHTML = '"' + this.getDirection("start") + '"'; //direction from start of drag
    directionVelocity.innerHTML = '"' + this.getDirection("velocity") + '"'; //momentary velocity *requires InertiaPlugin
    directionObject.innerHTML = '"' + this.getDirection(logoElement) + '"'; //direction from an object
}

gsap.set(logoElement, {xPercent:-50, yPercent:-50})

/*
See https://www.greensock.com/draggable/ for details about Draggable. 
This demo uses InertiaPlugin which is a membership benefit of Club GreenSock, https://www.greensock.com/club/
InertiaPlugin is necessary for getDirection("velocity").
*/
              
            
!
999px

Console