




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第vue實現導航欄下拉菜單本文實例為大家分享了vue實現導航欄下拉菜單的具體代碼,供大家參考,具體內容如下
先看效果:
下拉菜單鋪滿全屏
div.../div
div.../div
.nav{
position:relative;
.dropdown-content{
position:absolute;
width:100%;
//拉滿
}
方法一:鼠標移入移出事件
使用的是vue的transition組件以及鼠標事件@mouseenter和@mouseleave
.dropdown-enter-active{
animation:expand-contract1sease;
.dropdown-leave-active{
animation:expand-contract1seasereverse;
@keyframesexpand-contract{
0%{
overflow:hidden;
opacity:0;
max-height:0;
100%{
max-height:300px;
//大于等于下拉菜單的高度
opacity:1;
}
優點:
1、結構層次清楚
2、多個導航需要下拉菜單,且結構相似內容不同,只需要重新渲染數據即可。
缺點:
1、使用了事件處理相對復雜
案例代碼
template
div
!--導航欄--
divref="navRef"
div@mouseenter="isShow=false"導航欄1/div
div@mouseenter="showDropDown('2')"導航欄2/div
div@mouseenter="showDropDown('3')"導航欄3/div
div@mouseenter="isShow=false"導航欄4/div
div@mouseenter="isShow=false"導航欄5/div
/div
!--下拉菜單--
transitionname="dropdown"
divv-show="isShow"@mouseleave="hideDropDown"
div
divv-for="(item,index)inanalog":key="index"
{{item}}
/div
/div
/div
/transition
/div
/template
script
exportdefault{
data(){
return{
isShow:false,
navTop:0,
//模擬下拉菜單內容
analog:[],
};
mounted(){
//導航欄距頁面高度=元素頂部距頁面距離+元素本身高度
this.navTop=
this.$refs.navRef.getBoundingClientRect().top+
this.$refs.navRef.offsetHeight;
methods:{
showDropDown(val){
if(!this.isShow)this.isShow=true;
if(val==="2"){
this.analog=["菜單1","菜單1","菜單1","菜單1","菜單1"];
}else{
this.analog=["菜單22","菜單22","菜單22","菜單22","菜單22"];
}
},
hideDropDown(e){
//e.pageY:鼠標指針相對頁面的偏移量
if(this.isShowe.pageY=this.navTop)this.isShow=false;
},
/script
stylelang="scss"scoped
//下拉菜單收縮展開
@keyframesexpand-contract{
0%{
opacity:0;
height:0;
//max-height:0;
100%{
opacity:1;
height:300px;
//max-height:300px;
//大于等于下拉菜單的高度
.dropdown-enter-active{
animation:expand-contract0.6s;
.dropdown-leave-active{
animation:expand-contract0.6sreverse;
//內容變化
@keyframesmenu{
0%{
opacity:0;
100%{
opacity:1;
//導航欄
.nav{
position:relative;
display:flex;
width:100%;
height:80px;
line-height:80px;
background-color:#eee;
border-bottom:1pxsolid#ccc;
.nav-item{
position:relative;
margin:020px;
cursor:pointer;
transition:all0.3slinear;
::before{
content:"";
position:absolute;
bottom:0;
left:0;
height:2px;
width:100%;
background-color:#1678e9;
transform:scale(0);
transition:all0.4slinear;
}
:hover{
color:#1678e9;
::before{
transform:scale(1);
}
}
.dropdown-content{
position:absolute;
width:100%;//拉滿
overflow:hidden;
.dropdown-menu{
padding:10px8px15px;
color:white;
background-color:rgba($color:#ccc,$alpha:0.5);
border-radius:4px;
/*animation:menu0.6s;*/
.menuItem{
width:100%;
white-space:nowrap;
padding:10px16px;
font-size:16px;
color:#000;
cursor:pointer;
transition:all0.3s;
border-radius:4px;
:hover{
background-color:#ccc;
}
}
/style
方法二:hover
將下拉菜單需要下拉的導航欄下一級下,使用hover控制元素,nav-item不要設置相對定位,以免定位時下拉菜單寬度不能100%鋪滿導航欄寬度。
將菜單初始高度設為0
優點:
1、簡單明了,不需要事件,js等操作
缺點:
1、每個下拉菜單獨立,也就是說切換導航欄,下拉菜單顯示隱藏也會動畫堆疊
2、每個導航標題都需要單獨寫下拉菜單,結構層次變多
案例代碼
template
div
!--導航欄--
div
divspan導航欄1/span/div
div
span導航欄2/span
!--下拉菜單--
div
div
div菜單1/div
div菜單菜單1/div
div菜單2/div
div菜單菜單菜單1/div
div菜單3/div
/div
/div
/div
divspan導航欄3/span/div
div
span導航欄4/span
!--下拉菜單--
div
div
div菜單1/div
div菜單菜單1/div
div菜單2/div
div菜單菜單菜單1/div
div菜單3/div
/div
/div
/div
divspan導航欄5/span/div
/div
/div
/template
script
exportdefault{
data(){
return{
isShow:false,
};
mounted(){},
methods:{},
/script
stylelang="scss"scoped
.nav{
position:relative;
display:flex;
width:100%;
height:80px;
line-height:80px;
background-color:#eee;
border-bottom:1pxsolid#ccc;
.nav-item{
//position:relative;
margin:020px;
cursor:pointer;
transition:all0.3slinear;
.nav-item-title{
position:relative;
display:block;
height:inherit;
width:inherit;
::before{
content:"";
position:absolute;
bottom:0;
left:0;
height:2px;
width:100%;
background-color:#1678e9;
transform:scale(0);
transition:all0.4slinear;
}
:hover{
color:#1678e9;
::before{
transform:scale(1);
}
}
}
:hover.dropdown-content{
height:300px;
}
//下拉菜單
.dropdown-content{
position:absolute;
top:80px;//為導航欄高度
left:0;//設置為0,不然會直接定位到父元素下方
width:100%;
height:0;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中藥制劑技術模擬試題+參考答案
- 上海中心大廈買賣合同
- 酒店住宿預訂取消政策協議
- 鋼筋檢測考試題及答案
- 浙江國企招聘2025臺州溫嶺市糧食收儲有限責任公司招聘14人筆試參考題庫附帶答案詳解
- 2025重慶金納動力科技有限公司招聘22人筆試參考題庫附帶答案詳解
- 2025浙江寧波交通工程建設集團有限公司招聘30人筆試參考題庫附帶答案詳解
- 2025河北唐山政務服務外包有限公司為服務項目招聘66人筆試參考題庫附帶答案詳解
- 加油站項目投資前景分析報告
- 透徹理解紡織工藝的試題及答案
- 2025屆廣東省廣州市省實教育集團中考生物對點突破模擬試卷含解析
- 河道疏浚及堤防工程施工重難點及相關技術保證措施
- 出國人員安全教育
- 2025年中石化招聘筆試參考題庫含答案解析
- 湖南省邵陽市2024年中考物理試卷(解析版)
- 2025年中考語文復習之小題狂練300題(選擇題):語法知識(20題)
- 天津中考英語2020-2024年5年真題匯編-教師版-專題07 完成句子
- 應用PDCA降低藥占比
- 風電場道路施工安全管理方案
- 漁業基礎設施與裝備現代化考核試卷
- 高一生物生物膜的流動鑲嵌模型練習題(含答案)
評論
0/150
提交評論