vue学习笔记-9-tab选项卡小案例

一,自己写的:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/base.css"/>
		<style type="text/css">
			 #app{
				 width: 400px;
				 margin: 200px auto;
				 border: 1px solid #ccc;
			 }
			.box>ul:first-child{
				display: flex;
				justify-content: space-between;
			}
			.box>ul:first-child>li{
				background: orange;
				line-height: 40px;
				width: 100px;
				text-align: center;
				color: #fff;
				font-size: 14px;
			}
			.box>ul:first-child>li:hover{
				background: rgba(255,165,0,0.5);
			}
			
		</style>
	</head>
	<body>
		<div id="app">
			<div class="box">
				<ul>
					<li @mouseenter="mouseenter1">第一张</li>
					<li @mouseenter="mouseenter2">第二张</li>
					<li @mouseenter="mouseenter3">第三张</li>
					<li @mouseenter="mouseenter4">第四张</li>
				</ul>
				<ul>
					<li v-if="val==1"><img src="img/1.jpg"></li>
					<li v-else-if="val==2"><img src="img/2.jpg"></li>
					<li v-else-if="val==3"><img src="img/3.jpeg"></li>
					<li v-else><img src="img/4.jpg"></li>
				</ul>
			</div>
		</div>
		 
		<script type="text/javascript" src="./js/vue.min.js"></script>
		<script type="text/javascript">
			var vm=new Vue({
				el: "#app",
				data:{
					val:1
				},
				methods:{
					mouseenter1:function(){
						this.val=1
					},
					mouseenter2:function(){
						this.val=2
					},
					mouseenter3:function(){
						this.val=3
					},
					mouseenter4:function(){
						this.val=4
					}
				}
			})
		</script>
	</body>
</html>

实现的效果:
在这里插入图片描述
二,实际案例中的写法:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/base.css"/>
		<style type="text/css">
			 #app{
				 width: 400px;
				 margin: 200px auto;
				 border: 1px solid #ccc;
			 }
			.box>ul:first-child{
				display: flex;
				justify-content: space-between;
			}
			.box>ul:first-child>li{
				background: orange;
				line-height: 40px;
				width: 100px;
				text-align: center;
				color: #fff;
				font-size: 14px;
			}
			.box>ul:first-child>li:hover{
				background: rgba(255,165,0,0.5);
			}
			.box>ul:last-child>li{
				display: none;
			}
			.box>ul:last-child>.current{
				display: block;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<div class="box">
				<ul>
					<li @mouseenter="mouseenter(index)" v-for="(item,index) in list">{{item.title}}</li>
					<!-- 通过mouseenter(index)把index参数传入,获取哪个被点击 -->
				</ul>
				<ul>
					<li v-bind:class=currenIndex==index?"current":""   v-for="(item,index) in list"><img :src="item.path"></li>
					<!-- 三目运算,给index对应的img添加显示的样式 -->
				</ul>
			</div>
		</div>
		 
		<script type="text/javascript" src="./js/vue.min.js"></script>
		<script type="text/javascript">
			var vm=new Vue({
				el: "#app",
				data:{
					currenIndex: 0,
					list:[{
						id:1,
						title:"第一张",
						path:"img/1.jpg"
					},
					{
						id:2,
						title:"第二张",
						path:"img/2.jpg"
					},
					{
						id:3,
						title:"第三张",
						path:"img/3.jpeg"
					},
					{
						id:4,
						title:"第四张",
						path:"img/4.jpg"
					}]
				},
				methods:{
					mouseenter:function(index){
						this.currenIndex=index
						//这是本案例的重点,把按钮处的索引做参数导入,
						//由此建立起两个模块间的联系(可以知道哪个被点击,哪个被就应该对应显示)
					}
				}
				
			})
		</script>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值