cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div ng-app="app" ng-controller="GridController as grid" class="grid">
  <h1 scroll-header  
      tolerance="grid.scrollTolerance" 
      class="grid__header grid__header--fixed">
    Animated Header
  </h1>
  
  <div class="grid__list">
    <div class="grid__item" 
         ng-style="{'background-color': item.bgColor}"
         ng-repeat="item in grid.items track by $index">
          {{item.index}}
    </div>
  </div>
</div>

            
          
!
            
              .grid {
  width: 100%;
}

.grid__header {
  height: 75px;
  min-height: 75px;
  line-height: 75px;
  background: black;
  color: white;
  display: block;
  margin: 0 auto;
  text-align: center;
  transition: transform .4s ease;
}

.grid__header--fixed {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1;
}

.grid__list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding-top: 75px;
}

.grid__item {
  width: 12.5%;
  flex: 0 1 auto;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid__item:after {
  content: '';
  display: block;
  padding-bottom: 100%;
}
            
          
!
            
              (function() {
  'use strict';

  angular
    .module('app', [])
    .controller('GridController', GridController)
    .directive('scrollHeader', scrollHeader);

  function GridController() {
    this.items = createItemList(250);
    this.scrollTolerance = {
      up: 150,
      down: 75
    };
  }

  function scrollHeader($window) {
    return {
      restrict: 'A',
      scope: {
        tolerance: '='
      },
      link: function($scope, element, attrs) {

        var prevScroll = {
            val: null,
            direction: null
          },
          directionChangedVal,
          resizeListener,
          scrollListener,
          isListeningWindowEvents;

        resizeListener = function() {
          prevScroll.val = $window.scrollY;
        };

        scrollListener = function() {

          var scroll = {
              val: $window.scrollY,
              direction: $window.scrollY > prevScroll.val ? 'down' : 'up'
            },
            headerHeight = 75;

          if(isOutOfDocumentBounds(scroll.val)) {
            return;
          }

          if(scroll.direction === 'up') {
            if(!inUpTolerance(scroll.val)) {
              clearTolerance();
              showHeader();
            }

          } else if(scroll.direction === 'down') {
            if(!inDownTolerance(scroll.val)) {
              clearTolerance();
              hideHeader(headerHeight);
            }
          }

          prevScroll = {
            val: scroll.val,
            direction: scroll.direction
          };
        };

        initListenWindowEvents();

        function showHeader() {
          element.css('transform', 'translateY(0)');
        }

        function hideHeader(headerHeight) {
          element.css('transform', 'translateY(' + (-1 * headerHeight) + 'px)');
        }

        function initListenWindowEvents() {
          if(!isListeningWindowEvents) {
            isListeningWindowEvents = true;
            angular.element($window).on('resize', resizeListener);
            angular.element($window).on('scroll', scrollListener);
          }
          initHeader();
        }

        function destroyListenWindowEvents() {
          isListeningWindowEvents = false;
          angular.element($window).off('resize', resizeListener);
          angular.element($window).off('scroll', scrollListener);
        }

        function initHeader() {
          prevScroll = {
            val: 0,
            direction: null
          };
          directionChangedVal = null;
          showHeader();
        }

        function inUpTolerance(scrollVal) {

          if(prevScroll.direction === 'down') {
            directionChangedVal = scrollVal;
          }

          return scrollVal > 0 &&
            ((directionChangedVal - scrollVal) <= $scope.tolerance.up);
        }

        function inDownTolerance(scrollVal) {

          if(prevScroll.direction === 'up') {
            directionChangedVal = scrollVal;
          }

          return ($window.innerHeight + $window.scrollY < getDocumentHeight()) &&
            ((scrollVal - directionChangedVal) <= $scope.tolerance.down);
        }

        function clearTolerance() {
          directionChangedVal = null;
        }

        function getDocumentHeight() {
          return Math.max(
            document.body.scrollHeight, document.documentElement.scrollHeight,
            document.body.offsetHeight, document.documentElement.offsetHeight,
            document.body.clientHeight, document.documentElement.clientHeight
          );
        }

        function isOutOfDocumentBounds(scrollVal) {
          return scrollVal < 0 || ($window.innerHeight + $window.scrollY > getDocumentHeight());
        }
      }
    };
  }

  function generateRandomColor() {
    return'#' + Math.floor(Math.random() * 16777215).toString(16);
  }
  
  function createItemList(count) {
    var list = [];

    for (var i = 0; i < count; i++) {
      var item = {
        index: i + 1,
        bgColor: generateRandomColor()
      };
      
      list.push(item);
    }
    return list;
  }
})();
            
          
!
999px
Loading ..................

Console