Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="fullscreen">
  <div class="text">
    <div class="container">
      <h2>Animate Elements If In Viewport</h2>
      <p>
        Detect if element is within viewport and leverage CSS to animate.
      </p>
      <a class="btn" href="http://xtianmiller.com/notes/animating-elements-when-they-appear-in-viewport/" target="_blank">
        <span class="row1">Read Article</span>
        <span class="row2">Read Article</span>
      </a>
    </div>
  </div>
  <div class="scroll">Scroll Down...</div>
</div>
<div class="container container--circles">
  <div class="circle c1 revealer">
    <div class="circle__inner">
      01
    </div>
  </div>
  <div class="circle c2 revealer">
    <div class="circle__inner">
      02
    </div>
  </div>
  <div class="circle c3 revealer">
    <div class="circle__inner">
      03
    </div>
  </div>
  <div class="circle c4 revealer">
    <div class="circle__inner">
      04
    </div>
  </div>
  <div class="circle c5 revealer">
    <div class="circle__inner">
      05
    </div>
  </div>
  <div class="circle c6 revealer">
    <div class="circle__inner">
      06
    </div>
  </div>
  <div class="circle c7 revealer">
    <div class="circle__inner">
      07
    </div>
  </div>
  <div class="circle c8 revealer">
    <div class="circle__inner">
      08
    </div>
  </div>
  <div class="circle c9 revealer">
    <div class="circle__inner">
      09
    </div>
  </div>
  <div class="circle c10 revealer">
    <div class="circle__inner">
      10
    </div>
  </div>
  <div class="circle c11 revealer">
    <div class="circle__inner">
      11
    </div>
  </div>
  <div class="circle c12 revealer">
    <div class="circle__inner">
      12
    </div>
  </div>
  <div class="circle c13 revealer">
    <div class="circle__inner">
      13
    </div>
  </div>
  <div class="circle c14 revealer">
    <div class="circle__inner">
      14
    </div>
  </div>
  <div class="circle c15 revealer">
    <div class="circle__inner">
      15
    </div>
  </div>
  <div class="circle c16 revealer">
    <div class="circle__inner">
      16
    </div>
  </div>
  <div class="circle c17 revealer">
    <div class="circle__inner">
      17
    </div>
  </div>
  <div class="circle c18 revealer">
    <div class="circle__inner">
      18
    </div>
  </div>
  <div class="circle c19 revealer">
    <div class="circle__inner">
      19
    </div>
  </div>
  <div class="circle c20 revealer">
    <div class="circle__inner">
      20
    </div>
  </div>
  <div class="circle c21 revealer">
    <div class="circle__inner">
      21
    </div>
  </div>
  <div class="circle c22 revealer">
    <div class="circle__inner">
      22
    </div>
  </div>
  <div class="circle c23 revealer">
    <div class="circle__inner">
      23
    </div>
  </div>
  <div class="circle c24 revealer">
    <div class="circle__inner">
      24
    </div>
  </div>
</div>
<div class="fullscreen">
  <div class="text">
    <div class="container">
      <p>
        Scroll back up to reset animations.
      </p>
    </div>
  </div>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  font-size: 18px;
  line-height: 1.4;
  font-family: 'Inconsolata', sans-serif;
  background-color: #1d1f20;
  color: #666;
}

h2 {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  font-family: 'Raleway', sans-serif;
  position: relative;
  line-height: 1.3;
}

p {
  padding-top: 15px;
}

a.btn {
  display: inline-block;
  margin-top: 25px;
  border: 1px solid #00c1bb;
  color: #00c1bb;
  text-decoration: none;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 2px;
  position: relative;
  overflow: hidden;
}

a.btn span {
  display: inline-block;
  padding: 6px 40px;
  transition: transform 0.4s;
}

a.btn .row2 {
  position: absolute;
  left: 0;
  top: 0;
  transform: translateY(100%);
}

a.btn:hover .row1 {
  transform: translateY(-100%);
}

a.btn:hover .row2 {
  transform: translateY(0);
}

.container {
  margin: 0 auto;
  padding-left: 6%;
  padding-right: 6%;
  max-width: 1400px;
  clear: both;
}

.container:after {
  content: ' ';
  display: block;
  clear: both;
}

.container--circles {
  padding-top: 50px;
  padding-bottom: 50px;
}

.fullscreen {
  display: table;
  text-align: center;
  width: 100%;
  position: relative;
  z-index: 100;
}

.footer {
  width: 100%;
  text-align: center;
  padding-top: 150px;
  padding-bottom: 150px;
}

.text {
  display: table-cell;
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

.scroll {
  width: 100%;
  position: absolute;
  left: 0;
  bottom: 30px;
  text-align: center;
  color: #fff;
  text-decoration: none;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 2px;
}

.circle {
  width: 31.333333%;
  margin-top: 1%;
  margin-bottom: 1%;
  margin-left: 1%;
  margin-right: 1%;
  background-color: #fff;
  border-radius: 50%;
  display: inline-block;
  float: left;
  position: relative;
  font-size: 250%;
}

.enhanced .circle {
  transition: transform 600ms cubic-bezier(0.8, 0, 0, 0.8);
  transform: scale(0.3);
}

.circle.revealed {
  transform: scale(1);
}

.circle:before {
  display: block;
  content: "";
  width: 100%;
  padding-top: 100%;
}

.circle.c1 {
  background-color: #ed194b;
}

.circle.c2 {
  background-color: #f37c78;
  transition-delay: 0.1s;
}

.circle.c3 {
  background-color: #f8a19a;
  transition-delay: 0.1s;
}

.circle.c4 {
  background-color: #f7921e;
}

.circle.c5 {
  background-color: #faac57;
  transition-delay: 0.1s;
}

.circle.c6 {
  background-color: #fdc78b;
  transition-delay: 0.2s;
}

.circle.c7 {
  background-color: #ffd401;
}

.circle.c8 {
  background-color: #ffe500;
  transition-delay: 0.1s;
}

.circle.c9 {
  background-color: #fef46e;
  transition-delay: 0.2s;
}

.circle.c10 {
  background-color: #a6ce3a;
}

.circle.c11 {
  background-color: #bed82f;
  transition-delay: 0.1s;
}

.circle.c12 {
  background-color: #d6e167;
  transition-delay: 0.2s;
}

.circle.c13 {
  background-color: #00bbbd;
}

.circle.c14 {
  background-color: #72cbcf;
  transition-delay: 0.1s;
}

.circle.c15 {
  background-color: #9bd8d9;
  transition-delay: 0.2s;
}

.circle.c16 {
  background-color: #016bb5;
}

.circle.c17 {
  background-color: #628ac8;
  transition-delay: 0.1s;
}

.circle.c18 {
  background-color: #8aa4d5;
  transition-delay: 0.2s;
}

.circle.c19 {
  background-color: #af72ae;
}

.circle.c20 {
  background-color: #c196c4;
  transition-delay: 0.1s;
}

.circle.c21 {
  background-color: #d0b1d3;
  transition-delay: 0.2s;
}

.circle.c22 {
  background-color: #ba3599;
}

.circle.c23 {
  background-color: #d468b9;
  transition-delay: 0.1s;
}

.circle.c24 {
  background-color: #eda4db;
  transition-delay: 0.2s;
}

.circle__inner {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  width: 100%;
  text-align: center;
  transform: translateY(-52%);
  color: #1d1f20;
  line-height: 1;
}
              
            
!

JS

              
                var withinViewport = (function() {

  'use strict';

  // Cutting the mustard
  // http://webfieldmanual.com/guides/cutting-the-mustard.html

  if (window.requestAnimationFrame && document.documentElement.classList) {

    // Passes the test so add enhanced class to HTML tag
    document.documentElement.classList.add('enhanced');

    // Throttle
    // https://underscorejs.org/#throttle
    var throttle = function(func, wait, options) {
      var _ = {
        now: Date.now || function() {
          return new Date().getTime();
        }
      };
      var context, args, result;
      var timeout = null;
      var previous = 0;
      if (!options) {
        options = {};
      }
      var later = function() {
        previous = options.leading === false ? 0 : _.now();
        timeout = null;
        result = func.apply(context, args);
        if (!timeout) {
          context = args = null;
        }
      };
      return function() {
        var now = _.now();
        if (!previous && options.leading === false) {
          previous = now;
        }
        var remaining = wait - (now - previous);
        context = this;
        args = arguments;
        if (remaining <= 0 || remaining > wait) {
          if (timeout) {
            clearTimeout(timeout);
            timeout = null;
          }
          previous = now;
          result = func.apply(context, args);
          if (!timeout) {
            context = args = null;
          }
        } else if (!timeout && options.trailing !== false) {
          timeout = setTimeout(later, remaining);
        }
        return result;
      };
    };
    
    // requestAnimationFrame
    // http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
    var _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;

    // Global class for revealing element
    var revealer = document.querySelectorAll('.revealer');

    // Get the viewport (window) dimensions
    var getViewportSize = function() {
      return {
        width: window.document.documentElement.clientWidth,
        height: window.document.documentElement.clientHeight
      };
    };

    // Get the current scoll position
    var getCurrentScroll = function() {
      return {
        x: window.pageXOffset,
        y: window.pageYOffset
      };
    };

    // Get element dimensions and position
    var getElemInfo = function(elem) {
      var offsetTop = 0;
      var offsetLeft = 0;
      var offsetHeight = elem.offsetHeight;
      var offsetWidth = elem.offsetWidth;

      do {
        if (!isNaN(elem.offsetTop)) {
          offsetTop += elem.offsetTop;
        }
        if (!isNaN(elem.offsetLeft)) {
          offsetLeft += elem.offsetLeft;
        }
      } while ((elem = elem.offsetParent) !== null);

      return {
        top: offsetTop,
        left: offsetLeft,
        height: offsetHeight,
        width: offsetWidth
      };
    };

    // Check visibility of the element in the viewport
    var checkVisibility = function(elem) {
      var viewportSize = getViewportSize();
      var currentScroll = getCurrentScroll();
      var elemInfo = getElemInfo(elem);
      var spaceOffset = 0.2;
      var elemHeight = elemInfo.height;
      var elemWidth = elemInfo.width;
      var elemTop = elemInfo.top;
      var elemLeft = elemInfo.left;
      var elemBottom = elemTop + elemHeight;
      var elemRight = elemLeft + elemWidth;

      var checkBoundaries = function() {
        // Defining the element boundaries and extra space offset
        var top = elemTop + elemHeight * spaceOffset;
        var left = elemLeft + elemWidth * spaceOffset;
        var bottom = elemBottom - elemHeight * spaceOffset;
        var right = elemRight - elemWidth * spaceOffset;

        // Defining the window boundaries and window offset
        var wTop = currentScroll.y + 0;
        var wLeft = currentScroll.x + 0;
        var wBottom = currentScroll.y - 0 + viewportSize.height;
        var wRight = currentScroll.x - 0 + viewportSize.width;

        // Check if the element is within boundary
        return (top < wBottom) && (bottom > wTop) && (left > wLeft) && (right < wRight);
      };

      return checkBoundaries();
    };

    // Run a loop with checkVisibility() and add / remove classes to the elements
    var toggleElement = function() {
      for (var i = 0; i < revealer.length; i++) {
        if (checkVisibility(revealer[i])) {
          revealer[i].classList.add('revealed');
        } else {
          revealer[i].classList.remove('revealed');
        }
      }
    };

    // Throttle events and requestAnimationFrame
    var scrollHandler = throttle(function() {
      _requestAnimationFrame(toggleElement);
    }, 300);

    var resizeHandler = throttle(function() {
      _requestAnimationFrame(toggleElement);

      // For demo purposes only
      fullscreenIntro();
    }, 300);

    scrollHandler();

    // Listening for events
    if (window.addEventListener) {
      addEventListener('scroll', scrollHandler, false);
      addEventListener('resize', resizeHandler, false);
    } else if (window.attachEvent) {
      window.attachEvent('onscroll', scrollHandler);
      window.attachEvent('onresize', resizeHandler);
    } else {
      window.onscroll = scrollHandler;
      window.onresize = resizeHandler;
    }

  }

  // FOR DEMO PURPOSES ONLY
  // Makes fullscreen intro on any device so user is forced to scroll
  var fullscreenIntro = function() {
    var fullscreen = document.querySelectorAll('.fullscreen');
    for (var i = 0; i < fullscreen.length; i++) {
      fullscreen[i].style.height = getViewportSize().height + 1 + 'px';
    }
  };
  fullscreenIntro();

  return withinViewport;

}());
              
            
!
999px

Console