vue實(shí)現(xiàn)三級(jí)導(dǎo)航展示和隱藏
本文實(shí)例為大家分享了vue實(shí)現(xiàn)三級(jí)導(dǎo)航展示和隱藏的具體代碼,供大家參考,具體內(nèi)容如下
需求描述:
要實(shí)現(xiàn)側(cè)邊欄三級(jí)導(dǎo)航的顯示和隱藏。點(diǎn)擊其中一個(gè)一級(jí)導(dǎo)航,展示該一級(jí)導(dǎo)航的二級(jí)導(dǎo)航,再點(diǎn)擊關(guān)閉該二級(jí)導(dǎo)航。一級(jí)導(dǎo)航的其他項(xiàng),展開二級(jí)導(dǎo)航。關(guān)閉其他一級(jí)導(dǎo)航的二級(jí)導(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: "項(xiàng)目信息", child: [ { subTitle: "項(xiàng)目簡(jiǎn)介", esubTitle: "#projectIntroduction", threeChild: [ { threeTitle: "三級(jí)導(dǎo)航" }, { threeTitle: "三級(jí)導(dǎo)航" }, { threeTitle: "三級(jí)導(dǎo)航" }, ], }, { subTitle: "樣品信息", threeChild: [ { threeTitle: "三級(jí)導(dǎo)航22" }, { threeTitle: "三級(jí)導(dǎo)航22" }, { threeTitle: "三級(jí)導(dǎo)航22" }, ], }, { subTitle: "樣品信息", threeChild: [ { threeTitle: "三級(jí)導(dǎo)航33" }, { threeTitle: "三級(jí)導(dǎo)航33" }, { threeTitle: "三級(jí)導(dǎo)航33" }, ], }, ], }, { title: "項(xiàng)目信息2", child: [ { subTitle: "項(xiàng)目簡(jiǎn)介22", threeChild: [ { threeTitle: "三級(jí)導(dǎo)航44" }, { threeTitle: "三級(jí)導(dǎo)44" }, { threeTitle: "三級(jí)導(dǎo)航22" }, ], }, { subTitle: "樣品信息22", }, ], }, { title: "項(xiàng)目信息3", eTitle: "#projectMessage", child: [ { subTitle: "項(xiàng)目簡(jiǎn)介33", esubTitle: "#projectIntroduction", }, { subTitle: "樣品信息33", esubTitle: "#sampleInformation", }, ], }, { title: "項(xiàng)目信息2", child: [ { subTitle: "項(xiàng)目簡(jiǎn)介22", }, { subTitle: "樣品信息22", }, ], }, { title: "項(xiàng)目信息3", child: [ { subTitle: "項(xiàng)目簡(jiǎn)介33", }, { subTitle: "樣品信息33", }, ], }, ], currIndex: "", //當(dāng)前索引 spanIndex: [], //索引數(shù)組 arrIndex: "", //用于判斷是否做索引數(shù)組找到當(dāng)前索引。-1為找不到,0找到了。 currIndex2: "", //二級(jí)導(dǎo)航當(dāng)前索引 spanIndex2: [], //索引數(shù)組 arrIndex2: "", //用于判斷是否做索引數(shù)組找到當(dāng)前索引。-1為找不到,0找到了。 }; }, methods: { handleClick(index) { // 初始化三級(jí)導(dǎo)航,默認(rèn)不顯示。 this.show3 =false; this.spanIndex2.splice(-1, 1); // 當(dāng)前索引=index this.currIndex = index; console.log("當(dāng)前索引index", index); // 判斷當(dāng)前索引是否在索引數(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刪除該索引,隱藏二級(jí)導(dǎo)航。 this.spanIndex.splice(this.arrIndex, 1); this.show2 = false; } else { // arrIndex ==-1,沒(méi)有找到,splice(-1,1)從spanIndex數(shù)組結(jié)尾處刪除1個(gè)值,并將當(dāng)前索引添加到索引數(shù)組spanIndex,show2為true,展示二級(jí)導(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); // 當(dāng)前索引=index this.currIndex2 = index; // 判斷當(dāng)前索引是否在索引數(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刪除該索引,隱藏二級(jí)導(dǎo)航。 this.spanIndex2.splice(this.arrIndex2, 1); this.show3 = false; } else { // arrIndex ==-1,沒(méi)有找到,splice(-1,1)從spanIndex數(shù)組結(jié)尾處刪除1個(gè)值,并將當(dāng)前索引添加到索引數(shù)組spanIndex,show2為true,展示二級(jí)導(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+ts+pinia+vant項(xiàng)目搭建詳細(xì)步驟
最近公司想重構(gòu)一個(gè)項(xiàng)目,這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue3+ts+pinia+vant項(xiàng)目搭建的詳細(xì)步驟,文中通過(guò)圖文及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09對(duì)vuex中g(shù)etters計(jì)算過(guò)濾操作詳解
今天小編就為大家分享一篇對(duì)vuex中g(shù)etters計(jì)算過(guò)濾操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue實(shí)現(xiàn)el-menu與el-tabs聯(lián)動(dòng)的項(xiàng)目實(shí)踐
本文講述了如何使用Vue.js中的ElementUI組件庫(kù)實(shí)現(xiàn)el-menu與el-tabs的聯(lián)動(dòng),通過(guò)在el-menu中選擇菜單項(xiàng),可以切換el-tabs的內(nèi)容區(qū)域,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11vue實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)實(shí)例代碼(防抖函數(shù)的應(yīng)用場(chǎng)景)
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)的相關(guān)資料,主要是防抖函數(shù)的應(yīng)用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Vue項(xiàng)目中實(shí)現(xiàn)AES加密解密的全過(guò)程
AES算法是一種對(duì)稱加密算法,用于加密和解密數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中實(shí)現(xiàn)AES加密解密的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08vue.js 1.x與2.0中js實(shí)時(shí)監(jiān)聽input值的變化
這篇文章主要介紹了vue.js 1.x與vue.js2.0中js實(shí)時(shí)監(jiān)聽input值的變化的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03前端vue2?element?ui高效配置化省時(shí)又省力
這篇文章主要為大家介紹了前端高效配置化vue2?element?ui省時(shí)又省力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07