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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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