效果:
主要就是利用v-for循环list里面的元素,然后渲染标题和图片
再判断currentIndex是否和当前遍历元素的索引index相等,相等就显示
通过事件绑定来动态根据鼠标点击的元素修改currentIndex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
ul {
overflow: hidden;
padding: 0;
margin: 0;
}
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: pointer;
}
.tab ul li:first-child {
border-left: 1px solid blue;
}
.tab ul li.active {
background-color: coral;
}
.tab div {
box-sizing: border-box;
width: 300px;
height: 448px;
display: none;
text-align: center;
line-height: 300px;
border: 1px solid blue;
border-top: 0px;
}
.tab div.current {
display: block;
}
img {
width: 298px;
}
</style>
</head>
<body>
<div id="app">
<div class="tab">
<ul>
<li v-bind:class="currentIndex==index?'active':''" :key="item.id" v-for="(item,index) in list" v-on:click="handleClick(index)">{{item.title}}</li>
</ul>
<div v-bind:class="currentIndex==index?'current':''" :key="item.id" v-for="(item,index) in list">
<img v-bind:src="item.src">
</div>
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
currentIndex:0,
list: [{
id: 1,
title: 'Super Girl',
src: 'https://pic.yefu365.com/uploads/allimg/201712/90b8fab56ebc549c.jpg'
}, {
id: 2,
title: 'High Castle',
src: 'https://pic.yefu365.com/uploads/allimg/201806/53a623f7d177f6b7.jpg'
}, {
id: 3,
title: 'Mom',
src: 'https://pic.yefu365.com/uploads/allimg/171101/9e09d3953a34f377.jpg'
}]
},
methods: {
handleClick:function(id){
this.currentIndex = id
}
}
});
</script>
</body>
</html>