vue v-bind:class 动态添加类以改变样式

要做成这样的点击切换的导航效果

 需要做点击添加类以改变样式

在菜鸟教程上看到了一些方法,大家可以参考学习 Vue.js 样式绑定 | 菜鸟教程 (runoob.com)

但是为了实现效果,我这样写了:

<view class="nav">
			<view class="title_one" :class="{active:current==index}" v-for="(item,index) in list" @click="selectTitle(index)">
				{{item}}
				<text class="borderLine"></text>
			</view>
		</view>

 js

data() {
    return {
        list:["博客园","首页","新随笔","联系"], //定义数组内容
		current:0  //定义 current 当前值
    }
},
methods:{
    selectTitle(index){  //点击事件
			console.log(index);
			this.current = index;
		}
}

css:

.nav{
	width: 100%;
	height: 50px;
	padding: 0 20px;
	box-sizing: border-box;
	display: flex;
}
.title_one{
	width: 25%;
	height: 50px;
	line-height: 50px;
	text-align: center;
	position: relative;
}
.active{
	color: #007AFF;
}
.active .borderLine{
	width: 20px;
	height: 4px;
	background-color: #007AFF;
	position: absolute;
	bottom: 2px;
	left: calc(50% - 10px);
}

这样就可以有点击改变样式的效果啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值