uni-app微信小程序tab切换demo

 具体效果图如上

因为refs获取元素在小程序环境是不能被使用的,我们只能通过小程序提供的api --

uni.createSelectorQuery()来获取元素位置信息

 js部分

updateTabWidth(){
				const query = uni.createSelectorQuery().in(this);
				query.select('#_tab_' + this.activeIndex).boundingClientRect(res=>{
					this.left = res.width/2 + res.width * (this.activeIndex -1) - 16;
				}).exec();
			},
			changeIndex(index){
				this.activeIndex = index;
				this.updateTabWidth();
			}

html部分

<view class="menu">
				<view class="item" v-for="item in list" :key="item.id" @tap="changeIndex(item.id)" :id="'_tab_'+item.id">
					{{item.name}}
				</view>
				<view class="lines" :style="{transform: 'translateX(' + left + 'px)'}"></view>
			</view>

scss部分

.menu{
				position: relative;
				display: flex;
				align-items: center;
				width: 100%;
				height: 100rpx;
				.item{
					width: 33%;
					font-size: 32rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #212121;
					line-height:50rpx;
					text-align: center;
				}
				.lines{
					width: 64rpx;
					height: 6rpx;
					background: #FFD61E;
					border-radius: 4rpx;
					transition: .5s cubic-bezier(.215,.61,.355,1);
					position: absolute;
					bottom: 16rpx;
				}
			}

实现原理是id选择器标志,获取不同id标志的位置信息,然后计算出下划线需要滑动的距离,加上渐变样式,菜单滑动选择器就实现了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值