Background Fix Video

by Manikandan Kalaivanan

HTML

<nav>
  <div class="bg"></div>
  <ul>
    <li><a href="#">Menu 1</a></li>
    <li>Menu 2</li>
    <li>Menu 3</li>
  </ul>
</nav>
<div class="section-1">
  <video autoplay muted loop>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
<source src="https://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg">
</video>
</div>
<div class="section-2">
  <h2>Hello</h2>
</div>

SCSS

body{
  margin: 0;
  padding:0;
}

nav {
  position: fixed;
  width: 100%;
  .bg {
    position: absolute;
    width: 100%;
    top: 0;
    height: 0;
    background-color: black;
    z-index: -1;
    transition: height .2s linear;
  }
  .active {    
    height: 80px;
  }
  ul {
    list-style: none;
    padding-left: 0;
    li {
      display: inline-block;
      background-color: dimgrey;
      padding: 15px;
      margin-right: 15px;
    }
  }
}

.section-1 {
  height: 100vh;
  video {
    min-width: 100%;
    position: fixed;
    top: 0;
    z-index: -1;
    height: 100vh;
  }
}

.section-2 {
  z-index: 2;
  background-color: #000;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

JavaScript

$(window).scroll(function() {
  const scrollTop = $(window).scrollTop();
  if (scrollTop > 50) {
    $('nav .bg').addClass('active')
  } else {
    $('nav .bg').removeClass('active')
  }

});