Sticky Nav Shrink Hero

By Rafi

F6
#
How do I use This?

A hero that shrinks away on scroll allowing the navigation to be sticky.

HTML
<header class="sticky-shrinknav-header">
  <h1 class="sticky-shrinknav-header-title">sticky-shrinknav</h1>
  <ul class="menu align-center sticky-shrinknav-menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Gallery</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</header>

$sticky-shrinknav-menu-height: rem-calc(60);
$sticky-shrinknav-hero-height: 300px;


body {
  padding-top: $sticky-shrinknav-hero-height + 30px;
}

.sticky-shrinknav-menu {
  @include horizontal-center;
  bottom: 0;
  height: $sticky-shrinknav-menu-height;
  line-height: $sticky-shrinknav-menu-height;
  width: 100%;
  background-color: rgba($primary-color, 0.1);
  transition: all 0.5s ease;

  li {
    border-radius: 2px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 0 0 1px $white;
    }
  }

  a {
    color: $white;
  }
}

.sticky-shrinknav-header-title {
  transition: all 0.3s ease;
  position: relative;
  transform: translateY(-$sticky-shrinknav-menu-height/2);
  margin-bottom: 0;
  color: $white;
}

.sticky-shrinknav-header {
  width: 100%;
  height: $sticky-shrinknav-hero-height;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lighten($primary-color, 10%);
  text-align: center;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
  transition: all 0.5s ease;
}

body.sticky-shrinknav-wrapper {
  padding-top: 130px;

  .sticky-shrinknav-header{
    height: $sticky-shrinknav-menu-height;
    background-color: rgba($primary-color, 0.9);

    .sticky-shrinknav-header-title{
      transform: scale(0);
      transition: all 0.3s ease;
    }
  }
}


body {
  padding-top: 330px;
}

.sticky-shrinknav-menu {
  position: absolute;
  left: 50%;
  -webkit-transform: translateX(-50%);
      -ms-transform: translateX(-50%);
          transform: translateX(-50%);
  bottom: 0;
  height: 3.75rem;
  line-height: 3.75rem;
  width: 100%;
  background-color: rgba(23, 121, 186, 0.1);
  transition: all 0.5s ease;
}

.sticky-shrinknav-menu li {
  border-radius: 2px;
  transition: all 0.3s ease;
}

.sticky-shrinknav-menu li:hover {
  box-shadow: 0 0 0 1px #fefefe;
}

.sticky-shrinknav-menu a {
  color: #fefefe;
}

.sticky-shrinknav-header-title {
  transition: all 0.3s ease;
  position: relative;
  -webkit-transform: translateY(-1.875rem);
      -ms-transform: translateY(-1.875rem);
          transform: translateY(-1.875rem);
  margin-bottom: 0;
  color: #fefefe;
}

.sticky-shrinknav-header {
  width: 100%;
  height: 300px;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-align-items: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-justify-content: center;
      -ms-flex-pack: center;
          justify-content: center;
  background-color: #2196e3;
  text-align: center;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
  transition: all 0.5s ease;
}

body.sticky-shrinknav-wrapper {
  padding-top: 130px;
}

body.sticky-shrinknav-wrapper .sticky-shrinknav-header {
  height: 3.75rem;
  background-color: rgba(23, 121, 186, 0.9);
}

body.sticky-shrinknav-wrapper .sticky-shrinknav-header .sticky-shrinknav-header-title {
  -webkit-transform: scale(0);
      -ms-transform: scale(0);
          transform: scale(0);
  transition: all 0.3s ease;
}

JS
$(function() {
  $(window).scroll(function() {
    var winTop = $(window).scrollTop();
    if (winTop >= 30) {
      $("body").addClass("sticky-shrinknav-wrapper");
    } else{
      $("body").removeClass("sticky-shrinknav-wrapper");
    }
  });
});