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

              
                <nav id="main-nav">
  <ul>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Product List</a></li>
    <li class="last"><a href="#">Contact</a></li>
    <li class="last"><a href="#">FAQ</a></li>
    <li id="indicator"></li>
  </ul>
</nav>

<button id="reset" type="button">reset</button>
              
            
!

CSS

              
                $blue: #1b3d4e;
$copper: #c07053;
$snow: #f3efeb;
$stone: #cbc0ba;

body {
  box-sizing: border-box;
  background-color: $snow;
  font-family: "Unica One", cursive;
  font-size: 16px;
}

nav {
  max-width: 960px;
  padding: 2.666em 1em 1.333em 1em;
  position: relative;
  background-color: $blue;
  ul {
    list-style: none;
    margin: 0;
    padding-top: 1em;
    position: relative;
    display: flex;
    justify-content: flex-start;
    li {
      // padding: 0 1.5em;
      // border: 1px solid orange;
      a {
        font-size: 1.25em;
        line-height: 1;
        letter-spacing: 0.05em;
        transition: position 1s ease;
        text-decoration: none;
        text-transform: uppercase;
        // background-color: red;
        // display: inline-block;
        margin: 0 1em 0 0;
        color: $stone;
        &.active {
          color: $copper;
        }
      }
    }
    li.last {
      margin-right: 0;
    }
  } // ul
} // nav

#indicator {
  display: block;
  width: 100px;
  height: 3px;
  background-color: lighten($copper, 10%);
  position: absolute;
  bottom: -3px;
  left: -100px;
  margin: 0;
  padding: 0;
  transition: left 700ms ease, width 700ms ease, opacity 500ms ease;
  opacity: 0;
  &.active {
    opacity: 1;
  }
} // #indicator

#reset {
  position: fixed;
  bottom: 1em;
  right: 1em;
  background: none;
  border: none;
  font-family: inherit;
  text-transform: uppercase;
  background-color: $stone;
  padding: 0.5em 1em;
  display: none;
  &.show {
    display: block;
  }
}

              
            
!

JS

              
                // NOTES:
// Not really terribly flexible yet, as it requires a lot of hardcoding of values and it tricky to get working with right-aligned navs

//https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
// forEach method, could be shipped as part of an Object Literal/Module
function forEach(array, callback, scope) {
  for (var i = 0; i < array.length; i++) {
    callback.call(scope, i, array[i]); // passes back stuff we need
  }
}

// Cacheing DOM items
var indicator = document.getElementById("indicator"),
  nav = document.getElementById("main-nav"),
  navItems = document.querySelectorAll("nav > ul li a"),
  paddingCompensation = "1em",
  resetButton = document.getElementById("reset");

resetButton.addEventListener("click", function(event) {
  resetState(event);
});

// State
var activeState = false,
  activeLink;

// Default position of indicator, captured from styles
var defaultSettings = {
  left: window.getComputedStyle(indicator, null).getPropertyValue("left"),
  width: window.getComputedStyle(indicator, null).getPropertyValue("width")
};

// If a link is "active" set the indicator's position to it instead of the default
function activeLinkSet() {
  let thisPosition = activeLink.getBoundingClientRect();
  let thisWidth = activeLink.offsetWidth;
  let thisHeight = activeLink.offsetHeight;
  indicator.style.left = "calc(" + thisPosition.x + "px - 1em)";
  indicator.style.width = thisWidth + "px";
  indicator.classList.add("active");
}

// Returns to default on hover out
function returnToDefault() {
  indicator.style.left = defaultSettings.left;
  indicator.style.width = defaultSettings.width;
  indicator.classList.remove("active");
  resetButton.classList.remove("show");
}

// Logic for everything, should probably rename this since it's used on more than hover. Takes event, and a string for logic as parameters
function handleHover(e, direction) {
  if (direction === "set active") {
    // if we're setting active, run that function
    activeLinkSet();
  } else {
    // otherwise, check hover in or out
    if (direction === "in") {
      // if hover in, get the event target's settings and apply them.
      let thisPosition = e.currentTarget.getBoundingClientRect();
      let thisWidth = e.currentTarget.offsetWidth;
      let thisHeight = e.currentTarget.offsetHeight;
      // calc is here for difficulties with getBoundingClientRect and padding issues
      indicator.style.left =
        "calc(" + thisPosition.x + "px - " + paddingCompensation + ")";
      indicator.style.width = thisWidth + "px";
      indicator.classList.add("active");
    } else if (direction === "out") {
      // if hover out, set back to a default, depending on state of activeState
      if (activeState === true) {
        activeLinkSet();
      } else {
        returnToDefault();
      }
    }
  }
}

// for each lopps to add event listeners to links
forEach(navItems, function(index, value) {
  navItems[index].addEventListener("mouseenter", function(event) {
    handleHover(event, "in");
  });
  navItems[index].addEventListener("click", function(event) {
    handleClick(event);
  });
});

// hover out is actually on the nav element itself, not the links!!!
nav.addEventListener("mouseleave", function(event) {
  handleHover(event, "out");
});

// fake link clicking
// could be replaced by something simpler on a real multi-page site
function handleClick(e) {
  e.preventDefault();
  resetState();
  // add active to current target
  e.currentTarget.classList.add("active");
  // set state
  activeState = true;
  // set active link
  activeLink = e.currentTarget;
  // run logic function to set placement of indicator
  handleHover(e, "set active");
  // turn reset button on
  resetButton.classList.add("show");
}

function resetState() {
  activeState = false;
  // get all links
  let allLinks = document.querySelectorAll("nav a");
  // link is clicked, remove all active classes, just in case
  for (var i = 0; i < allLinks.length; i++) {
    allLinks[i].classList.remove("active");
  }
  returnToDefault();
}

              
            
!
999px

Console