Vue實現(xiàn)簡單選項卡效果
本文實例為大家分享了Vue實現(xiàn)簡單選項卡效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
1.頭部選項卡
點擊切換下標(biāo)ID 傳遞一個參數(shù)回去,active綁定選中樣式
<div id="app"> ? ? ? ? <ul> ? ? ? ? ? ? <li v-for='(item,index) in list' @click='check(index)' :class="{ active: index === TabId }"> ? ? ? ? ? ? ? ? <h1> {{item.title}}</h1> ? ? ? ? ? ? </li> ? ? ? ? </ul> </div>
2.完整HTML
默認(rèn)使用V-show來切換,獲取盒子下標(biāo),沒有dom操作
當(dāng)然,Vue官方文檔也不推薦V-if和V-for同時使用
<div id="app"> ? ? ? ? <ul> ? ? ? ? ? ? <li v-for='(item,index) in list' @click='check(index)' :class="{ active: index === TabId }"> ? ? ? ? ? ? ? ? <h1> {{item.title}}</h1> ? ? ? ? ? ? </li> ? ? ? ? </ul> ? ? ? ? <div class="mycontent" v-for='(item,index) in list' v-show="index === TabId"> ? ? ? ? ? ? <h1>{{item.content}}</h1> ? ? ? ? </div> </div>
3.Vue
下標(biāo)默認(rèn)為0
<script> ? ? ? ? //清除默認(rèn)提示樣式 ? ? ? ? Vue.config.devtools = false ? ? ? ? Vue.config.productionTip = false ? ? ? ? let vm = new Vue({ ? ? ? ? ? ? el: '#app', ? ? ? ? ? ? data() { ? ? ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? ? ? //下標(biāo)ID 默認(rèn)從零開始 ? ? ? ? ? ? ? ? ? ? TabId: 0, ? ? ? ? ? ? ? ? ? ? list: [ ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Itachi', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'VUE' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Sasuke', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'HTML' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Obito', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'CSS' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Madara', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'JavaScript' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ] ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ? ? ? ? methods: { ? ? ? ? ? ? ? ? ? //將點擊事件獲取的下標(biāo)ID傳遞給要切換的盒子 ? ? ? ? ? ? ? ? check(index) { ? ? ? ? ? ? ? ? ? ? this.TabId = index; ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? } ? ? ? ? }); </script>
4.完整代碼
<!DOCTYPE html> ? <html lang='en'> ? <head> ? ? <meta charset="UTF-8"> ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0"> ? ? <meta http-equiv="X-UA-Compatible" content="ie=edge"> ? ? <title>Document</title> ? ? <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script> </head> <style> ? ? * { ? ? ? ? margin: 0; ? ? ? ? padding: 0; ? ? ? ? list-style: none; ? ? } ? ? ? #app ul { ? ? ? ? width: 800px; ? ? ? ? background: black; ? ? ? ? opacity: .9; ? ? ? ? margin: 100px auto; ? ? ? ? display: flex; ? ? ? ? justify-content: space-between; ? ? } ? ? ? #app li { ? ? ? ? width: 300px; ? ? ? ? text-align: center; ? ? ? ? cursor: pointer; ? ? ? ? color: #fff; ? ? } ? ? ? #app li:hover { ? ? ? ? cursor: pointer; ? ? } ? ? ? #app li.active { ? ? ? ? background: #03a9f4; ? ? ? } ? ? ? .mycontent { ? ? ? ? width: 800px; ? ? ? ? height: 500px; ? ? ? ? margin: 0 auto; ? ? ? ? background: #03a9f4; ? ? ? ? opacity: .8; ? ? ? ? display: flex; ? ? ? ? justify-content: center; ? ? ? ? align-items: center; ? ? } </style> ? <body> ? ? <div id="app"> ? ? ? ? <ul> ? ? ? ? ? ? <li v-for='(item,index) in list' @click='check(index)' :class="{ active: index === TabId }"> ? ? ? ? ? ? ? ? <h1> {{item.title}}</h1> ? ? ? ? ? ? </li> ? ? ? ? </ul> ? ? ? ? <div class="mycontent" v-for='(item,index) in list' v-show="index === TabId"> ? ? ? ? ? ? <h1>{{item.content}}</h1> ? ? ? ? </div> ? ? </div> ? ? ? ? <script> ? ? ? ? //清除默認(rèn)提示樣式 ? ? ? ? Vue.config.devtools = false ? ? ? ? Vue.config.productionTip = false ? ? ? ? let vm = new Vue({ ? ? ? ? ? ? el: '#app', ? ? ? ? ? ? data() { ? ? ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? ? ? //下標(biāo)ID 默認(rèn)從零開始 ? ? ? ? ? ? ? ? ? ? TabId: 0, ? ? ? ? ? ? ? ? ? ? list: [ ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Itachi', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'VUE' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Sasuke', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'HTML' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Obito', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'CSS' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? title: 'Madara', ? ? ? ? ? ? ? ? ? ? ? ? ? ? content: 'JavaScript' ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ] ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ? ? ? ? methods: { ? ? ? ? ? ? ? ? ? //將點擊事件獲取的下標(biāo)ID傳遞給要切換的盒子 ? ? ? ? ? ? ? ? check(index) { ? ? ? ? ? ? ? ? ? ? this.TabId = index; ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? } ? ? ? ? }); ? ? </script> </body> ? </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3?reactive響應(yīng)式依賴收集派發(fā)更新原理解析
這篇文章主要為大家介紹了vue3響應(yīng)式reactive依賴收集派發(fā)更新原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03vue2.x element-ui實現(xiàn)pc端購物車頁面demo
這篇文章主要為大家介紹了vue2.x element-ui實現(xiàn)pc端購物車頁面demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用)
這篇文章主要介紹了多頁vue應(yīng)用的單頁面打包方法(內(nèi)含打包模式的應(yīng)用),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Vue使用Proxy監(jiān)聽所有接口狀態(tài)的方法實現(xiàn)
這篇文章主要介紹了Vue使用Proxy監(jiān)聽所有接口狀態(tài)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06vue中調(diào)接口的方式詳解this.$api、直接調(diào)用、axios
這篇文章主要介紹了vue中調(diào)接口的方式:this.$api、直接調(diào)用、axios,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11