css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="main">
<header class="header">
  <div class="hero">
    <div class="container">
      <h1>Let's Build: With JavaScript - Sticky Nav</h1>
    </div>
  </div>
  <nav class="primary-nav">
    <ul class="nav">
      <li><a href="#"><svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><title>sun</title><path d="M24 7.029L19 5l-2-5-5 2-5-2-2 5-5 2.029L2 12l-2 4.971L5 19l2 5 5-2 5 2 2-5 5-2.029L22 12zM12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12z"/></svg></a></li>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Links</a></li>
      <li><a href="#">Blog</a></li>
      <li><a href="#">Contact</a></li>
      <li class="hire-me"><a href="#">Hire me!</a></li>
    </ul>
  </nav> 
</header>

<div class="content">
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis, amet adipisci! Mollitia porro, esse fuga eos rem enim vel dolorem, numquam, necessitatibus nobis animi quis? Voluptatem, ipsum quos. Libero, maxime.</p>
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui non quibusdam sint nihil excepturi rerum maiores iusto dolorem id neque itaque vero, deleniti, vel possimus? Quidem asperiores fugit veritatis assumenda?</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nostrum ducimus fuga numquam modi, voluptas mollitia sit consectetur est aperiam a eaque. Ex fugit vero unde repellat officiis quae rerum cumque.>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nostrum ducimus fuga numquam modi, voluptas mollitia sit consectetur est aperiam a eaque. Ex fugit vero unde repellat officiis quae rerum cumque.</p>
    
<p>Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Maecenas faucibus mollis interdum. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</p>

<p>Maecenas faucibus mollis interdum. Nullam id dolor id nibh ultricies vehicula ut id elit. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Nullam id dolor id nibh ultricies vehicula ut id elit. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Maecenas faucibus mollis interdum. Aenean lacinia bibendum nulla sed consectetur.</p>

<p>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean lacinia bibendum nulla sed consectetur. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>

<p>Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Etiam porta sem malesuada magna mollis euismod. Donec id elit non mi porta gravida at eget metus.</p>
  
<p>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean lacinia bibendum nulla sed consectetur. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>

<p>Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Etiam porta sem malesuada magna mollis euismod. Donec id elit non mi porta gravida at eget metus.</p>
</div>
</div>
            
          
!
            
              .container {
  max-width: 1200px;
  margin: 0 auto;
}

.hero {
  background-image: url(https://images.unsplash.com/photo-1479621051492-5a6f9bd9e51a?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=282eb7b3a40113da0e089027d036a796);
  background-size: cover;
  background-position: bottom center;
  background-repeat: no-repeat;
  background-color: #222;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  color: white;
  min-height: 450px;
  
  h1 {
    text-shadow: 0 0 1px rgba(black, .1);
  }
  
}


nav {
  background: #242424;
  padding: 20px 0;
  position: relative;
}

.nav {
  display: flex;
  align-items: center;
  list-style: none;
  justify-content: space-between;
  max-width: 800px;
  margin: 0 auto;
  padding: 0;

  
  @media screen and (max-width: 767px) {
    flex-direction: column;
    li {
      padding: 10px 0;
    }
  }
  
  a {
    color: white;
    text-decoration: none;
    
    svg { fill: white; }
  }
  
  .hire-me { 
    position: absolute;
    right: 20px;
    display: none;
      -webkit-animation: fadein 1s;
       -moz-animation: fadein 1s;
     animation: fadein 1s;
    
    a {
      background: rgb(131,58,180);
background: linear-gradient(90deg, rgba(131,58,180,1) 0%, rgba(252,72,69,1) 100%);
      padding: 10px 16px;
      border-radius: 3px;
      transition: all .3s ease;
      
      &:hover {
        filter: brightness(120%);
      }
     
    }
  }
}

.content {
  max-width: 800px;
  padding: 20px;
  margin: 0 auto;
  line-height: 1.5;
}

// You could add CSS to offset the fixed position or in our JS we use an exact amount thanks to offsetHeight
// .fixed .content {
//   padding-top: 68px;
// }

.fixed .primary-nav {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  box-shadow: 0 3px 6px rgba(black, .3);
}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}
            
          
!
            
              /* Let's Build: With JavaScript - Web-Crunch.com
   Subscribe on YouTube - https://youtube.com/c/webcrunch
   
   Let's Build: Sticky Nav 
*/

const nav = document.querySelector('.primary-nav');
const topToNav = nav.offsetTop;
const main = document.querySelector('.main');

function stickyNav() {
  if (window.scrollY >= topToNav) {
      main.style.paddingTop = nav.offsetHeight + 'px';
      main.classList.add('fixed');
      document.querySelector('.hire-me').style.display = "block";
  } else {
      main.style.paddingTop = 0;
      main.classList.remove('fixed');
      document.querySelector('.hire-me').style.display = "none";
  }
}


window.addEventListener('scroll', stickyNav);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console