cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <nav class="nav">
  <ul class="nav__list">
    <li class="nav__item">
      <a class="nav__link is-active" href="#dashboard">Dashboard</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#lorem">Lorem</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#dolor">Doloripsumdot</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#thing">Thing</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#other-thing">Other Thing</a>
    </li>
  </ul>
  <div class="nav__shadow nav__shadow--start"></div>
  <div class="nav__shadow nav__shadow--end"></div>
</nav>

<div id="dashboard" class="container page is-active">
  <h1>Dashboard</h1>
  <h2><a href="#lorem">Lorem</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#dolor">Doloripsumdot</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#thing">Thing</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#other-thing">Other Thing</a></h2>
  <p>Lorem Ipsum</p>
</div>

<div id="lorem" class="container page">
  <h1>Lorem</h1>
</div>

<div id="dolor" class="container page">
  <h1>Doloripsumdot</h1>
</div>

<div id="thing" class="container page">
  <h1>Thing</h1>
</div>

<div id="other-thing" class="container page">
  <h1>Other thing</h1>
</div>

            
          
!
            
              $nav-height: 3em;
$nav-scrollbar-height: 1.5em;
$nav-shadow-width: 4.5em;

.nav {
  position: relative;
  height: $nav-height;
  display: flex;
  align-items: center;

  @media (pointer: coarse) {
    // Hide the scrollbar on devices with touch input.
    overflow: hidden;
  }
}

.nav__list {
  position: relative;
  display: flex;
  margin-left: 1em;
  margin-right: 1em;
  padding: 0;
  align-items: center;
  list-style-type: none;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;

  @media (pointer: coarse) {
    // Move the scrollbar outside of the visible area
    // by making the element taller than the parent element.
    height: $nav-height + $nav-scrollbar-height;
  }
}

.nav__item {
  flex-shrink: 0;
  
  &:not(:first-child) {
    margin-left: 1em;
  }
}

.nav__link {
  display: inline-flex;
  height: $nav-height;
  align-items: center;
  text-decoration: none;
  
  &.is-active {
    font-weight: bold;
  }
}

.nav__shadow {
  width: $nav-shadow-width;
  height: $nav-height;
  position: absolute;
  top: 0;
  // Using 0% rgba value instead of transparent because of Safari.
  background: linear-gradient(to right, rgba(#fff, 0), #fff 80%);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  
  &.is-visible {
    opacity: 1;
  }
}

.nav__shadow--start {
  left: 0;
  transform: rotate(180deg);
}

.nav__shadow--end {
  right: 0;
}

.page {
  display: none;
  
  &.is-active {
    display: block;
  }
}

.container {
  padding-right: 1em;
  padding-left: 1em;
}

body {
  background: #fff;
  margin: 2em auto;
  width: 20em;
  padding-top: 1em;
  padding-bottom: 1em;
  border: 0.25em solid #444;
  border-radius: 1em;
}

hr {
  margin-top: 1em;
  margin-bottom: 1em;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
            
          
!
            
              const $navList = document.querySelector('.nav__list');
const $body = document.querySelector('body');
const $shadowStart = document.querySelector('.nav__shadow--start');
const $shadowEnd = document.querySelector('.nav__shadow--end');

function activatePage($page) {
  const $activePage = document.querySelector('.page.is-active');
  
  if ($activePage) {
    $activePage.classList.remove('is-active');
  }
  
  $page.classList.add('is-active');
}

function activateItem($item, $container) {
  const href = $item.getAttribute('href');
  const $navItem = $container.querySelector(`[href="${href}"]`);
  const $activeItem = $container.querySelector('a.is-active');
  const $page = document.querySelector(href);
  
  if ($activeItem) {
    $activeItem.classList.remove('is-active');
  }
  
  $navItem.classList.add('is-active');

  activatePage($page);
}

function handleShadowVisibility() {
  const maxScrollStartReached = $navList.scrollLeft <= 0;
  const maxScrollEndReached = $navList.scrollLeft >= $navList.scrollWidth - $navList.offsetWidth;

  toggleShadow($shadowStart, maxScrollStartReached);
  toggleShadow($shadowEnd, maxScrollEndReached);
}

function toggleShadow($el, maxScrollReached) {
  const shadowIsVisible = $el.classList.contains('is-visible');
  const showShadow = !maxScrollReached && !shadowIsVisible;
  const hideShadow = maxScrollReached && shadowIsVisible;

  // Using requestAnimationFrame for optimal scroll performance.
  // https://stackoverflow.com/a/44779316
  if (showShadow) {
    window.requestAnimationFrame(() => $el.classList.add('is-visible'));
  } else if (hideShadow) {
    window.requestAnimationFrame(() => $el.classList.remove('is-visible'));
  }
}

$navList.addEventListener('scroll', (e) => handleShadowVisibility(e));

$body.addEventListener('click', (e) => {
  const $target = e.target;
  const isLink = $target.getAttribute('href').indexOf('#') === 0;
  
  if (isLink) {
    e.preventDefault();
    activateItem($target, $navList);
  }
});

handleShadowVisibility();

            
          
!
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