个人笔记002--vue点击按钮实现状态的切换

昨天项目改版,写了一个功能——点击按钮在启用/未启用两个状态之间切换。下面直接上代码

<template>
	<div class="hello">
		<div class="payShow" v-for="n,index in details" :key='index'>
			<!--按钮-->
			<div @click="changStatus(index)" :class="n.status==1?'selected':'default'">
				<div v-if="n.status==1" class="btns">
					<span class="on">ON</span>
					<span class="cir_right"></span>
				</div>
				<div class="btns" v-else>
					<span class="cir_left"></span>
					<span class="off">OFF</span>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'Hello',
		data() {
			return {
				details: [{
						status: '1'
					},
					{
						status: '2'
					},
					{
						status: '2'
					},
					{
						status: '1'
					},
					{
						status: '1'
					},
					{
						status: '2'
					}
				]
			}
		},
		methods: {
			changStatus(a) {
				this.details[a].status = this.details[a].status == 2 ? 1 : 2
			}
		}
	}
</script>

<style scoped>
	/*按钮间距*/
	.btns {
		overflow: hidden;
	}
	
	/*左边圆*/
	.cir_left {
		float: left;
		width: 18px;
		height: 18px;
		border-radius: 50%;
		background-color: white;
		margin: 2px;
	}
	
	/*右边圆*/
	.cir_right {
		float: right;
		width: 18px;
		height: 18px;
		border-radius: 50%;
		background-color: white;
		margin: 2px;
	}
	
	.on {
		float: left;
		margin: 1px 4px;
		color: white;
	}
	
	.off {
		float: right;
		margin: 1px 4px;
		color: white;
	}
	
	/*选择*/
	.selected {
		display: inline-block;
		vertical-align: middle;
		width: 70px;
		height: 22px;
		line-height: 22px;
		margin: 10px;
		border-radius: 10px;
		background-color: #7A98F7;
		cursor: pointer;
	}
	
	/*未选中*/
	.default {
		display: inline-block;
		vertical-align: middle;
		width: 70px;
		height: 22px;
		line-height: 22px;
		margin: 10px;
		border-radius: 10px;
		background-color: #A7B6C2;
		cursor: pointer;
	}
</style>

上面这段代码也能实现那些点击收藏或者取消的功能,根据接口的返回数据把值赋给detail然后判断状态。当然,也欢迎小伙伴们指出不足之处

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值