1 <!-- 2 * @Description: nav側邊欄 3 * @Version: 1.0 4 * @Autor: Nanke_南柯 5 * @Date: 2020-09-03 17:22:19 6 * @LastEditors: Nanke_南柯 7 * @LastEditTime: 2020-09-19 15:55:27 8 --> 9 <template> 10 <div class="Top20_ProblemAnalysis"> 11 <div class="rightNav"> 12 <ul class="tab-nav"> 13 <li v-for="(i, index) in navList" :key="index"> 14 <div class="nav" @click="openNav(index, i.list.length)"> 15 <i class="el-icon-s-home"></i> 16 <span>{{ i.title }}</span> 17 <i v-if="i.list.length" id="right-btn" class="el-icon-arrow-right"></i> 18 </div> 19 <div class="nav-n-box"> 20 <div class="nav-n" v-for="(n, index) in i.list" :key="index">{{ n.title }}</div> 21 </div> 22 </li> 23 </ul> 24 </div> 25 </div> 26 </template> 27 28 <script> 29 export default { 30 components: {}, 31 props: {}, 32 data() { 33 return { 34 navList: [ 35 { 36 title: '首頁', 37 name: 'home', 38 list: [], 39 }, 40 { 41 title: '業務介紹', 42 name: 'BusinessIntroduction', 43 list: [ 44 { 45 title: '交易規則', 46 name: 'BusinessIntroduction', 47 }, 48 { 49 title: '政策法規', 50 name: 'BusinessIntroduction', 51 }, 52 ], 53 }, 54 { 55 title: '新聞資訊', 56 name: 'News', 57 list: [ 58 { 59 title: '公告通知', 60 name: 'BusinessIntroduction', 61 }, 62 { 63 title: '公司新聞', 64 name: 'BusinessIntroduction', 65 }, 66 { 67 title: '行業資訊', 68 name: 'BusinessIntroduction', 69 }, 70 ], 71 }, 72 { 73 title: '關於我們', 74 name: 'AboutUs', 75 list: [ 76 { 77 title: '中心介紹', 78 name: 'BusinessIntroduction', 79 }, 80 { 81 title: '法律申明', 82 name: 'BusinessIntroduction', 83 }, 84 { 85 title: '常見問題', 86 name: 'BusinessIntroduction', 87 }, 88 ], 89 }, 90 ], 91 }; 92 }, 93 watch: {}, 94 computed: {}, 95 methods: { 96 openNav(index, num) { 97 let _this = this; 98 let nav = document.querySelectorAll('.nav'); //獲取父級菜單欄,以便添加選中樣式 99 let items = document.querySelectorAll('.nav-n-box'); //獲取容納子級菜單欄的容器,以便動態設置高度,實現下拉效果 100 101 //-----------------可注釋部分開始------注釋后則不是手風琴效果------------------ 102 // 遍歷菜單欄,移除所有選中后的樣式 添加此段可實現手風琴效果,注釋則實現多展示效果 103 for (let i = 0; i < nav.length; i++) { 104 if ( 105 items[i].style.height == '' || 106 items[i].style.height == '0rem' || 107 nav[index].classList.contains('nav-n-box-active') //判斷標簽內是否含有該class屬性,以布爾值類型返回 108 ) { 109 let height = items[index].style.height; 110 items[index].style.height = height; 111 } else { 112 items[i].style.height = '0rem'; 113 } 114 nav[i].classList.remove('nav-n-box-active'); 115 } 116 //-----------------可注釋部分結束------------------------ 117 118 //根據子菜單欄的高度判斷,是否展開菜單欄,若有進行遍歷操作,那么每次點擊某個菜單欄的時候 height 都為 0 119 if (items[index].style.height == '' || items[index].style.height == '0rem') { 120 //num 為子菜單欄的個數,根據子菜單欄確定容器的高度 121 items[index].style.height = num * 2 + 'rem'; 122 //添加右箭頭旋轉樣式 123 nav[index].classList.add('nav-n-box-active'); 124 } else { 125 items[index].style.height = '0rem'; 126 //移除右箭頭旋轉樣式 127 nav[index].classList.remove('nav-n-box-active'); 128 } 129 //------------------------------------------ 130 }, 131 }, 132 created() {}, 133 mounted() {}, 134 }; 135 </script> 136 <style lang="scss" scoped> 137 .Top20_ProblemAnalysis { 138 width: 100%; 139 height: calc(100vh - 160px); 140 position: relative; 141 display: flex; 142 justify-content: center; 143 align-items: center; 144 color: #fff; 145 .rightNav { 146 width: 65%; 147 height: 500px; 148 background: white; 149 max-width: 200px; 150 margin-top: 20px; 151 152 .tab-nav { 153 padding: 1rem; 154 list-style: none; 155 156 li { 157 display: flex; 158 align-items: center; 159 flex-wrap: wrap; 160 cursor: pointer; 161 user-select: none; 162 163 .nav { 164 padding: 1rem 0; 165 width: 100%; 166 display: flex; 167 align-items: center; 168 justify-content: space-between; 169 170 i { 171 transition: 0.3s; 172 color: rgb(0, 225, 255); 173 } 174 175 span { 176 display: inline-block; 177 width: 100%; 178 text-align: left; 179 color: #808080; 180 font-size: 0.88rem; 181 margin-left: 1rem; 182 } 183 } 184 185 .nav-n-box { 186 transition: 0.3s; 187 width: 100%; 188 height: 0; 189 overflow: hidden; 190 191 .nav-n { 192 width: 100%; 193 font-size: 0.88rem; 194 color: #808080; 195 height: 2rem; 196 text-align: left; 197 padding-left: 2rem; 198 line-height: 2rem; 199 transition: 0.3s; 200 201 &:hover { 202 background: rgb(0, 225, 255); 203 color: white; 204 opacity: 0.5; 205 } 206 } 207 } 208 } 209 } 210 } 211 212 //點擊后右箭頭的反轉效果 213 .nav-n-box-active { 214 #right-btn { 215 transform: rotate(90deg) !important; 216 } 217 } 218 } 219 </style>