最近学习vue的时候用vue实现了一个简单的tab栏切换,分享给大家
一、案例介绍?
效果如下:
二、案例代码
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.tab {
width: 500px;
/* height: 1000px; */
margin: 100px auto;
}
.tab ul {
overflow: hidden;
background-color: #20232A;
}
.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 #20232A;
border-right: 1px solid #20232A;
color: #61DAFB;
cursor: pointer;
}
.tab ul li:first-child {
border-left: 1px solid #20232A;
}
.tab ul li.active {
background-color: #757575;
}
.tab div {
width: 500px;
height: 300px;
display: none;
text-align: center;
font-size: 30px;
line-height: 300px;
border: 1px solid #20232A;
border-top: 0px;
}
.tab div img {
width: 100%;
height: 100%;
}
.tab div.current {
display: block;
}
</style>
</head>
<body>
<div id="app">
<div class="tab">
<ul>
<!-- v-for循环渲染出li的标题 -->
<!-- class属性判断currentIndex与当前li的index是否匹配,匹配就添加相应类否则将类名置空 -->
<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>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
data: {
// 选项卡li当前的索引
currentIndex: 0,
list: [{
id: 1,
title: 'First',
path: 'img/1.jpg'
}, {
id: 2,
title: 'Second',
path: 'img/2.jpg'
}, {
id: 3,
title: 'Third',
path: 'img/3.jpeg'
}, {
id: 4,
title: 'Fourth',
path: 'img/4.jfif'
}, {
id: 5,
title: 'Fifth',
path: 'img/5.png'
}]
},
methods: {
change: function (index) {
// 选项卡切换操作,通过currentIndex操作类名添加到哪个类
this.currentIndex = index
}
}
});
</script>
</body>
</html>
这里由于是用到了vue的知识所以这里需要先自行导入vue.js文件