开发一个uniapp项目时,需要使用做一个能左右滑动的表,如下图

.nav-tab-wrap{
		width: 100%;
		white-space: nowrap;
		.nav-tab{
			position: relative;
			display: flex;
			justify-content: space-around;
			.nav-tab-item{
				margin-right: 60rpx;
				line-height: 80rpx;
				display: inline-block;
				color: #333333;
				font-weight: bold;
				font-size: 30rpx;
				&:nth-of-type(1){
					margin-left: 60rpx;
				}
				&.active{
					color: #00a1ff; 

				}
			}
			// uni-view.underline {
			// 	left: 130rpx !important;
			// 	width: 150rpx !important;
			// }
			.underline{
				position: absolute;
				width: 40rpx;
				height: 5rpx;
				background-color: #00a1ff;
				left: 20rpx;
				bottom: 0;
				transition: 0.4s;
			}
		}
	}

1.template

<scroll-view scroll-x class="nav-tab-wrap" :throttle="false" @scroll="scrollEvent">
		<view class="nav-tab">
			<block v-for="(item,index) in tabList">
				<view class="nav-tab-item" :class="{'active':currentIndex==index}" @tap="changeTab(index)">{{item}}</view>
			</block>
			<view class="underline" :style="'left:'+ left +'px;width:'+ width +'px;'"></view>
		</view>
	</scroll-view>

	<swiper class="swiper" :current="currentIndex" @change="changeIndex" style="touch-action: none;">
		<swiper-item v-for="(item, index) in contentList" :key="index">
			<scroll-view style="height: 100%;" scroll-y>
				<view>{{ item.content }}</view>
			</scroll-view>
		</swiper-item>
	</swiper> 

 2.script

currentIndex: 0,
		contentList: [
			{
				content: '首页1'
			},
			{
				content: '首页2'
			},
		], //
		value: "",
		currentIndex:0, //当前选中的索引值
		tabList:["浓度设备", "状态设备"],
		left:uni.upx2px(123), //滑动条左边距离
		width: uni.upx2px(160), //滑动条宽度
		scrollX:0, //滚动条的位置
		space:uni.upx2px(-20),//滑动条和item的左右间距
		navIndex: 0,

methods:
changeTab(num){
		let that = this;
		that.currentIndex = num;
		let selectorQuery = uni.createSelectorQuery().in(that);
		selectorQuery.selectAll('.nav-tab-item').boundingClientRect(function(data) {
			// data 为当前选中tab的节点信息
			that.left = data[num].left + that.scrollX + that.space;
			that.width = data[num].width - that.space*2;
		}).exec();
		console.log(num);
	},
	scrollEvent(e){
		this.scrollX = e.detail.scrollLeft;
	},
	changeIndex(e) {
		this.currentIndex = e.detail.current;
		console.log(e);
		let that = this;
		let selectorQuery = uni.createSelectorQuery().in(that);
		selectorQuery.selectAll('.nav-tab-item').boundingClientRect(function(data) {
			// data 为当前选中tab的节点信息
			that.left = data[e.detail.current].left + that.scrollX + that.space;
			that.width = data[e.detail.current].width - that.space*2;
		}).exec();
		// this.scrollEvent();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值