小程序中实现横向滚动点击平滑

<template>
	<view>
		<!-- tabs -->
		<view class="scroll-view-container">
			<scroll-view :show-scrollbar="false" scroll-x="true" :scroll-left="x" :scroll-with-animation="true">
				<view class="tab-container">
					<view :class="{active:active == index}" @tap="changeTabs(index)" v-for="(item,index) in tabsList" :key="index">
						{{item}}
						<view v-if="active == index" class="line"></view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				active: 0,
				title: '',
				tabsList: [
					"自考公开课",
					"公开课",
					"专科",
					"本科",
					"公共英语三级"
				],
				x: ''
			};
		},
		onLoad(options) {
			// console.log(options)
			this.title = options.title
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.title
			});
		},
		methods: {
			changeTabs(i) {
				this.active = i
				let screenWidth = uni.getSystemInfoSync().windowWidth;
				let itemWidth = screenWidth / 5;
				let scrollX = itemWidth * i - itemWidth * 2;
				let maxScrollX = (this.tabsList.length + 1) * itemWidth;
				if (scrollX < 0) {
					scrollX = 0;
				} else if (scrollX >= maxScrollX) {
					scrollX = maxScrollX;
				}

				this.x = scrollX
			},
		}
	}
</script>


<style lang="less" scoped>
	.active {
		color: #333;
		font-size: 40upx;
	}

	.scroll-view-container {
		// 包裹scroll-view的容器
		height: 110upx;
		overflow: hidden; // 这个设置了就能截掉滚动条啦
	}

	scroll-view {
		width: 100%;
		white-space: nowrap; // 不让它换行

	}

	scroll-view::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}

	.tab-container {
		display: flex;
		align-items: center;
		height: 120upx;
		font-size: 36upx;
		color: #666;
		padding: 0 30upx 0 0;
		box-sizing: border-box;
		white-space: nowrap;

		>view {
			height: 100%;
			line-height: 120upx;
			position: relative;
			padding: 0 30upx;
			box-sizing: border-box;

			.line {
				position: absolute;
				bottom: 15upx;
				left: 50%;
				transform: translate(-50%);
				width: 30upx;
				height: 6upx;
				border-radius: 6upx;
				background-color: #4FC7FF;
				// margin: 0 auto;
			}
		}
	}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值