uniapp:使用mescroll实现左导航,右数据(数据带分页)

<template>
	<view class="u-wrap">
		<u-navbar title="复购" :is-back="false" :border-bottom="false" title-color="#282828"></u-navbar>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in navList" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index,item.id)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :fixed="true" :top="mescrollTop" class="mescroll">
				<view class="right-box">
					<view class="page-view">
						<view class="class-item">
							<view class="item-container">
								<view class="thumb-box" v-for="(item, index) in goodsList" :key="index" @click="$go(2,'/pages/mall/goodsDetail?goods_id='+item.id)">
									<image class="item-menu-image" :src="item.image" mode=""></image>
									<view class="fsz-26 u-line-1 mb-10">{{item.title}}</view>
									<view class="">
										<text class="fsz-22 color-red"></text>
										<text class="fsz-28 color-red">{{item.price}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	let systemInfo = uni.getSystemInfoSync();
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				navList:[],
				goodsList:[],
				id:'',
				mescrollTop:'44px',
			}
		},
		onLoad() {
			let height = systemInfo.platform == 'ios' ? 44 : 48;
			// #ifndef H5
			this.mescrollTop = systemInfo.statusBarHeight+height+'px';
			// #endif
			// #ifdef H5
			this.mescrollTop = height+'px';
			// #endif
			
			this.$http.post('/api/goodsSortList').then(res=>{
				if(res.code == 200){
					this.navList = res.data
					this.id = this.navList[0].id
					this.mescroll.resetUpScroll();
				}
			})
		},
		methods: {
			/*下拉刷新的回调*/
			downCallback() {
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				if(this.id == '') return
				this.$http.post('/api/getGoods', {
					page: page.num,
					sort_id:this.id
				}).then(res => {
					if (res.code == 200) {
						let arr = res.data.data;
						if (page.num === 1) this.goodsList = [];
						this.goodsList = this.goodsList.concat(arr);
						this.$nextTick(() => {
							this.mescroll.endSuccess(arr.length)
						})
					}
				}).catch((e) => {
					this.mescroll.endErr();
				});
			},
			// 点击左边的栏目切换
			async swichMenu(index,id) {
				if(index == this.current) return ;
				this.id = id
				this.mescroll.resetUpScroll();
				this.current = index;
				// 如果为0,意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成,res值为null,循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh - var(--window-bottom));
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #fff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #fff;
		font-size: 30rpx;
		font-weight: 600;
		background: #FF770F;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #FF770F;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	.right-box{
		width: 550rpx;
	}
	/deep/.mescroll{
		width: 550rpx;
		border: 1px solid red;
		.mescroll-uni{
			width: 550rpx;
			right: 0;
			left: auto;
		}
	}
	.page-view {
		padding-bottom: 1rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
	}
	

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 240rpx;
		height: 335rpx;
		margin-top: 20rpx;
		margin-left: 25rpx;
		background: #fff;
		border-radius: 10rpx;
		padding: 20rpx;
	}
	
	.item-menu-image {
		display: block;
		margin: 0 auto 15rpx;
		width: 204rpx;
		height: 204rpx;
	}
</style>

mescroll-uni 需要定位
兼容H5 APP 小程序
在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp实现下拉分页加载数据,可以通过以下步骤: 1. 首先,在页面中引入下拉刷新组件和分页加载组件。 ```html <template> <view> <list :finished="finished" @load="loadData" > <refresh refresher-triggered="onRefresh"> <!-- 下拉刷新的内容 --> </refresh> <cell v-for="(item, index) in dataList" :key="index"> <!-- 显示数据项的内容 --> </cell> <loading v-if="loading"></loading> </list> </view> </template> ``` 2. 在页面的 `data` 中定义数据相关的变量,如 `dataList` 用于存储加载的数据,`loading` 用于标识数据加载中,`finished` 用于标识是否已加载完所有数据。 ```javascript export default { data() { return { dataList: [], loading: false, finished: false, }; }, // ... }; ``` 3. 实现下拉刷新和分页加载的方法。 ```javascript export default { // ... methods: { // 下拉刷新 onRefresh() { // 清空数据列表 this.dataList = []; // 重置加载状态 this.finished = false; // 加载数据 this.loadData(); }, // 分页加载 loadData() { if (this.loading || this.finished) return; this.loading = true; // 发起网络请求获取数据 // 假设每页加载 10 条数据 // 模拟异步请求延迟 setTimeout(() => { // 模拟请求成功 const newData = [...]; // 新加载的数据 if (newData.length > 0) { this.dataList = this.dataList.concat(newData); } else { this.finished = true; // 数据已加载完 } this.loading = false; }, 1000); }, }, }; ``` 以上是一个简单的示例,实现了下拉刷新和分页加载数据的功能。当用户下拉刷新页面时,会触发 `onRefresh` 方法,清空数据并重新加载;当用户滚动到列表底部时,会触发 `loadData` 方法,加载更多数据并追加到列表中。你可以根据你的实际需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值