English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This is a dropdown navigation menu with deformation animation effect. When the navigation menu switches between menu items, the dropdown menu quickly deforms dynamically according to the size of the menu content, displaying the appropriate dropdown menu size, which looks very good.
The fast navigation dropdown menu animation effect is as follows:
Effect Demonstration Source Code Download
HTML
The HTML structure of the navigation menu is as follows:
<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>
For CSS styles, refer to the css in the source code./style.css file.
Javascript
To implement this navigation menu, a morphDropdown object was created in the effect, and the bindEvents () method was used to handle element events.
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(); } bindEvents() method is used to bind events on .has-detect mouse enter and mouse leave events on .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(); }); //... }; showDropdown method is used to handle width, height, and .dropdown-list element's translateX value, as well as zoom and shrink.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 + '); }); };
The above is the animation effect of the navigation dropdown menu based on JS implemented quickly, with source code download provided by the editor. Hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Also, I would like to express my sincere gratitude to everyone for their support of the Yelling Tutorial website!