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

              
                <div class="wrapper">
  <div class="theOne" id="ball">
    
  </div>
</div>
              
            
!

CSS

              
                *{
  margin: 0;
}
.wrapper{
  width: 100vw;
  height: 100vh;
  background: #fff;
  position: relative;
}
.theOne{
  background-image: url(https://images.unsplash.com/photo-1417721885406-d31aee8c2a79?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ);
  background-size: 100vw;
  width: 180px;
  height: 180px;
  position: absolute;
  cursor: pointer;
  animation-duration: .3s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-name: anim2;
}
.theOne:after{
  content: 'DARE DRAG ME';
  color: blue;
  position: absolute;
  bottom: -20px;
  animation-duration: .3s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-name: anim1;
}


@keyframes anim1 {
  from {
    transform: translateX(20px) scale(1);
  }
  to {
    transform: translateX(20px) scale(1.2);
  }
}
              
            
!

JS

              
                ball.onmousedown = function(event) {

  let shiftX = event.clientX - ball.getBoundingClientRect().left;
  let shiftY = event.clientY - ball.getBoundingClientRect().top;

  ball.style.position = 'absolute';
  ball.style.zIndex = 1000;
  document.body.append(ball);

  moveAt(event.pageX, event.pageY);

  // moves the ball at (pageX, pageY) coordinates
  // taking initial shifts into account
  function moveAt(pageX, pageY) {
    ball.style.left = pageX - shiftX + 'px';
    ball.style.top = pageY - shiftY + 'px';
    ball.style.backgroundPosition = `${-pageX + shiftX}px ${-pageY + shiftY}px`
  }

  function onMouseMove(event) {
    moveAt(event.pageX, event.pageY);
    
  }

  // move the ball on mousemove
  document.addEventListener('mousemove', onMouseMove);

  // drop the ball, remove unneeded handlers
  ball.onmouseup = function() {
    document.removeEventListener('mousemove', onMouseMove);
    ball.onmouseup = null;
  };

};

ball.ondragstart = function() {
  return false;
};
              
            
!
999px

Console