未加星标

Stripe.com Navigation

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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 .

<headerclass="cd-morph-dropdown"> <ahref="#0" class="nav-trigger">Open Nav<spanaria-hidden="true"></span></a> <navclass="main-nav"> <ul> <liclass="has-dropdown gallery" data-content="about"> <ahref="#0">About</a> </li> <liclass="has-dropdown links" data-content="pricing"> <ahref="#0">Pricing</a> </li> <liclass="has-dropdown button" data-content="contact"> <ahref="#0">Contact</a> </li> </ul> </nav> <divclass="morph-dropdown-wrapper"> <divclass="dropdown-list"> <ul> <liid="about" class="dropdown gallery"> <!-- dropdown content here --> </li> <liid="pricing" class="dropdown links"> <!-- dropdown content here --> </li> <liid="contact" class="dropdown button"> <!-- dropdown content here --> </li> </ul> <divclass="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 auser clicks the menuicon, 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 widthto 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 ofchanging 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

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaJavaScriptHTML
分页:12
转载请注明
本文标题:Stripe.com Navigation
本站链接:http://www.codesec.net/view/481753.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)