uniapp 实现局部下拉刷新

<template>
	<view>
		<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
			<view class="scroll-view-item_H" v-for="(tab,index) in tabBars" :key="tab.id" :id="tab.id"
				:class="navIndex==index ? 'activite' : ''" @click="checkIndex(index)">{{tab.name}}</view>
		</scroll-view>
		<!-- 内容切换 -->
		 <view>
		        <scroll-view style="height: 300px;" scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered"
		            :refresher-threshold="100" refresher-background="#FFF" @refresherpulling="onPulling"
		            @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
					<view class="content" v-if="navIndex==0">
						我是选项1
					</view>
					<view class="content" v-if="navIndex==1">
						我是选项2
					</view>
				</scroll-view>
		    </view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				triggered: false,
				tabBars: [{
					name: '关注',
					id: 'guanzhu'
				}, {
					name: '推荐',
					id: 'tuijian'
				}, {
					name: '体育',
					id: 'tiyu'
				}],
			}
		},
		 onLoad() {
			   this._freshing = false;
		    // this._freshing = false;
		    // setTimeout(() => {
		    // this.triggered = true;
		    // }, 1000)
		        },
		methods: {
			checkIndex(index) {
				console.log(index)
				this.navIndex = index;
			},
			scroll: function(e) {
				console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			 onPulling(e) {
				 this._freshing = false;
				 setTimeout(() => {
				 this.triggered = true;
				 }, 1000)
			    // console.log("onpulling", e);
				//调接口,等接口回来之后,关闭 this.triggered = true;
				console.log("下拉刷新了")
			},
			onRefresh() {
			    if (this._freshing) return;
			    this._freshing = true;
			    setTimeout(() => {
			        this.triggered = false;
			        this._freshing = false;
			    }, 1000)
			},
			onRestore() {
			    this.triggered = 'restore'; // 需要重置
			    console.log("onRestore");
			},
			onAbort() {
			console.log("onAbort");
			}
		}
	}
</script>

<style scoped>
	.activite {
		color: #04c9c3;
	}

	.content {
		/* background: #008000; */
		height: 300px;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
		color: #CCCCCC;
	}

	.scroll-view-item_H {
		display: inline-block;
		width: 20%;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		padding: 10px 0;
	}
</style>

需要注意的是初始化的时候,我们会调接口获取列表数据, this.triggered = true;这个是关掉下拉刷新的,我当时遇到的问题是一进页面就会刷新,发现 this.triggered = true;写在了初始化获取数据成功之后(status==200),然后我 onPulling时,再次调的获取列表这个函数

解决办法是: 我把调接口的代码在onPulling时又写了一遍,接口调用成功, this.triggered = true;

初始化的时候,(不加 this.triggered = true;)

以上是我写的一个小案例,直接放在项目里面就可以啦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值