Vue中tab欄切換的簡單實(shí)現(xiàn)
一、效果展示

二、實(shí)現(xiàn)原理
主體通過綁定事件,索引的利用,v-for的數(shù)組遍歷,來實(shí)現(xiàn)的切換效果。
具體細(xì)節(jié)看代碼段的解釋,根據(jù)個(gè)人所需去了解一下,更多的是入門理解其中的細(xì)思。
三、css和h5的代碼,獲得最基本的樣式
1.css
主體的布局根據(jù)個(gè)人的喜好,這里我只進(jìn)行了簡單的布局。
其中也用到了浮動(dòng),和清除浮動(dòng)。
主要讓展現(xiàn)的效果好看一些。具體樣式還是根據(jù)個(gè)人。
<style>
a{
text-decoration: none;
width: 180px;
height: 30px;
line-height: 30px;
text-align: center;
color: #666;
float: left;
margin-right: 15px;
}
.nav::after{
content: '';
display: block;
clear: both;
}
.nav a{
background-color: beige;
}
.nav a.hover{
background-color: blue;
}
.nav_con div{
display: none;
}
.nav_con .center{
display: block;
}
img{
width: 570px;
}
</style>2.H5 這是沒有在使用Vue書寫前的樣式
其中的“內(nèi)容,動(dòng)態(tài),行業(yè)”被上文的display none 隱藏起來了,并不是沒有內(nèi)容
<div class="tab">
<div class="nav">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="hover">圖片一</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >圖片二</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >圖片三</a>
</div>
<div class="nav_con">
<div><img src="./圖片/2.jpg" alt=""></div>
<div><img src="./圖片/3.jpg" alt="">/div>
<div><img src="./圖片/4.jpg" alt=""></div>
</div>
</div>

四、Vue部分
填充的內(nèi)容以數(shù)組的形勢來給到想要給的地方,可以給每一個(gè)內(nèi)容都取一個(gè)固定的id,在后續(xù)可以提高性能,currentIndex:0,是定義的一個(gè)索引,通過這個(gè)索引來綁定類名,methods定義函數(shù),也就是方法,后續(xù)在其中來實(shí)現(xiàn)切換。
<script src="./vue.js"></script>
<script>
let vm = new Vue({
el:'.tab',
data:{
currentIndex:0, //定義一個(gè)索引
list:[{
id: 1,
title:'圖片一',
path:'./圖片/2.jpg'
},{
id: 2,
title:'圖片二',
path:'./圖片/3.jpg'
},{
id: 3,
title:'圖片三',
path:'./圖片/4.jpg'
}]},
methods:{
change(index){
vm.currentIndex = index;//通過參數(shù)獲得索引
}
}
})
</script> 此段是使用Vue后的h5代碼
其中使用了點(diǎn)擊的事件綁定
v-for的數(shù)組遍歷(item,index)in list .list是自己定義的數(shù)組名
在插值表達(dá)式中獲取所對應(yīng)的值
通過 :class來綁定類名,是通過定義的索引來判斷,如果兩個(gè)索引相同,就會(huì)獲得背景顏色,也會(huì)出現(xiàn)相對應(yīng)的值,否則就。
<div class="tab">
<div class="nav">
<a :class="currentIndex==index?'hover':''" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="change(index)" :key="item.id" v-for="(item,index) in list">{{item.title}}</a>
</div>
<div class="nav_con">
<div :class="currentIndex==index?'center':''" :key="item.id" v-for="(item,index) in list"><img :src="item.path" alt=""></div>
</div>
</div>到此這篇關(guān)于Vue中tab欄切換的簡單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue tab欄切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 百度地圖(vue-baidu-map)繪制方向箭頭折線實(shí)例代碼詳解
這篇文章主要介紹了vue 百度地圖(vue-baidu-map)繪制方向箭頭折線,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue.js 2.0 移動(dòng)端拍照壓縮圖片上傳預(yù)覽功能
這篇文章主要介紹了Vue.js 2.0 移動(dòng)端拍照壓縮圖片上傳預(yù)覽功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Vuex如何獲取getter對象中的值(包括module中的getter)
這篇文章主要介紹了Vuex如何獲取getter對象中的值(包括module中的getter),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue項(xiàng)目中keepAlive的使用說明(超級(jí)實(shí)用版)
這篇文章主要介紹了Vue項(xiàng)目中keepAlive的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue實(shí)現(xiàn)文章內(nèi)容過長點(diǎn)擊閱讀全文功能的實(shí)例
下面小編就為大家分享一篇vue實(shí)現(xiàn)文章內(nèi)容過長點(diǎn)擊閱讀全文功能的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

