uniapp:列表渲染下tap切换底部滑动块效果实现(vue)升级

36 篇文章 0 订阅
36 篇文章 1 订阅

这次之前得tap切换效果是实现了但是,不大灵动,比如这样子的:

可以看见此时得tap切换下面得横条是直接消失又出现得,下面得是升级版:

这样得话是不是就看起来丝滑很多下面得横条是直接从第一个盒子滑动到第二个盒子。

源码如下:

<template>
	<view class="tap">
		<view class="container">
			<view class="item" v-for="(item, index) in itemList" :key="index" :class="{ active: selectedIdx === index }"
				@click="selectItem(index)">
				{{ item.name }}
			</view>
			<view class="line" :style="{ left: selectedIdx * 374 + 'rpx' }">

			</view>
		</view>
		
		<view class="information" v-show="selectedIdx==0" >
			<pinformation></pinformation>
		</view>
		<view class="informations" v-show="selectedIdx==1" >
			<step></step>
		</view>
	</view>
</template>

<script>
	import pinformation from '@/components/pinformation/pinformation.vue';
	import step from  '@/components/step/step.vue'
	export default {
		components:{
			pinformation,
			step
		},
		data() {
			return {
				selectedIdx: 0, // 默认选中第一个对象
				itemList: [{
						name: '基本信息'
					},
					{
						name: '审批流程'
					}
				],
			};
		},
		methods: {
			selectItem(index) {
				this.selectedIdx = index; // 更新选中的索引
			},
		},
	};
</script>

<style lang="scss" scoped>
	.tap{
		width: 100%;
		height: 100vh;
		background-color: #ffffff;
	}
	.container {
		position: relative;
		display: flex;
	}

	.item {
		flex: 1;
		text-align: center;
		padding: 10px;
		cursor: pointer;
		color: #b7b8b7;
	}

	.active {
		color: #5290ff;
		background-color: #ffffff;
	}

	.line {
		position: absolute;
		bottom: 0;
		height: 4rpx;
		width: 374rpx;
		background-color: #5290ff;
		transition: left 0.5s;
	}
	.information{
		width: 100%;
		height: 100vh;
		background-color: #f9f9f9;
	}
	.informations{
		width: 100%;
		height: 100vh;
		padding-top: 20rpx;
		background-color: #f9f9f9;
	}
	.Type-purchase{
		width: 100%;
		height: 100rpx;
		background-color: #ffffff;
	}
</style>

注意:代码中的这些部分是自定义组件,需要区components目录中进行注册才行

核心部分:

html部分

JavaScript部分

css部分

原理:先在父级盒子中的container的样式中加入position: relative;  在横条从横条的样式中加入position: absolute;组成子绝父相的格式,然后bottom=0,让横条在父级盒子底部,然后设置transition: left 0.5s;让向右滑动的效果持续0.5秒,然后在html页面中通过v-bind的绑定横条left的样式通过索引selectedIdx * 374(这是每个盒子的宽度)+rpx(单位),在JavaScript中通过列表渲染的每个盒子的下标index赋值给selectedIdx从而实现横条向右滑动的效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值