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="holder">

  <section></section>
  <section class="parallax parallax-1" data-parallax="100"></section>
  <section></section>
  <section>
    <div class="scrollreveal scrollreveal-1">
      Show when enter Viewport
    </div>
  </section>
  <section></section>
  <section class="parallax parallax-2" data-parallax="200">
    <div class="scrollreveal scrollreveal-2">
      Only on first reveal
    </div>
  </section>
  <section></section>
  <section></section>
  <section class="parallax parallax-3" data-parallax="50"></section>
  <section></section>

</div>
              
            
!

CSS

              
                .holder {
  min-height: 2500px;
}

section {
  min-height: 300px;
  padding-top: 70px;
  padding-bottom: 70px;
}

section:nth-child(0) {
  background-color: rgba(255, 0, 0, 0.3);
}

section:nth-child(1) {
  background-color: rgba(0, 128, 0, 0.3);
}

section:nth-child(2) {
  background-color: rgba(255, 255, 0, 0.3);
}

section:nth-child(3) {
  background-color: rgba(0, 0, 255, 0.3);
}

section:nth-child(4) {
  background-color: rgba(0, 0, 0, 0.3);
}

section:nth-child(5) {
  background-color: rgba(0, 255, 255, 0.3);
}

section:nth-child(6) {
  background-color: rgba(255, 165, 0, 0.3);
}

section:nth-child(7) {
  background-color: rgba(255, 192, 203, 0.3);
}

section:nth-child(8) {
  background-color: rgba(128, 128, 128, 0.3);
}

section:nth-child(9) {
  background-color: rgba(0, 0, 139, 0.3);
}

section:nth-child(10) {
  background-color: rgba(0, 128, 0, 0.3);
}

.parallax {
  position: relative;
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: 50% 50%;
}

.parallax-1 {
  min-height: 500px;
  background-image: url("https://placekitten.com/g/1900/1280");
}

.parallax-2 {
  background-image: url("https://placekitten.com/g/1900/1230");
}

.parallax-3 {
  background-image: url("https://placekitten.com/g/1900/1300");
}

.scrollreveal { font-size: 60px; color: white; text-align: center; width: 50%; margin: 0 auto; padding: 30px; background-color: rgba(0, 0, 0, 0.2); -webkit-transition: all .5s; transition: all .5s; /*  Hide on default*/ opacity: 0; }

.scrollreveal-2 { -webkit-transition-duration: 2s; transition-duration: 2s; -webkit-transform: scale(0); -ms-transform: scale(0); transform: scale(0); }

.scrollreveal-2.js-revealed { opacity: 1; -webkit-transform: scale(1); -ms-transform: scale(1); transform: scale(1); }

/*  Will be added when item is fully visible, and removed when it's fully out of viewport */
.js-reveal { opacity: 1; }

/*  Will be added the first time item is fully visible  */
.js-revealed { background-color: rgba(0, 0, 0, 0.7); }

/*# sourceMappingURL=style.css.map */
              
            
!

JS

              
                ;
(function($, window, document, undefined) {

  var defaults = {
    viewportSelector: null,

    //Effect could be parallax or scrollReveal
    effectType: 'parallax',

    //Parallax options
    parallaxMaxOffset: 100,

    //Scroll Reveal options

    //Added when item is fully visible, removed when it's fully out of viewport
    scrollRevealedClass: 'js-reveal',

    //Added the first time the item is revealed, it's not remove when the item is hidden
    scrollFirstRevealClass: 'js-revealed',
    autoEnable: true,

    //
    debug: false
  };

  function ScrollEffects(element, options) {

    this.settings = $.extend({}, defaults, options);

    this.$window = $(window);
    this.$viewport = this.settings.viewportSelector ? $(this.settings.viewportSelector) : this.$window;

    this.$elements = $(element);

    this.scrollables = [];

    this.isEnabled = false;

    this.wpWidth = this.$viewport.width();
    this.wpHeight = this.$viewport.height();
    this.scrollTop = 0;

    this.init();
  }

  ScrollEffects.TYPE = {
    PARALLAX: 'parallax',
    SCROLLREVEAL: 'scrollReveal'
  };

  ScrollEffects.prototype = {
    init: function() {
      this.createScrollables();
      if (this.settings.autoEnable) {
        this.enable();
      }
    },

    createScrollables: function() {

      if (this.settings.debug) {
        this.$debug = $('<div />').css({
          'z-index': 1000,
          'position': 'fixed',
          'top': '10px',
          'left': '20px',
          'color': 'white',
          'background-color': 'rgba(0,0,0,.3)',
          'padding': '10px 5px',
          'border-radius': '3px'
        }).text('debug').appendTo($('body'));
      }

      for (var i = 0, j = this.$elements.length; i < j; i++) {

        var $element = this.$elements.eq(i),
          scrollable = {
            $element: $element
          };

        //Parallax
        if (this.settings.effectType == ScrollEffects.TYPE.PARALLAX) {
          scrollable.maxOffset = $.isNumeric($element.data('parallax')) ? parseInt($element.data('parallax')) : this.settings.parallaxMaxOffset;
        }

        //ScrollReveal
        if (this.settings.effectType == ScrollEffects.TYPE.SCROLLREVEAL) {
          scrollable.isFirstReveal = true;
          scrollable.isRevealed = false;
        }

        //ScrollReveal items
        this.scrollables.push(scrollable);
      }
    },

    addEvents: function() {

      this.resetScrollables();
      this.$window.on('resize', $.proxy(this.onResize, this));
      this.$window.on('scroll', $.proxy(this.onScroll, this));

      this.$window.trigger('resize').trigger('scroll');
    },

    resetScrollables: function() {
      if (this.settings.effectType == ScrollEffects.TYPE.PARALLAX) {}

      if (this.settings.effectType == ScrollEffects.TYPE.SCROLLREVEAL) {
        for (var i = 0, j = this.scrollables.length; i < j; i++) {
          var scrollable = this.scrollables[i];
          scrollable.$element.removeClass(this.settings.scrollRevealedClass);
          scrollable.$element.removeClass(this.settings.scrollFirstRevealClass);
          scrollable.isFirstReveal = true;
          scrollable.isRevealed = false;
        }
      }
    },

    onScroll: function() {

      this.scrollTop = this.$window.scrollTop();

      for (var i = 0, j = this.scrollables.length; i < j; i++) {
        var scrollable = this.scrollables[i],
          scrollData = this.getScrollData(scrollable.$element);

        if (this.settings.effectType == ScrollEffects.TYPE.PARALLAX) {
          this.processParallaxItem(scrollable, scrollData);
        }
        if (this.settings.effectType == ScrollEffects.TYPE.SCROLLREVEAL) {
          this.processScrollRevealItem(scrollable, scrollData);
        }

      }

    },

    processParallaxItem: function(_scrollable, _scrollData) {

      if (_scrollData.isVisible) {
        var offset = this.percentToRange(_scrollData.percentEntered, -_scrollable.maxOffset, _scrollable.maxOffset);
        _scrollable.$element.css({
          'background-position': '50% ' + parseInt(offset) + 'px'
        });
      }
    },

    processScrollRevealItem: function(_scrollable, _scrollData) {
      if (!_scrollable.isRevealed && _scrollData.isFullyEntered) {
        _scrollable.$element.addClass(this.settings.scrollRevealedClass);
        if (_scrollable.isFirstReveal) {
          _scrollable.isFirstReveal = false;
          _scrollable.$element.addClass(this.settings.scrollFirstRevealClass);
        }
        _scrollable.isRevealed = true;
      }

      if (_scrollable.isRevealed && _scrollData.isFullyExited) {
        _scrollable.isRevealed = false;
        _scrollable.$element.removeClass(this.settings.scrollRevealedClass);
      }
    },

    onResize: function() {
      this.wpWidth = this.$viewport.width();
      this.wpHeight = this.$viewport.height();
    },

    //--------------------------------------------------------------
    //  Return percent from 0 to 1
    //  0.01 when element is almost at the top
    //  0.99 when just entered from bottom
    //--------------------------------------------------------------

    getScrollData: function(_$element) {

      var viewPortData = {
        isVisible: false,
        isFullyEntered: false,
        isFullyExited: true,
        percentEntered: 0
      };
      
      
      var elementHeight = _$element.outerHeight(),
        boundRect = _$element.get(0).getBoundingClientRect(),
        elementTop = boundRect.top,
        elementBottom = boundRect.bottom;
      if (elementTop >= -elementHeight && elementBottom <= (this.wpHeight + elementHeight)) {
        viewPortData.isVisible = true;
        viewPortData.isFullyEntered = elementTop >= 0 && elementBottom <= this.wpHeight;
        viewPortData.percentEntered = this.rangeToPercent(elementTop, -elementHeight, this.wpHeight);
        viewPortData.isFullyExited = viewPortData.percentEntered == 0;
      }

      return viewPortData;
    },

    enable: function() {

      if (!this.isEnabled) {
        this.isEnabled = true;
        this.resetScrollables();
        this.addEvents();
      }
    },

    disable: function(_normalize) {

      if (this.isEnabled) {
        this.isEnabled = false;

        this.$window.off('scroll', $.proxy(this.onScroll, this));
        this.$window.off('resize', $.proxy(this.onResize, this));

        if (_normalize) {
          if (this.settings.effectType == ScrollEffects.TYPE.PARALLAX) {
            this.$elements.css({
              'background-position': '50% 50%'
            });
          } else if (this.settings.effectType == ScrollEffects.TYPE.SCROLLREVEAL) {
            for (var i = 0, j = this.scrollables.length; i < j; i++) {
              var scrollable = this.scrollables[i];
              scrollable.$element.addClass(this.settings.scrollRevealedClass);
              scrollable.$element.addClass(this.settings.scrollFirstRevealClass);
            }
          }
        }
      }
    },

    //--------------------------------------------------------------
    //  UTILS
    //--------------------------------------------------------------
    rangeToPercent: function(_number, _min, _max) {
      return ((_number - _min) / (_max - _min));
    },

    percentToRange: function(_percent, _min, _max) {
      return ((_max - _min) * _percent + _min);
    }

  };

  window.ScrollEffects = ScrollEffects;

})(jQuery, window, document);

//

$(function() {

  var parallax = new ScrollEffects('.parallax', {
    effectType: 'parallax'
  });

  var scrollShow = new ScrollEffects('.scrollreveal', {
    effectType: 'scrollReveal'
  });

  var $window = $(window);
  $window.on('resize', function() {

    if ($window.width() >= 900) {
      parallax.enable();
      scrollShow.enable();
    } else {
      parallax.disable(true);
      scrollShow.disable(true);
    }
  }).trigger('resize');

});
              
            
!
999px

Console