123

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.

            
              <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<section class="water" id="content">
  <div class="container">
    <h1>Parallax Scrolling CSS Bubbles</h1>
    <p>If you are working on an underwater site, you might want to consider adding some bubbles that float towards the surface as the user scrolls down the page.</p>
    <p>Go ahead and use this code if you want. But while you are at it, go check out my <a href="https://purealoha.herokuapp.com">website</a> and certainly go on a boat tour with Pure Aloha if you are ever in Kauai.</p>
  </div>
</section>
<div id="bubble-container">
  <span class="bubble bubble-sm" id="bubble-1"></span>
  <span class="bubble bubble-md" id="bubble-2"></span>
  <span class="bubble bubble-lg" id="bubble-3"></span>
  <span class="bubble bubble-md" id="bubble-4"></span>
  <span class="bubble bubble-md" id="bubble-5"></span>
  <span class="bubble bubble-sm" id="bubble-6"></span>
  <span class="bubble bubble-lg" id="bubble-7"></span>
  <span class="bubble bubble-md" id="bubble-8"></span>
  <span class="bubble bubble-sm" id="bubble-9"></span>
</div>
            
          
!
            
              @import "compass/css3";

.water{
  top:0;
  position:absolute;
  width:100%;
  height:100%;
  min-height:2500px;
  @include background-image(linear-gradient(#01C6FF, #29315F));
}
.container{
  color:#fff;
  width:600px;
  margin: 0 auto;
  font-family: 'Open Sans', sans-serif;
}
.bubble{
  position:absolute;
  border-radius:50%;
  @include background-image(radial-gradient(45px 45px, white 10px, cyan 30px));
  opacity:0.4;
  z-index:0;
}
.bubble-sm{
  width:20px;
  height:20px;
}
.bubble-md{
  width:33px;
  height:33px;
}
.bubble-lg{
  width:40px;
  height:40px;
}
.force-show {
	display: block !important;
}
            
          
!
            
              // shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          window.oRequestAnimationFrame      ||
          window.msRequestAnimationFrame     ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

(function(win, d) {

  var $ = d.querySelector.bind(d);

  var bubble1 = $('#bubble-1');
  var bubble2 = $('#bubble-2');
  var bubble3 = $('#bubble-3');
  var bubble4 = $('#bubble-4');
  var bubble5 = $('#bubble-5');
  var bubble6 = $('#bubble-6');
  var bubble7 = $('#bubble-7');
  var bubble8 = $('#bubble-8');
  var bubble9 = $('#bubble-9');

  var mainBG = $('section#content');

  var ticking = false;
  var lastScrollY = 0;

  function onResize () {
    updateElements(win.scrollY);
  }

  function onScroll (evt) {

    if(!ticking) {
      ticking = true;
      requestAnimFrame(updateElements);
      lastScrollY = win.scrollY;
    }
  }

  function updateElements () {

    var relativeY = lastScrollY / 3000;

    mainBG.style.backgroundPosition = 'center ' + pos(0, 0, relativeY, 0) + 'px';

    bubble1.style.top = pos(254, -1400, relativeY, 0) + 'px';
    bubble1.style.left = 15 + '%';

    bubble2.style.top = pos(954, -2400, relativeY, 0) + 'px';
    bubble2.style.left = 35 + '%';

    bubble3.style.top = pos(1054, -900, relativeY, 0) + 'px';
    bubble3.style.left = 75 + '%';

    bubble4.style.top = pos(1400, -3900, relativeY, 0) + 'px';
    bubble4.style.left = 5 + '%';

    bubble5.style.top = pos(1730, -2900, relativeY, 0) + 'px';
    bubble5.style.left = 40 + '%';

    bubble6.style.top = pos(2860, -4900, relativeY, 0) + 'px';
    bubble6.style.left = 90 + '%';

    bubble7.style.top = pos(2550, -1900, relativeY, 0) + 'px';
    bubble7.style.left = 65 + '%';

    bubble8.style.top = pos(2300, -700, relativeY, 0) + 'px';
    bubble8.style.left = 20 + '%';

    bubble9.style.top = pos(3700, -6000, relativeY, 0) + 'px';
    bubble9.style.left = 85 + '%';

    ticking = false;
  }

  function pos(base, range, relY, offset) {
    return base + limit(0, 1, relY - offset) * range;
  }

  function prefix(obj, prop, value) {
    var prefs = ['webkit', 'moz', 'o', 'ms'];
    for (var pref in prefs) {
      obj[prefs[pref] + prop] = value;
    }
  }

  function limit(min, max, value) {
    return Math.max(min, Math.min(max, value));
  }

  (function() {

    updateElements(win.scrollY);

    bubble1.classList.add('force-show');
    bubble2.classList.add('force-show');
    bubble3.classList.add('force-show');
    bubble4.classList.add('force-show');
    bubble5.classList.add('force-show');
    bubble6.classList.add('force-show');
    bubble7.classList.add('force-show');
    bubble8.classList.add('force-show');
    bubble9.classList.add('force-show');
  })();

  win.addEventListener('resize', onResize, false);
  win.addEventListener('scroll', onScroll, false);

})(window, document);
            
          
!
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.

Console