標籤頁組件,即實現選項卡切換,經常使用於平級內容的收納與展現。css
由於每一個標籤頁的內容是由使用組件的父級控制的,即這部份內容爲一個 slot。因此通常的設計方案是,在 slot 中定義多個 div,而後在接到切換消息時,再顯示或隱藏相關的 div。這裏面就把相關的交互邏輯也編寫進來了,咱們但願在組件中處理這些交互邏輯,slot 只單純處理業務邏輯。這能夠經過再定義一個 pane 組件來實現,pane 組件嵌在 tabs 組件中。html
由於 tabs 組件中的標題是在 pane 組件中定義的,因此在初始化或者動態變化標題時,tabs 組件須要從 pane 組件中獲取標題。vue
html:數組
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>標籤頁組件</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<div id="app" v-cloak>
<tabs v-model="activeIndex">
<pane label="科技">
火星疑似發現「外星人墓地」?至今沒法解釋
</pane>
<pane label="體育">
全美沸騰!湖人隊4年1.2億迎頂級後衛,詹姆斯:有他就能奪冠
</pane>
<pane label="娛樂">
阿米爾汗談中國武俠 想拍印度版《鹿鼎記》
</pane>
</tabs>
</div>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<script src="tabs.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
activeIndex: 0
}
});
</script>
</body>
</html>
複製代碼
pane 組件:bash
Vue.component('pane', {
name: 'pane',
template: '\ <div class="pane" v-show="isShow">\ <slot></slot>\ </div>\ ',
props: {
//標題
label: {
type: String,
default: ''
}
},
data: function () {
return {
//顯示或隱藏
isShow: true
}
},
methods: {
//通知父組件,更新標題
init() {
this.$parent.init();
}
},
watch: {
//當 label 值發生變化時,更新標題
label() {
this.init();
}
},
//掛載時,更新標題
mounted() {
this.init();
}
});
複製代碼
在 pane 組件中,咱們作了如下設計:app
this.$parent
來調用父組件 tabs 的初始化方法。tabs 組件:函數
Vue.component('tabs', {
template: '\ <div class="tabs">\ <div class="tabs-bar">\ <!-- 標籤頁標題-->\ <div :class="tabClass(item)"\ v-for="(item, index) in titleList"\ @click="change(index)">\ {{ item.label }}\ </div>\ </div>\ <div class="tabs-content">\ <!-- pane 組件位置-->\ <slot></slot>\ </div>\ </div>',
props: {
value: {
type: [String, Number]
}
},
data: function () {
return {
currentIndex: this.value,
titleList: []//存放標題
}
},
methods: {
//設置樣式
tabClass: function (item) {
return ['tabs-tab', {
//爲當前選中的 tab 添加選中樣式
'tabs-tab-active': (item.name === this.currentIndex)
}]
},
//獲取定義的全部 pane 組件
getTabs() {
return this.$children.filter(function (item) {
return item.$options.name === 'pane';
})
},
//更新 pane 是否顯示狀態
updateIsShowStatus() {
var tabs = this.getTabs();
var that = this;
//迭代判斷並設置某個標籤頁是顯示仍是隱藏狀態
tabs.forEach(function (tab, index) {
return tab.isShow = (index === that.currentIndex);
})
},
//初始化
init() {
/**
* 初始化標題數組
*/
this.titleList = [];
var that = this;//設置 this 引用
this.getTabs().forEach(function (tab, index) {
that.titleList.push({
label: tab.label,
name: index
});
//初始化默認選中的 tab 索引
if (index === 0) {
if (!that.currentIndex) {
that.currentIndex = index;
}
}
});
this.updateIsShowStatus();
},
//點擊 tab 標題時,更新 value 值爲相應的索引值
change: function (index) {
var nav = this.titleList[index];
var name = nav.name;
this.$emit('input', name);
}
},
watch: {
//當 value 值發生改變時,更新 currentIndex
value: function (val) {
this.currentIndex = val;
},
//當 currentIndex 值發生改變時,更新 pane 是否顯示狀態
currentIndex: function () {
this.updateIsShowStatus();
}
}
});
複製代碼
getTabs()
中經過 this.$children
來獲取定義的全部 pane 組件。由於不少地方都會用到getTabs()
,因此這裏把它單獨定義出來。var that = this;
,在 that 中引用 Vue 實例自己,也可使用 ES2015 的箭頭函數。updateIsShowStatus()
用於更新 tab 是否顯示狀態。之因此獨立出來,是爲了在監聽 currentIndex 發生變化時,也能調用該方法。v-for
指令渲染出標題,並綁定了 tabClass 函數,從而實現了動態設置樣式。由於須要傳參,因此不能使用計算屬性。change()
,來更新 value 值爲相應的索引值。在 watch 中,咱們監聽了 value 值,當 value 值發生改變時,更新 currentIndex。也監聽了 currentIndex 值,當 currentIndex 值發生改變時,更新 pane 是否顯示狀態。總結以下:動畫
$parent
與 $children
)實現通訊。樣式:ui
[v-cloak] {
display: none;
}
.tabs {
font-size: 14px;
color: #657180;
}
.tabs-bar:after {
content: '';
display: block;
width: 100%;
height: 1px;
background: #d7dde4;
margin-top: -1px;
}
.tabs-tab {
display: inline-block;
padding: 4px 16px;
margin-right: 6px;
background: #fff;
border: 1px solid #d7dde4;
cursor: pointer;
position: relative;
}
.tabs-tab:hover {
color: #336699;
font-weight: bolder;
}
.tabs-tab-active {
color: #336699;
border-top: 1px solid #336699;
border-bottom: 1px solid #fff;
}
.tabs-tab-active:before {
content: '';
display: block;
height: 1px;
background: #3399ff;
position: absolute;
top: 0;
left: 0;
right: 0;
}
.tabs_content {
padding: 8px 0;
}
.pane {
margin-top: 26px;
font-size: 16px;
line-height: 24px;
color: #333;
text-align: justify;
}
複製代碼
效果:this
咱們爲 pane 組件新增一個 closable 屬性,用於控制該標籤是否可關閉。
在子窗口組件的 props 中,新增 closable 屬性:
props: {
...
//是否可關閉
closable: {
type: Boolean,
default: false
}
}
複製代碼
在標籤頁組件中的模板中,新增關閉標籤:
...
template: '\ <div class="tabs">\ <div class="tabs-bar">\ <!-- 標籤頁標題-->\ <div :class="tabClass(item)"\ v-for="(item, index) in titleList"\ @click="change(index)">\ {{ item.label }}\ <span v-if="item.closable" class="close" @click="close(index,item.name)"></span>\ </div>\ </div>\ <div class="tabs-content">\ <!-- pane 組件位置-->\ <slot></slot>\ </div>\ </div>',
...
複製代碼
close()
函數,傳入標籤所在索引以及標籤的名稱。在標籤頁組件中的方法中,新增了 close(),用於執行關閉標籤頁邏輯:
close: function (index, name) {
//刪除對應的標題元素
this.titleList.splice(index, 1);
var tabs = this.getTabs();
var that = this;
//迭代判斷並設置點擊的標籤頁是隱藏狀態
tabs.forEach(function (tab, index) {
if (index === name) {
return tab.isShow = false;
}
});
}
複製代碼
新增的樣式:
.close{
color: #FF6666;
}
.close::before {
content: "\2716";
}
.close:hover {
color: #990033;
font-weight: bolder;
}
複製代碼
爲須要添加關閉標籤的 pane ,添加 closable 屬性:
<div id="app" v-cloak>
<tabs v-model="activeIndex">
<pane label="科技" closable="true">
火星疑似發現「外星人墓地」?至今沒法解釋
</pane>
<pane label="體育">
全美沸騰!湖人隊4年1.2億迎頂級後衛,詹姆斯:有他就能奪冠
</pane>
<pane label="娛樂" closable="true">
阿米爾汗談中國武俠 想拍印度版《鹿鼎記》
</pane>
</tabs>
</div>
複製代碼
效果:
咱們在切換標籤頁時,加上滑動動畫吧,這很簡單,只要在激活的樣式中加上 transform 與 transition 樣式便可:
.tabs-tab-active {
color: #336699;
border-top: 1px solid #336699;
border-bottom: 1px solid #fff;
transform:translateY(-1px);
transition: transform 0.5s;
}
複製代碼
效果:
咱們讓標籤頁標題被點擊時,以動畫的形式往上移動 1 個像素。是否是很酷呀O(∩_∩)O~