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="container">
  <h1 class="main-title">Bouncing Ball</h1>
  <div class="container-inner">
    <div class="ball"></div>
  </div>
  
  <div class="flex-container">
    <button class="btn btn-stop" disabled>Stop</button>
    <button class="btn btn-play">Play</button>
  </div>
  
  
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background-color: #222;
  font-family: sans-serif;
}

.container {
  width: 100%;
  max-width: 800px;
  height: 100%;
  margin: 0 auto;
  border: 2px solid #222;
  background-color: #222;
}

.main-title {
  text-align: center;
  color: white;
}

.container-inner {
  width: 100%;
  height: 200px;
  overflow: hidden;
  margin: 0 auto;
  background-color: #fff;
}

.ball {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background-color: #222;
  border-radius: 50%;
}

.flex-container {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-around;
  margin: auto;
}

.btn {
  position: relative;
  padding: 1em;
  max-width: 100px;
  margin: 2em auto;
  outline: 0;
  text-transform: uppercase;
  text-align: center;
  vertical-align: middle;
  letter-spacing: 1px;
  color: #fff;
  background-color: #222;
  border-color: #555;
  border-radius: 10px;
  cursor: pointer;
  transition: border-color, 0.5s;
}

.btn:focus,
.btn:hover {
  border-color: #ddd;
}

.btn:disabled,
.btn[disabled]{
  border: 1px solid #999999;
  background-color: #cccccc;
  color: #666666;
  cursor:not-allowed;
}
              
            
!

JS

              
                //Moves the ball up or down
/* Use of const for storing values you don't need to upate.*/
const changeBallPosition = (elem, propVal, easingVal, durationVal) => {
  Velocity(elem, {
    translateY: propVal
  }, {
    easing: easingVal,
    duration: durationVal
  });
};

//Brings about the squashed appearance of the ball
//and resumes the ball's natural shape after hitting
//the floor
const changeBallWidth = (elem, propVal, easingVal, durationVal) => {
  Velocity(elem, {
    scaleX: propVal
  }, {
    easing: easingVal,
    duration: durationVal
  });
};

//Store return value of requestAnimationFrame() here
let animationFrameID;

//Velocity calls inside a looping functions 
const launchBall = (elem) => {
  changeBallPosition(elem, '130px', 'easeInQuart', 300);
  changeBallWidth(elem, 1.2, 'linear', 50);
  changeBallWidth(elem, 1, 'linear', 50);
  changeBallPosition(elem, '-10px', 'easeOutQuart', 300);
  changeBallWidth(elem, 1, 'linear', 50);
  animationFrameID = requestAnimationFrame(
    () => {
      launchBall(elem);
    });
};

//Use cancelAnimationFrame() to stop the animation
const removeAnimFrame = () => {
  if (animationFrameID) {
    cancelAnimationFrame(animationFrameID);
  }
}

const ball = document.querySelector('.ball'),
  btnStop = document.querySelector('.btn-stop'),
  btnPlay = document.querySelector('.btn-play');

/*Notice use of regular function rather than arrow function
to be able to refer to the button being clicked using the this 
keyword*/
btnPlay.addEventListener('click', function() {
  launchBall(ball);
  this.disabled = true;
  btnStop.disabled = false;
});

btnStop.addEventListener('click', function() {
  removeAnimFrame();
  Velocity(ball, 'stop', true);
  this.disabled = true;
  btnPlay.disabled = false;
});
              
            
!
999px

Console