vue實現(xiàn)三級導(dǎo)航展示和隱藏
本文實例為大家分享了vue實現(xiàn)三級導(dǎo)航展示和隱藏的具體代碼,供大家參考,具體內(nèi)容如下
需求描述:
要實現(xiàn)側(cè)邊欄三級導(dǎo)航的顯示和隱藏。點擊其中一個一級導(dǎo)航,展示該一級導(dǎo)航的二級導(dǎo)航,再點擊關(guān)閉該二級導(dǎo)航。一級導(dǎo)航的其他項,展開二級導(dǎo)航。關(guān)閉其他一級導(dǎo)航的二級導(dǎo)航。
效果如下:

代碼:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
<div class="first" v-for="(item, key) in navLists" :key="key">
<li>
<span @click="handleClick(key)"> {{ item.title }}</span>
<div
v-for="(item2, key2) in item.child"
:key="key2"
class="second"
v-show="show2 && currIndex == key"
>
<p @click="secondClick(key2)">{{ item2.subTitle }}</p>
<div
v-for="(item3, key3) in item2.threeChild"
:key="key3"
class="three"
v-show="show3 && currIndex2 == key2"
>
{{ item3.threeTitle }}
</div>
</div>
</li>
</div>
</div>
</template>
<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
name: "App",
components: {
HelloWorld,
},
data() {
return {
i: 0,
show3: false,
show2: false,
navLists: [
{
title: "項目信息",
child: [
{
subTitle: "項目簡介",
esubTitle: "#projectIntroduction",
threeChild: [
{ threeTitle: "三級導(dǎo)航" },
{ threeTitle: "三級導(dǎo)航" },
{ threeTitle: "三級導(dǎo)航" },
],
},
{
subTitle: "樣品信息",
threeChild: [
{ threeTitle: "三級導(dǎo)航22" },
{ threeTitle: "三級導(dǎo)航22" },
{ threeTitle: "三級導(dǎo)航22" },
],
},
{
subTitle: "樣品信息",
threeChild: [
{ threeTitle: "三級導(dǎo)航33" },
{ threeTitle: "三級導(dǎo)航33" },
{ threeTitle: "三級導(dǎo)航33" },
],
},
],
},
{
title: "項目信息2",
child: [
{
subTitle: "項目簡介22",
threeChild: [
{ threeTitle: "三級導(dǎo)航44" },
{ threeTitle: "三級導(dǎo)44" },
{ threeTitle: "三級導(dǎo)航22" },
],
},
{
subTitle: "樣品信息22",
},
],
},
{
title: "項目信息3",
eTitle: "#projectMessage",
child: [
{
subTitle: "項目簡介33",
esubTitle: "#projectIntroduction",
},
{
subTitle: "樣品信息33",
esubTitle: "#sampleInformation",
},
],
},
{
title: "項目信息2",
child: [
{
subTitle: "項目簡介22",
},
{
subTitle: "樣品信息22",
},
],
},
{
title: "項目信息3",
child: [
{
subTitle: "項目簡介33",
},
{
subTitle: "樣品信息33",
},
],
},
],
currIndex: "", //當前索引
spanIndex: [], //索引數(shù)組
arrIndex: "", //用于判斷是否做索引數(shù)組找到當前索引。-1為找不到,0找到了。
currIndex2: "", //二級導(dǎo)航當前索引
spanIndex2: [], //索引數(shù)組
arrIndex2: "", //用于判斷是否做索引數(shù)組找到當前索引。-1為找不到,0找到了。
};
},
methods: {
handleClick(index) {
// 初始化三級導(dǎo)航,默認不顯示。
this.show3 =false;
this.spanIndex2.splice(-1, 1);
// 當前索引=index
this.currIndex = index;
console.log("當前索引index", index);
// 判斷當前索引是否在索引數(shù)組spanIndex中。arrIndex的值只有兩種結(jié)果,-1未找到。0找到了。
this.arrIndex = this.spanIndex.indexOf(index);
console.log("arrIndex", this.arrIndex);
if (this.arrIndex == 0) {
//arrIndex ==0,找到索引了,在索引數(shù)組spanIndex刪除該索引,隱藏二級導(dǎo)航。
this.spanIndex.splice(this.arrIndex, 1);
this.show2 = false;
} else {
// arrIndex ==-1,沒有找到,splice(-1,1)從spanIndex數(shù)組結(jié)尾處刪除1個值,并將當前索引添加到索引數(shù)組spanIndex,show2為true,展示二級導(dǎo)航,
this.spanIndex.splice(this.arrIndex, 1);
this.spanIndex.push(index);
this.show2 = true;
}
console.log("索引數(shù)組", this.spanIndex);
},
secondClick(index) {
console.log(index);
// 當前索引=index
this.currIndex2 = index;
// 判斷當前索引是否在索引數(shù)組spanIndex中。arrIndex的值只有兩種結(jié)果,-1未找到。0找到了。
this.arrIndex2 = this.spanIndex2.indexOf(index);
console.log("arrIndex2", this.arrIndex2);
if (this.arrIndex2 == 0) {
//arrIndex ==0,找到索引了,在索引數(shù)組spanIndex刪除該索引,隱藏二級導(dǎo)航。
this.spanIndex2.splice(this.arrIndex2, 1);
this.show3 = false;
} else {
// arrIndex ==-1,沒有找到,splice(-1,1)從spanIndex數(shù)組結(jié)尾處刪除1個值,并將當前索引添加到索引數(shù)組spanIndex,show2為true,展示二級導(dǎo)航,
this.spanIndex2.splice(this.arrIndex2, 1);
this.spanIndex2.push(index);
this.show3 = true;
}
console.log("索引數(shù)組2", this.spanIndex2);
},
},
};
</script>
<style>
p {
padding: 5px 0;
margin-block-start: 0;
margin-block-end: 0;
}
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.first {
width: 200px;
font-size: 24px;
font-weight: bold;
/* height: 60px; */
/* background:red; */
}
.first:hover {
cursor: pointer;
/* color:red; */
}
.second {
font-size: 18px;
font-weight: normal;
background: #eee;
margin-left: 50px;
}
.three {
background: yellow;
margin-left: 20px;
font-size: 14px;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù)
這篇文章主要介紹了VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
vue如何使用pdf.js實現(xiàn)在線查看pdf文件功能
PDF.js是一個開源的JavaScript庫,用于在網(wǎng)頁上渲染和顯示PDF文件,在Vue中使用PDF.js來預(yù)覽PDF文件是很常見的需求,這篇文章主要給大家介紹了關(guān)于vue如何使用pdf.js實現(xiàn)在線查看pdf文件功能的相關(guān)資料,需要的朋友可以參考下2024-03-03
vue3通過render函數(shù)實現(xiàn)菜單下拉框的示例
本文主要介紹了vue3通過render函數(shù)實現(xiàn)菜單下拉框的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue3?使用provide?inject父子組件傳值失敗且子組件不響應(yīng)
這篇文章主要介紹了vue3使用provide?inject父子組件傳值傳不過去且傳遞后子組件不具備響應(yīng)性問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

