選項卡案例
- 第一:css樣式
* {
margin: 0;
padding: 0;
}
.tab ul {
overflow: hidden;
padding: 0;
margin: 0;
}
.tab ul li {
box-sizing: border-box;
padding: 0;
float: left;
width: 100px;
height: 45px;
line-height: 45px;
list-style: none;
text-align: center;
border-top: 1px solid blue;
border-right: 1px solid blue;
cursor: default
}
.tab ul li:first-child {
border-left: 1px solid blue;
}
.tab ul li.active {
background-color: orange;
}
.tab div {
width: 500px;
height: 300px;
display: none;
text-align: center;
font-size: 30px;
line-height: 300px;
border: 1px solid blue;
border-top: 0px;
}
.tab div.current {
display: block;
}
- 第二:結構html
<div id="app">
<div class="tab">
<ul>
<li class="active">apple</li>
<li>orange</li>
<li>lemon</li>
</ul>
<div class="current">
<img src="img/apple.png">
</div>
<div>
<img src="img/orange.png">
</div>
<div>
<img src="img/lemon.png">
</div>
</div>
</div>
- 第三:js邏輯
var vm = new Vue({
el: '#app',
data: {
}幌氮,
methods: {
}
)}
下面是解答結果 html結構和js代碼
1描焰、html結構
<div id="app">
<div class="tab">
<!-- tab欄 -->
<ul>
<!-- 動態(tài)綁定class 有 active 類名高亮 無 active 不高亮-->
<li @click="change(index)" :class='currentIndex==index?"active":""' v-for="(item, index) in list"
:key="item.id">
{{item.title}}</li>
</ul>
<!-- 對應顯示的圖片 -->
<!-- 動態(tài)綁定class 有 current 類名顯示 無 current 隱藏-->
<div :class="currentIndex==index?'current':''" v-for="(item, index) in list" :key="item.id"><img :src="item.path">
</div>
</div>
</div>
2、js邏輯
let vm = new Vue({
el: '#app',
data: {
currentIndex: 0,// 選項卡當前的索引 默認為 0
list: [{
id: 1,
title: 'apple',
path: 'img/apple.png'
},
{
id: 2,
title: 'orange',
path: 'img/orange.png'
},
{
id: 3,
title: 'lemon',
path: 'img/lemon.png'
}]
},
methods: {
// 通過傳入過來的索引來讓當前的 currentIndex 和點擊的index 值 相等
// 從而實現(xiàn) 控制類名
change(index) {
this.currentIndex = index
}
}
})