css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-- Container for preview's sake -->
<p style="text-align: center; width: 100%;">Scroll in the container or tap the Hoptimist to make it bounce its head vigorously.</p>
<div id="container">
  <!-- Real content starting here -->
  <!-- The Hoptimist both as image and trademark belongs to Hoptimist ApS. I am simply lending the figure. I will take it down if they contact me -->
  <div id="interactive-hoptimist" style="opacity=0;">
    <img src="https://res.cloudinary.com/simonmilfred/image/upload/v1544696740/CodePen/Interactive%20Hoptimist/hoptimist-lid.png" class="lid-part">
    <img src="https://res.cloudinary.com/simonmilfred/image/upload/v1544696740/CodePen/Interactive%20Hoptimist/hoptimist-spring.png" class="spring-part">
    <img src="https://res.cloudinary.com/simonmilfred/image/upload/v1544696740/CodePen/Interactive%20Hoptimist/hoptimist-bottom.png" class="bottom-part">
  </div>
</div>
            
          
!
            
              /* Container for preview's sake */
#container {
  position: relative;
  top: 25vh;
  height: 150vh;
  max-width: 50vh;
  margin: 0 auto;
}

/* Real content starting here */
#interactive-hoptimist {
  position: relative;
  width: 100%;
  height: auto;
}

#interactive-hoptimist img {
  display: block;
  z-index: 1;
  position: absolute;
  width: 100%;
  height: auto;
  padding: 0;
  margin: 0;
}

#interactive-hoptimist img.bottom-part {
  z-index: 2;
}
#interactive-hoptimist img.lid-part {
  z-index: 3;
}

#interactive-hoptimist img.lid-part:hover {
  cursor: pointer;
}

            
          
!
            
              window.onload = hoptimist_logic;

function hoptimist_logic() {
  var div = document.querySelector("#interactive-hoptimist");
  div.style.opacity = 1;
  var lid = document.querySelector("#interactive-hoptimist .lid-part");
  var lid_yoffset = 0;
  var lid_yspeed = 0;
  var spring = document.querySelector("#interactive-hoptimist .spring-part");
  var scroll_previous = 0;
  var scroll_speed = 0;
  var bottom = document.querySelector("#interactive-hoptimist .bottom-part");
  var naturalHeight = lid.naturalHeight + spring.naturalHeight + bottom.naturalHeight;
  update();
  lid.onclick = function() {
    lid_yspeed = 128 / bottom.naturalHeight * bottom.clientHeight;
  };
  /* Update */
  function update() {
    var scale = 1 / lid.naturalWidth * lid.clientWidth;
    div.style.height = naturalHeight * scale + "px";
    spring.style.bottom = bottom.clientHeight + "px";
    bottom.style.bottom = 0;
    scroll_speed = window.scrollY - scroll_previous;
    scroll_previous = window.scrollY;
    lid_yspeed += scroll_speed * 2 * scale;
    var output = numeric_springing(lid_yoffset, lid_yspeed, 0, 0.01, 1, 0.5);
    lid_yoffset = output[0].toFixed(2) * 1.0;
    if (lid_yoffset > spring.naturalHeight * scale * 2) {
      lid_yoffset = spring.naturalHeight * scale * 2;
      if (lid_yspeed > 0) {
        lid_yspeed = 0;
      }
    }
    lid_yspeed = output[1].toFixed(2) * 1.0;
    lid.style.transform = "translateY(" + lid_yoffset + "px)";
    spring.style.height = spring.naturalHeight * scale - lid_yoffset + 4 + "px";
    setTimeout(update, 25);
  }
  /* Numeric springing */
  function numeric_springing(x, v, xt, zeta, omega, h) {
    /*
          Based on the good work of Ming-lun "Allen" Chou
          x     - value             (input)
          v     - velocity          (input)
          xt    - target value      (input)
          zeta  - damping ratio     (input)
          omega - angular frequency (input)
          h     - time step         (input)
          output- array             (output)
        */
    var f = 1.0 + 2.0 * h * zeta * omega;
    var oo = omega * omega;
    var hoo = h * oo;
    var hhoo = h * hoo;
    var detInv = 1.0 / (f + hhoo);
    var detX = f * x + h * v + hhoo * xt;
    var detV = v + hoo * (xt - x);
    var output = [detX * detInv, detV * detInv];
    return output;
  }
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console