uniapp 小程序仿美团外卖商品分类,点击选择分类当前点击的元素保持在屏幕中间

仿美团外卖商品分类功能,点击元素会自动滚动到当前容器的中间位置。

效果:

701_1723183953


代码:

HTML:
<view class="class-view">
		<scroll-view class="class-left" scroll-y="true" :scroll-top="newScrollTop" :scroll-with-animation="true">
			<text class="class-left-item" v-for="item,index in tag_list" :key="index" @click="select(index, item)"
				:class="test == index ? 'active_left' : ''">{{ item.tag_name }}</text>
		</scroll-view>
		<view class="class-right">
			<view @click="nav_details(item.id)" v-for="item,index in game_list" :key="index">
				<image :src="item.icon" mode=""></image>
				<view>
					<text>{{ item.game_name }}</text>
					<text>{{ item.resh_tag }}</text>
				</view>
			</view>
		</view>
	</view>
JS:
export default {
		data() {
			return {
				test: 0,
                tag_list: [], // 分类标签数组
				newScrollTop: 0
			}
		},
		methods: {
			select(index, item) {
				this.test = index;
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.class-left-item').boundingClientRect((rects) => {
					const containerHeight = uni.getSystemInfoSync().windowHeight;
					const itemTop = rects[index].top;
					const itemHeight = rects[index].height;
					const containerTop = rects[0].top;
					// 计算新滚动条的位置,使选中项在可视区域居中
					this.newScrollTop = itemTop - containerTop + (itemHeight / 2) - (containerHeight / 2);
					console.log(this.newScrollTop);
				}).exec();
			},
		}
	}
CSS:
.class-view {
		display: flex;

        // 左侧分类列表容器
		.class-left {
			height: 100vh;
			width: 25%;
			background: #f1f1f1;
			display: flex;
			align-items: center;
			flex-direction: column;
			overflow-y: scroll;

			.class-left-item {
				color: #333;
				display: block;
				width: 100%;
				height: 100rpx;
				font-size: 32rpx;
				line-height: 100rpx;
				text-align: center;
			}
		}
        

        // 右侧选中分类后需要展示的数据
		.class-right {
			height: 100vh;
			width: 75%;
			overflow-y: scroll;
		}
	}

// 选中当前元素添加的样式
.active_left {
		color: #000 !important;
		font-size: 34rpx !important;
		font-weight: 550 !important;
		background: #fff !important;
	}
UniApp 中,要让 App 端唤起美团小程序,你需要使用 UniApp 提供的 `uni-navigateToMiniProgram` 方法,这是一个用于启动微信小程序的接口。这个方法需要小程序AppID 和页面路径。 以下是一个简单的步骤说明: 1. **获取小程序 AppID**:首先,确保你已经拥有一个美团小程序AppID。如果没有,你需要在美团开放平台(https://open.meituan.com/)注册并创建小程序。 2. **配置环境**:在 UniApp 的项目中,需要在配置文件(通常在 `config.js` 或者 `global.json` 中)里添加小程序的相关信息,如 AppID、小程序的scheme名称等。 ```javascript // config.js 或 global.json 示例 { "uni-app": { // ... "miniprogram": { "appid": "your_mituan_appid", "scheme": "meituan-scheme" }, // ... } } ``` 3. **调用接口**:在需要唤醒小程序的地方,使用 `uni-navigateToMiniProgram` 方法,传入页面路径和一些额外参数(如 query 参数)。 ```javascript import { uni.navigateToMiniProgram } from '@dcloudio/uni-app'; uni.navigateToMiniProgram({ appId: 'your_mituan_appid', path: '/pages/index/index', // 小程序的页面路径 query: { key: 'your_param_value' // 可选的查询参数 } }); ``` 4. **用户授权**:如果这是第一次访问,用户可能需要授权才能访问小程序。开发者需要处理`onLaunch`生命周期函数中的`getLaunchOptions`回调。 注意:这个过程依赖于微信客户端的环境支持,并且不是所有场景都能直接唤醒,例如用户未安装美团小程序或微信未打开。如果用户未安装,需要引导他们去微信小程序商店搜索安装。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值