JS實(shí)現(xiàn)快速的導(dǎo)航下拉菜單動(dòng)畫(huà)效果附源碼下載
這是一個(gè)帶變形動(dòng)畫(huà)特效的下拉導(dǎo)航菜單特效。該導(dǎo)航菜單在菜單項(xiàng)之間切換時(shí),下拉菜單會(huì)快速的根據(jù)菜單內(nèi)容的大小來(lái)動(dòng)態(tài)變形,顯示合適的下拉菜單大小,效果非常棒。

HTML
該導(dǎo)航菜單的HTML結(jié)構(gòu)如下:
<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>
CSS樣式請(qǐng)參照源碼中的css/style.css文件。
Javascript
為了實(shí)現(xiàn)這個(gè)導(dǎo)航菜單,特效中創(chuàng)建了一個(gè)morphDropdown對(duì)象。并使用bindEvents ()方法來(lái)處理元素的事件。
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()方法用于在.has-dropdown和.dropdown元素上檢測(cè)鼠標(biāo)進(jìn)入和鼠標(biāo)離開(kāi)事件。
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方法用于處理寬度、高度和.dropdown-list元素的translateX值,以及放大和縮小.bg-layer元素。
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 + ')'
});
};
以上所述是小編給大家介紹的JS實(shí)現(xiàn)快速的導(dǎo)航下拉菜單動(dòng)畫(huà)效果附源碼下載,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JS中用三種方式實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單
- JS 實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單的幾種方式
- 基于JS快速實(shí)現(xiàn)導(dǎo)航下拉菜單動(dòng)畫(huà)效果附源碼下載
- javascript仿京東導(dǎo)航左側(cè)分類(lèi)導(dǎo)航下拉菜單效果
- js實(shí)現(xiàn)兼容性好的微軟官網(wǎng)導(dǎo)航下拉菜單效果
- 頂部緩沖下拉菜單導(dǎo)航特效的JS代碼
- jquery仿京東導(dǎo)航/仿淘寶商城左側(cè)分類(lèi)導(dǎo)航下拉菜單效果
- jQuery實(shí)現(xiàn)的簡(jiǎn)潔下拉菜單導(dǎo)航效果代碼
- jquery實(shí)現(xiàn)簡(jiǎn)單的二級(jí)導(dǎo)航下拉菜單效果
- 原生JS實(shí)現(xiàn)導(dǎo)航下拉菜單效果
相關(guān)文章
js實(shí)現(xiàn)懸浮窗效果(支持拖動(dòng))
本文主要介紹了js實(shí)現(xiàn)懸浮窗效果(支持拖動(dòng))的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
小程序拖動(dòng)區(qū)域?qū)崿F(xiàn)排序效果
這篇文章主要為大家詳細(xì)介紹了小程序拖動(dòng)區(qū)域?qū)崿F(xiàn)排序效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
JavaScript?Promise執(zhí)行流程深刻理解
這篇文章主要介紹了JavaScript?Promise執(zhí)行流程深刻理解,他是一個(gè)構(gòu)造函數(shù),每個(gè)創(chuàng)建的promise都有各自狀態(tài)和值,且狀態(tài)初始值為pending,值為undefined2022-06-06
bootstrap-Treeview實(shí)現(xiàn)級(jí)聯(lián)勾選
這篇文章主要為大家詳細(xì)介紹了bootstrap-Treeview實(shí)現(xiàn)級(jí)聯(lián)勾選,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JS無(wú)限樹(shù)狀列表實(shí)現(xiàn)代碼
本人水平有限,這么個(gè)東東想了好幾天才弄出來(lái)個(gè)半成品,還有一些問(wèn)題沒(méi)有解決,先放上來(lái)存著備忘,也望高手指點(diǎn)一二。2011-01-01
prettier自動(dòng)格式化去換行的實(shí)現(xiàn)代碼
這篇文章主要介紹了prettier自動(dòng)格式化去換行的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
javascript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊生成文字特效
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊生成文字特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
微信小程序?qū)崿F(xiàn)MUI數(shù)字輸入框效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)MUI數(shù)字輸入框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

