Skip to content

Instantly share code, notes, and snippets.

@stanwmusic
Last active February 5, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stanwmusic/0161a57b1543b49b985e01985dca9407 to your computer and use it in GitHub Desktop.
Save stanwmusic/0161a57b1543b49b985e01985dca9407 to your computer and use it in GitHub Desktop.
Follow Along Navigation

Follow Along Navigation

Inspired by the slick navigation on Stripe's site, this is a navigation that has a follow along background. We use JavaScript to add/remove classes to handle the toggle. Then we use getBoundingClientRect() to transform a white background as the user moves across each dropdown link.

A Pen by Stan Williams on CodePen.

License.

<main>
<header>
<h1>Follow Along Navigation</h1>
<p>Inspired by the slick navigation on Stripe's site, this is a navigation that has a follow along background. We use JavaScript to add/remove classes to handle the toggle. Then we use getBoundingClientRect() to transform a white background as the user moves across each dropdown link.</p>
</header>
<section class="content">
<nav class="top">
<div class="dropdownBackground">
<span class="arrow"></span>
</div>
<ul class="cool">
<li>
<a href="#">About Me</a>
<div class="dropdown dropdown1">
<div class="bio">
<img src="https://stanwilliams.org/wp-content/uploads/2018/01/Stan_Sweetwater_Gap_Beveled-256px.jpg">
<p>Mostly retired engineer, musician, electronics technician, web-developer, hiker, explorer, lover of nature, and amateur photographer. I'm an animal lover and just another of the 7 billion + humans on this planet. </p>
</div>
</div>
</li>
<li>
<a href="#">Links</a>
<ul class="dropdown courses">
<li>
<a href="https://stanwilliamsmusic.com.org">StanWilliamsMusic.com</a>
</li>
<li>
<a href="https://soundcloud.com/stanwmusic">Stan's music on Soundcloud</a>
</li>
<li>
<a href="https://www.youtube.com/watch?v=rjNahMTd7KQ&list=PL802C7FB249992F95" target="_blank">Music I've Listened To</a>
</li>
<li>
<a href="https://stan.stream" target="_blank">My new blog</a>
</li>
</ul>
</li>
<li>
<a href="#">Social Media</a>
<ul class="dropdown dropdown3">
<li><a class="button" href="http://twitter.com/stanssongs" target="_blank">Twitter</a></li>
<li><a class="button" href="http://facebook.com/stanwilliamsmusic" target="_blank">Facebook</a></li>
<li><a class="button" href="https://linkedin.com/in/stanwilliams1" target="_blank">LinkedIn</a></li>
</ul>
</li>
</ul>
</nav>
</section>
</main>
const triggers = document.querySelectorAll('.cool > li');
const background = document.querySelector('.dropdownBackground');
const nav = document.querySelector('.top');
function handleEnter() {
this.classList.add('trigger-enter');
setTimeout(() => {
if(this.classList.contains('trigger-enter')) {
this.classList.add('trigger-enter-active');
// prevents content from showing if you quickly move around
}
}, 150);
background.classList.add('open');
const dropdown = this.querySelector('.dropdown'); // have to do this here because the dropdown can change
const dropdownCoords = dropdown.getBoundingClientRect(); // relative to the page, can change based on other page content
const navCoords = nav.getBoundingClientRect();
const coords = {
height: dropdownCoords.height,
width: dropdownCoords.width,
top: dropdownCoords.top - navCoords.top, // offset for non-anchored
left: dropdownCoords.left - navCoords.left
};
background.style.setProperty('width', `${coords.width}px`);
background.style.setProperty('height', `${coords.height}px`)
background.style.setProperty('transform', `translate(${coords.left}px, ${coords.top}px`)
}
function handleLeave() {
this.classList.remove('trigger-enter', 'trigger-enter-active');
background.classList.remove('open');
}
triggers.forEach(trigger => trigger.addEventListener('mouseenter', handleEnter));
triggers.forEach(trigger => trigger.addEventListener('mouseleave', handleLeave));
.content {
background: none;
box-shadow: none;
height: 500px;
}
nav {
position: relative;
perspective: 600px;
}
.cool > li > a {
color: $red;
text-decoration: none;
font-size: 20px;
background: white;
padding: 10px 20px;
display: inline-block;
margin: 20px;
border-radius: 5px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
}
.cool > li {
position: relative;
display: flex;
justify-content: center;
}
.dropdown {
opacity: 0;
position: absolute;
overflow: hidden;
padding: 20px;
top: -20px;
border-radius: 2px;
transition: all 0.5s;
transform: translateY(100px);
will-change: opacity;
display: none;
}
.trigger-enter .dropdown {
display: block;
}
.trigger-enter-active .dropdown {
opacity: 1;
}
.dropdownBackground {
width: 100px;
height: 100px;
position: absolute;
background: #fff;
border-radius: 4px;
box-shadow: 0 50px 100px rgba(50,50,93,.1), 0 15px 35px rgba(50,50,93,.15), 0 5px 15px rgba(0,0,0,.1);
transition: all 0.3s, opacity 0.1s, transform 0.2s;
transform-origin: 50% 0;
display: flex;
justify-content: center;
opacity:0;
}
.dropdownBackground.open {
opacity: 1;
}
.arrow {
position: absolute;
width: 20px;
height: 20px;
display: block;
background: white;
transform: translateY(-50%) rotate(45deg);
}
.bio {
min-width: 500px;
display: flex;
justify-content: center;
align-items: center;
line-height: 1.7;
}
.bio img {
float: left;
margin-right: 20px;
max-width: 40%;
}
.bio p {
margin-bottom: 0;
}
.courses {
min-width: 300px;
}
.courses li {
padding: 10px 0;
display: block;
border-bottom: 1px solid rgba(0,0,0,0.2);
}
.dropdown a {
text-decoration: none;
}
a.button {
background: black;
display: block;
padding: 10px;
color: $gray-lightest;
margin-bottom: 10px;
&:hover {
color: $white;
}
}
/* Matches Twitter, TWITTER, twitter, tWitter, TWiTTeR... */
.button[href*=twitter] { background: #019FE9; }
.button[href*=facebook] { background: #3B5998; }
.button[href*=linkedin] { background: #0077B5; }
<link href="https://codepen.io/colinlord/pen/db9ba42a2fd1bb4ad3b2c4898897abea" rel="stylesheet" />
@stanwmusic
Copy link
Author

@stanwmusic
Copy link
Author

stanwmusic commented Feb 5, 2020

Demo or fork this and add your own info and images etc.: https://codepen.io/Stanssongs/full/WNveYvO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment