這篇文章主要為大家展示了“vue如何實現選項卡”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“vue如何實現選項卡”這篇文章吧。
具體內容如下

實現靜態UI效果
用傳統的方式實現標簽結構和樣式
基于數據重構UI效果
將靜態的結構和樣式重構為基于Vue模板語法的形式
處理事件綁定和js控制邏輯
聲明式編程
模板的結構和最終顯示的效果基本一致
我們先把每組數據作為對象存儲在數組中
list: [{
id: 1,
title: 'apple',
path: 'images/蘋果.jpg'
}, {
id: 2,
title: 'orange',
path: 'images/橘子.jpg'
}, {
id: 3,
title: 'lemon',
path: 'images/檸檬.jpg'
}]然后通過v-for對這個數組進行遍歷,取到對應的title值
<li :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>對圖片也進行遍歷
<div :key='item.id' v-for='(item,index) in list'> <img src="item.path"> </div>
具體代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.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;
border-bottom: 1px solid blue;
cursor: pointer;
}
.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-style: 30px;
line-height: 300px;
border: 1px solid blue;
border-top: 0px;
}
.tab div.current {
margin-top: 0px;
width: 300px;
height: 300px;
display: block;
}
img {
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<div id="app">
<div class="tab">
<ul>
<li v-on:click='change(index)' :class='currentIndex==index?" active":""' :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>
</ul>
<div :class='currentIndex==index?"current":""' :key='item.id' v-for='(item,index) in list'>
<img :src="item.path">
</div>
<div>
<img src="images/檸檬.jpg">
</div>
</div>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
/*數據*/
data: {
currentIndex: 0,
/*當前索引*/
list: [{
id: 1,
title: 'apple',
path: 'images/蘋果.jpg'
}, {
id: 2,
title: 'orange',
path: 'images/橘子.jpg'
}, {
id: 3,
title: 'lemon',
path: 'images/檸檬.jpg'
}]
},
/*js控制邏輯*/
methods: {
// 在這里實現選項卡切換操作:本質就是操作類名
// 如何操作類名?就是通過currentIndex
change: function(index) {
this.currentIndex = index
}
}
})
</script>
</body>
</html>以上是“vue如何實現選項卡”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。