A few weeks ago stripe.com launched a new website design. It looks awesome. One thing we dig in particular is the morphing navigation dropdown: instead of hiding and showing a new dropdown “container” when the user switches from one navigation item to the other, they animate the dropdown background to make space for different content sizes.
We thought it would be interesting to explain how this effect is achieved, therefore we created our own version of Stripe navigation.
Original version: Stripe.com
Icons: Nucleoapp.com
Creating the structure
The HTML structure is composed of two main elements: the nav.main-nav
for the top navigation items and the div.morph-dropdown-wrapper
wrapping the dropdown elements.
For each list item in the nav.main-nav
element, a li.dropdown
is created inside the .morph-dropdown-wrapper
.
<header class="cd-morph-dropdown">
<a href="#0" class="nav-trigger">Open Nav<span aria-hidden="true"></span></a>
<nav class="main-nav">
<ul>
<li class="has-dropdown gallery" data-content="about">
<a href="#0">About</a>
</li>
<li class="has-dropdown links" data-content="pricing">
<a href="#0">Pricing</a>
</li>
<li class="has-dropdown button" data-content="contact">
<a href="#0">Contact</a>
</li>
</ul>
</nav>
<div class="morph-dropdown-wrapper">
<div class="dropdown-list">
<ul>
<li id="about" class="dropdown gallery">
<!-- dropdown content here -->
</li>
<li id="pricing" class="dropdown links">
<!-- dropdown content here -->
</li>
<li id="contact" class="dropdown button">
<!-- dropdown content here -->
</li>
</ul>
<div class="bg-layer" aria-hidden="true"></div>
</div> <!-- dropdown-list -->
</div> <!-- morph-dropdown-wrapper -->
</header>
An additional div.bg-layer
has been created inside the div.morph-dropdown-wrapper
and is used to create the dropdown morphing background.
Adding style
On small devices, the div.morph-dropdown-wrapper
is hidden by default; when a user clicks the menu icon, the .nav-open
class is added to the .cd-morph-dropdown
to reveal the navigation.
.cd-morph-dropdown {
position: relative;
}
.cd-morph-dropdown .morph-dropdown-wrapper {
display: none;
position: absolute;
top: 60px;
left: 0;
width: 100%;
}
.cd-morph-dropdown.nav-open .morph-dropdown-wrapper {
display: block;
}
On bigger devices (viewport width more than 1000px), the .dropdown-list
and the li.dropdown
elements are hidden by default.
@media only screen and (min-width: 1000px) {
.cd-morph-dropdown .dropdown-list {
position: absolute;
top: 0;
left: 0;
visibility: hidden;
}
.cd-morph-dropdown .dropdown {
position: absolute;
left: 0;
top: 0;
opacity: 0;
visibility: hidden;
width: 100%;
transition: opacity .3s, visibility .3s;
}
}
When a user hovers over one of the elements inside the nav.main-nav
, the .is-dropdown-visible
class is added to the .cd-morph-dropdown
and the .dropdown-list
visibility is changed to visible. At this point, the dropdown wrapper is visible, but its content is still hidden. To reveal the content selected by the user, the .active
class is added to the selected li.dropdown
element (the one with an id equal to the data-content of the navigation item the user is hovering over).
@media only screen and (min-width: 1000px) {
.cd-morph-dropdown .dropdown.active {
opacity: 1;
visibility: visible;
}
}
Since the li.dropdown
elements have an absolute position, they do not actually take space inside their parent (div.dropdown-list
), that means the div.dropdown-list
width and height do not change according to the visible content. Since its overflow property is set to hidden (so that no content is visible outside the dropdown wrapper), we use JavaScript to change its height and width to make sure the selected dropdown content is always visible.
To create the dropdown background, we use the div.bg-layer
. It has an absolute position, width and height equal to 1px and opacity of zero. When the .is-dropdown-visible
class is added to the .cd-morph-dropdown
, its opacity is changed to one and it is scaled up (using JavaScrip) to fit the visible content area.
@media only screen and (min-width: 1000px) {
.cd-morph-dropdown .bg-layer {
/* morph dropdown background */
position: absolute;
top: 0;
left: 0;
height: 1px;
width: 1px;
background: #FFFFFF;
opacity: 0;
transition: opacity .3s;
transform-origin: top left;
}
.cd-morph-dropdown.is-dropdown-visible .bg-layer {
opacity: 1;
transition: transform .3s, opacity .3s;
}
}
When the user moves from a navigation item to a different one, the scaleX and scaleY values of the div.bg-layer
are changed (using JavaScript) to create the morph effect.
Events handling
To implement this navigation, we created a morphDropdown
object and used the bindEvents
method to attach event handlers to the proper elements.
function morphDropdown( element ) {
this.element = element;
this.mainNavigation = this.element.find('.main-nav');
this.mainNavigationItems = this.mainNavigation.find('.has-dropdown');
this.dropdownList = this.element.find('.dropdown-list');
//...
this.bindEvents();
}
The bindEvents
method is used to detect the mouseenter/mouseleave events on the .has-dropdown
and .dropdown
elements.
morphDropdown.prototype.bindEvents = function() {
var self = this;
this.mainNavigationItems.mouseenter(function(event){
//hover over one of the nav items -> show dropdown
self.showDropdown($(this));
}).mouseleave(function(){
//if not hovering over a nav item or a dropdown -> hide dropdown
if( self.mainNavigation.find('.has-dropdown:hover').length == 0 && self.element.find('.dropdown-list:hover').length == 0 ) self.hideDropdown();
});
//...
};
The showDropdown
method takes care of changing the height, width and translateX values of the .dropdown-list
and to scale up/down the .bg-layer
element.
morphDropdown.prototype.showDropdown = function(item) {
var selectedDropdown = this.dropdownList.find('#'+item.data('content')),
selectedDropdownHeight = selectedDropdown.innerHeight(),
selectedDropdownWidth = selectedDropdown.children('.content').innerWidth(),
selectedDropdownLeft = item.offset().left + item.innerWidth()/2 - selectedDropdownWidth/2;
//update dropdown and dropdown background position and size
this.updateDropdown(selectedDropdown, parseInt(selectedDropdownHeight), selectedDropdownWidth, parseInt(selectedDropdownLeft));
//add the .active class to the selected .dropdown and .is-dropdown-visible to the .cd-morph-dropdown
//...
};
morphDropdown.prototype.updateDropdown = function(dropdownItem, height, width, left) {
this.dropdownList.css({
'-moz-transform': 'translateX(' + left + 'px)',
'-webkit-transform': 'translateX(' + left + 'px)',
'-ms-transform': 'translateX(' + left + 'px)',
'-o-transform': 'translateX(' + left + 'px)',
'transform': 'translateX(' + left + 'px)',
'width': width+'px',
'height': height+'px'
});
this.dropdownBg.css({
'-moz-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
'-webkit-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
'-ms-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
'-o-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
'transform': 'scaleX(' + width + ') scaleY(' + height + ')'
});
};