本篇文章需要有一丢丢的Vue基础 ,或者可以参考Vue官网,本篇文章会用到的知识点有 v-on ,v-bind ,v-if ,v-for 运行的时候 需要引入Vue.js文件
业务需求:
- 点击tab 栏内容去切换相对应的显示内容
- 比如:点击栏目一 内容区域显示栏目一 内容一

可以自己先敲一下,想想该怎么实现这个业务需求呢? 下面我会提供静态代码,只需要在这个基础上进行修改即可
我也会在文章的最后,写一遍demo
HTML静态代码
<div id="app" class="vertical-tab">
<ul class="nav nav-tabs1">
<li class="active"><a href="#"> Section 1 </a></li>
<li class=""><a href="#"> Section 2 </a></li>
<li class=""><a href="#"> Section 3 </a></li>
</ul>
<div class="tab-content tabs">
<div class="tab-pane fade active">
<h3>Section 1</h3>
<p>content1</p>
</div>
<div class="tab-pane fade">
<h3>Section 2</h3>
<p>content2</p>
</div>
<div class="tab-pane fade">
<h3>Section 3</h3>
<p>content3</p>
</div>
<div class="tab-pane fade">
<h3>Section 4</h3>
<p>content4</p>
</div>
<div class="tab-pane fade">
<h3>Section 5</h3>
<p>content5</p>
</div>
<div class="tab-pane fade">
<h3>Section 6</h3>
<p>content6</p>
</div>
</div>
<ul class="nav nav-tabs2">
<li class=""><a href="#"> Section 4 </a></li>
<li class=""><a href="#"> Section 5 </a></li>
<li class=""><a href="#"> Section 6 </a></li>
</ul>
</div>
CSS代码
<style>
* {
margin: 0;
padding: 0;
}
.vertical-tab {
width: 920px;
margin: 100px auto;
}
.vertical-tab .nav {
list-style: none;
width: 200px;
}
.vertical-tab .nav-tabs1 {
border-right: 3px solid #e7e7e7;
}
.vertical-tab .nav-tabs2 {
border-left: 3px solid #e7e7e7;
}
.vertical-tab .nav a {
display: block;
font-size: 18px;
font-weight: 700;
text-align: center;
letter-spacing: 1px;
text-transform: uppercase;
padding: 10px 20px;
margin: 0 0 1px 0;
text-decoration: none;
}
.vertical-tab .tab-content {
color: #555;
background-color: #fff;
font-size: 15px;
letter-spacing: 1px;
line-height: 23px;
padding: 10px 15px 10px 25px;
display: table-cell;
position: relative;
}
.vertical-tab .nav-tabs1 {
float: left;
}
.vertical-tab .tabs {
width: 500px;
box-sizing: border-box;
float: left;
}
.vertical-tab .tab-content h3 {
font-weight: 600;
text-transform: uppercase;
margin: 0 0 5px 0;
}
.vertical-tab .nav-tabs2 {
float: right;
}
.tab-content .tab-pane {
display: none;
}
.tab-content .tab-pane.active {
display: block;
}
</style>
data数据
list: [{
id: 1,
title: '栏目一',
content: '内容一'
}, {
id: 2,
title: '栏目二',
content: '内容二'
}, {
id: 3,
title: '栏目三',
content: '内容三'
}, {
id: 4,
title: '栏目四',
content: '内容四'
}, {
id: 5,
title: '栏目五',
content: '内容五'
}, {
id: 6,
title: '栏目六',
content: '内容六'
}]
Demo示例 思路:
- 首先循环数据,将数据渲染到页面
- 给tab栏 绑定事件,实现类名active的切换
- 拿到当前点击的索引,让对应的内容显示出来
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<div id="app" class="vertical-tab">
<ul class="nav nav-tabs1">
<li v-on:click="handel(index,0)" :calss = "curIndex == index?"active":"" " v-for = "(item,index) in list" v-if="index < list.index/2"><a href="#"> {{item.title}} </a></li>
</ul>
<div class="tab-content tabs">
<div class="tab-pane fade active"
:calss = "curIndex == index?"active":"" " v-for = "(item,index) in list">
<h3>{{item.title}}</h3>
<p>{{item.content}}</p>
</div>
</div>
<ul class="nav nav-tabs2">
<li v-on:click="handel(index,1)" :calss = "curIndex == index?"active":"" " v-for = "(item,index) in list">
<a href="#"> {{item.title}} </a></li>
</ul>
</div>
<script src="js/vue.js"></script>
<script>
var vm = new Vue({
el:"#app",
data:{
curIndex:0,
list: [{
id: 1,
title: '栏目一',
content: '内容一'
}, {
id: 2,
title: '栏目二',
content: '内容二'
}, {
id: 3,
title: '栏目三',
content: '内容三'
}, {
id: 4,
title: '栏目四',
content: '内容四'
}, {
id: 5,
title: '栏目五',
content: '内容五'
}, {
id: 6,
title: '栏目六',
content: '内容六'
}]
},
methods:{
handel:function(index,flag){
if(flag){
this.curIndex = index;
}else{
this.curIndes = index;
}
}
}
})
</script>
</body>
</html>
|