vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單_第1頁(yè)
vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單_第2頁(yè)
vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單_第3頁(yè)
vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單_第4頁(yè)
vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單本文實(shí)例為大家分享了vue實(shí)現(xiàn)導(dǎo)航欄下拉菜單的具體代碼,供大家參考,具體內(nèi)容如下

先看效果:

下拉菜單鋪滿全屏

div.../div

div.../div

.nav{

position:relative;

.dropdown-content{

position:absolute;

width:100%;

//拉滿

}

方法一:鼠標(biāo)移入移出事件

使用的是vue的transition組件以及鼠標(biāo)事件@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;

}

優(yōu)點(diǎn):

1、結(jié)構(gòu)層次清楚

2、多個(gè)導(dǎo)航需要下拉菜單,且結(jié)構(gòu)相似內(nèi)容不同,只需要重新渲染數(shù)據(jù)即可。

缺點(diǎn):

1、使用了事件處理相對(duì)復(fù)雜

案例代碼

template

div

!--導(dǎo)航欄--

divref="navRef"

div@mouseenter="isShow=false"導(dǎo)航欄1/div

div@mouseenter="showDropDown('2')"導(dǎo)航欄2/div

div@mouseenter="showDropDown('3')"導(dǎo)航欄3/div

div@mouseenter="isShow=false"導(dǎo)航欄4/div

div@mouseenter="isShow=false"導(dǎo)航欄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,

//模擬下拉菜單內(nèi)容

analog:[],

};

mounted(){

//導(dǎo)航欄距頁(yè)面高度=元素頂部距頁(yè)面距離+元素本身高度

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:鼠標(biāo)指針相對(duì)頁(yè)面的偏移量

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;

//內(nèi)容變化

@keyframesmenu{

0%{

opacity:0;

100%{

opacity:1;

//導(dǎo)航欄

.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

將下拉菜單需要下拉的導(dǎo)航欄下一級(jí)下,使用hover控制元素,nav-item不要設(shè)置相對(duì)定位,以免定位時(shí)下拉菜單寬度不能100%鋪滿導(dǎo)航欄寬度。

將菜單初始高度設(shè)為0

優(yōu)點(diǎn):

1、簡(jiǎn)單明了,不需要事件,js等操作

缺點(diǎn):

1、每個(gè)下拉菜單獨(dú)立,也就是說切換導(dǎo)航欄,下拉菜單顯示隱藏也會(huì)動(dòng)畫堆疊

2、每個(gè)導(dǎo)航標(biāo)題都需要單獨(dú)寫下拉菜單,結(jié)構(gòu)層次變多

案例代碼

template

div

!--導(dǎo)航欄--

div

divspan導(dǎo)航欄1/span/div

div

span導(dǎo)航欄2/span

!--下拉菜單--

div

div

div菜單1/div

div菜單菜單1/div

div菜單2/div

div菜單菜單菜單1/div

div菜單3/div

/div

/div

/div

divspan導(dǎo)航欄3/span/div

div

span導(dǎo)航欄4/span

!--下拉菜單--

div

div

div菜單1/div

div菜單菜單1/div

div菜單2/div

div菜單菜單菜單1/div

div菜單3/div

/div

/div

/div

divspan導(dǎo)航欄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;//為導(dǎo)航欄高度

left:0;//設(shè)置為0,不然會(huì)直接定位到父元素下方

width:100%;

height:0;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論