Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div class="sticky-wrap">
     <!-- This nav bar goes to the top of your hero section -->
      <header role="banner" itemscope="itemscope" itemtype="http://schema.org/WPHeader">
        <nav class="nav nav--primary">
          <ul class="nav__items">
            <li class="nav__item">
              <a class="title" href="/">
                Primary 1
              </a>
            </li>
            <li class="nav__item">
              <a class="title" href="/">
                Primary 2
              </a>
            </li>          
            <li class="nav__item">
              <a class="title" href="/">
               Primary 3
              </a>
            </li>          
            <li class="nav__item">
              <a class="title" href="/">
                Primary 4
              </a>
            </li>
          </ul>
        </nav>
      </header>
      <!-- This is your hero content -->
      <div class="hero hero__content">
        Amazing Hero content
      </div>
      <!-- This nav bar goes to the bottom of your hero section -->
      <nav class="nav nav--secondary">
        <ul class="nav__items nav__items--secondary">
          <li class="nav__sub-item">
            <a href="/" class="link">Secondary 1</a>
          </li>
          <li class="nav__sub-item">
            <a href="/" class="link">Secondary 2</a>
          </li>          
          <li class="nav__sub-item">
            <a href="/" class="link">Secondary 3</a>
          </li>          
          <li class="nav__sub-item">
            <a href="/" class="link">Secondary 4</a>
          </li>
        </ul>
      </nav>
    </div>
    <div class="content-wrap">
      Amazing body content
    </div>
    <div class="obi">
       <div style="width:100%;height:0;padding-bottom:50%;position:relative;"><iframe src="https://giphy.com/embed/Nx0rz3jtxtEre" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div>
      
    </div>
  </body>
</html>
              
            
!

CSS

              
                $color-white: white;
$color-green: #33d797;
$color-blue: #1c6cf0;
$color-navy: #1d1b2f;

@import url('https://fonts.googleapis.com/css?family=Lato');

*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  border: 0;
}

.sticky-wrap {
  z-index: 1;
  position: relative;
}

//base styles for both primary and secondary navs
.nav {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  position: relative;
  padding: 1rem;
  transition: 0.2s ease all;
  width: 100%;
  
  
   a {
     color: $color-white;
     text-decoration: none;
   }
  
    //single item within items (in the nav)
  &__item {
    padding: 0 0.5rem;
  }

  &__sub-item {
    padding: 0.5rem;
    text-align: center;
  }

  //colors of text and links for home navigation
  &--primary,
  &--secondary {
    color: $color-white;
  }
  
    //provides padding and margin for sticky header primary
  &--primary {
      background-color: $color-navy;
      padding: 1.3rem 3rem;
      position: fixed;
  }
  
  // secondary nav containing hot links to page content
  &--secondary {
      display: flex;
      justify-content: center;
      align-items: flex-start;
      padding: 0;
      margin: 0;
      background-color: $color-green;
  }

  //container for items within nav
  &__items {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    text-align: center;
    position: absolute;
    width: initial;
    left: 20%;
    right: 20%;
    top: initial;
    list-style: none;

    &--secondary {
      margin: 0;
      padding: 0.7rem 0;
      bottom: 0;
      width: 100%;
      left: 0;
      position: relative;
      transition: padding 0.3s ease;
    }
  }
}

//class that fixes top (primary) navigation to top of window on desktop
.nav-fixed {
  &--secondary {
    position: fixed;
    bottom: auto;
    background-color: $color-blue;
    
    
    //optional if you want to increase padding of the secondary nav once attached
    .nav__items--secondary {
      padding: 1rem 0;
    }
  }
}

.hero {
  background-color: coral;
  
  &__content {
    display: flex;
    justify-content: center;
    align-content: center;
    padding: 25rem;
    font-size: 7em;
    text-align: center;
    height: 100%;
    width: 100%;
    @media only screen and (max-width: 400px) {
      font-size: 3em;
      padding: 70% 20%;
    }
  }
}

.content-wrap {
  position: relative;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
  min-height: 1000px;
  font-size: 7em;
  @media only screen and (max-width: 400px) {
    font-size: 3em;
  }
}

.obi {
  padding: 5% 25%;
  @media only screen and (max-width: 400px) {
    padding: 5%;
  }
  
}

body {
  font-family: lato;
}
              
            
!

JS

              
                // Create variables for our primary and secondary navigations
const navPrimary = document.querySelector(".nav--primary");
const navSecondary = document.querySelector(".nav--secondary");
const stickyWrap = document.querySelector(".sticky-wrap");
const contentWrap = document.querySelector('.content-wrap');


// Get the size and position relative to the viewport of the primary nav
let navPrimaryHeight = navPrimary.getBoundingClientRect().height;

// Get the height of the secondary nav items
let navSecondaryHeight = navSecondary.getBoundingClientRect().height;

// Get the top position of the secondary nav items 
let navSecondaryTop = navSecondary.getBoundingClientRect().top;

// Get the whole hero section (.sticky-wrap) that wraps top nav, hero content and bottom nav, get it's bottom position and then take away the height of the secondary nav and add the number of pixels the doc has currently scrolled
let navSecondaryOrigPos = stickyWrap.getBoundingClientRect().bottom - navSecondaryHeight + window.pageYOffset;

// The function that ensures the nav shows if the user refreshes the page, scrolled past the hero section
function stickyNavInit() {
  navSecondaryTop = navSecondary.getBoundingClientRect().top;

  // If the top position of the secondary navigation items are less than or equal to the primary navigation's height
  
  if (navSecondaryTop <= navPrimaryHeight) {
    // add the fixed class to the secondary nav
    navSecondary.classList.add("nav-fixed--secondary");
    
    // add the height to the secondary nav as top
    navSecondary.style.top = navPrimaryHeight + "px";
    
    
    //add padding top to the web body to prevent jumping
    contentWrap.style.paddingTop = (navSecondaryTop + navPrimaryHeight) + "px";   
    
  }

  // If the window's scroll Y position and the height of the primary navigation are less than the whole hero section's bottom position
  
  if (window.scrollY + navPrimaryHeight < navSecondaryOrigPos) {
    
    // then remove the sticky class from secondary nav
    navSecondary.classList.remove("nav-fixed--secondary");
  
    
    // and remove the height of the navigation from the top
    navSecondary.removeAttribute("style");
    
    
    contentWrap.removeAttribute("style");
      
  }
}


// function that throttles the event listener (or any function you add)
function throttled(delay, fn) {
  let lastCall = 0;
  return function (...args) {
    const now = (new Date).getTime();
    if (now - lastCall < delay) {
      return;
    }
    lastCall = now;
    return fn(...args);
  }
}

// On scroll, fire the function
throttled(200, window.addEventListener("scroll", stickyNavInit));
              
            
!
999px

Console