Header Fade while scroll

Fade the header while scrolling

by Rohit Bisht

HTML

<div class="wrapper-parallax">
  <header class="site-header">
    <img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?w=940&h=650&auto=compress&cs=tinysrgb" width="250px" alt="Anna and James" class="logo">
  </header>

  <div class="content">
    <h1>Content</h1>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>


</script>

CSS

body {
  margin: 0;
  background: #f1f1f1;
}

header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: -1;
  height: 500px;
  line-height: 500px;
  text-align: center;
  margin: 0;
  padding: 0;
}

.wrapper-parallax {
  margin-top: 500px;
}

.content {
  border-top: 5px solid white;
  position: relative;
  z-index: 1;
  background: white;
  min-height: 2800px;
  background-image: url(https://i.imgur.com/DCXgzWc.png);
  background-position-y: top;
  background-repeat: no-repeat;
  background-size: 300%;
  background-position-x: center;
}

JavaScript

$(window).scroll(function(e) {
  var scrolled = $(window).scrollTop(),
    header = $('.site-header');
  header.css('top', -(scrolled) * 0.5 + 'px');
  var scrollPos = scrolled - ($(document).height() - $(window).height() - 470);
  var position = scrollPos / 10;
  var opacity = ((scrollPos / -1000) - 1.734) * 1.5;
  $('.site-header').css({
    "opacity": opacity,
    "position": position + '%'
  });
});