這篇文章將為大家詳細講解有關利用vue怎么實現一個tab欄切換二選一功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
<template> <div id="app"> <div class="tabWrap"> <!-- 這個結構是tab導航,并給其綁定對應的點擊事件,在點擊事件的回調中 去控制對應內容的顯示隱藏和樣式的修改即:tab的切換--> <div class="tabNav"> <div class="navOne" @click="tabOne">tab1</div> <div class="navTwo" @click="tabTwo">tab2</div> </div> <!-- 這個結構是tab導航對應的內容 --> <div class="tabContent"> <!-- 通過v-show控制隱藏,同一時刻隱藏一個顯示一個,就實現了tab欄的切換效果了 --> <div class="navOneBox" v-show="showTabOne">我是切換1</div> <div class="navTwoBox" v-show="showTabTwo">i am tab2</div> </div> </div> </div> </template>
js部分
<script> export default { name: "app", data() { return { showTabOne: true, // 二選一tab切換 showTabTwo: false, // 二選一tab切換 }; }, methods: { // 二選一tab欄切換 tabOne() { /* 點擊tab1的時候,讓tab1顯示,tab2隱藏,即showTabOne為true,showTabTwo為false 同時修改tab1的樣式使其"高亮",注意不要忘了修改tab2的樣式,使其"不高亮"。 點擊tab2的時候,也是同理。 */ this.showTabOne = true; this.showTabTwo = false; document.querySelector(".navOne").style.backgroundColor = "#fff"; document.querySelector(".navTwo").style.backgroundColor = "#e3e3e3"; document.querySelector(".navOne").style.color = "#3985EC"; document.querySelector(".navTwo").style.color = "#80868D"; }, // 二選一tab欄切換 tabTwo() { this.showTabTwo = true; this.showTabOne = false; document.querySelector(".navOne").style.backgroundColor = "#e3e3e3"; document.querySelector(".navTwo").style.backgroundColor = "#fff"; document.querySelector(".navTwo").style.color = "#3985EC"; document.querySelector(".navOne").style.color = "#80868D"; }, }, }; </script>
css部分
<style lang="less"> .tabNav { width: 126px; height: 30px; border-radius: 2px; background-color: #e3e3e3; display: flex; align-items: center; justify-content: space-evenly; .navOne { width: 60px; height: 26px; border-radius: 2px; background-color: #fff; color: #3985ec; font-size: 14px; font-weight: 500; display: flex; justify-content: center; align-items: center; cursor: pointer; } .navTwo { width: 60px; height: 26px; color: #80868d; border-radius: 2px; font-size: 14px; font-weight: 500; display: flex; justify-content: center; align-items: center; cursor: pointer; } } .tabContent { margin-top: 8px; .navOneBox { background-color: #bfa; } .navTwoBox { background-color: #baf; } } </style>
關于利用vue怎么實現一個tab欄切換二選一功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。