Skip to content

Commit 408d835

Browse files
authored
fix(pull-down-refresh): improved distance top update (#3863)
1 parent efb88ef commit 408d835

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/common/wechat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const getObserver = (context, selector: string) => {
2-
return new Promise((resolve) => {
2+
return new Promise<WechatMiniprogram.IntersectionObserverObserveCallbackResult>((resolve) => {
33
context
44
.createIntersectionObserver(context)
55
.relativeToViewport()

src/pull-down-refresh/pull-down-refresh.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getObserver } from '../common/wechat';
12
import { RelationsOptions, SuperComponent, wxComponent } from '../common/src/index';
23
import config from '../common/config';
34
import props from './props';
@@ -70,11 +71,7 @@ export default class PullDownRefresh extends SuperComponent {
7071

7172
this.pixelRatio = 750 / screenWidth;
7273

73-
getRect(this, `.${name}`).then((rect) => {
74-
this.setData({
75-
distanceTop: rect.top,
76-
});
77-
});
74+
this.updateDistanceTop();
7875
},
7976

8077
detached() {
@@ -119,6 +116,27 @@ export default class PullDownRefresh extends SuperComponent {
119116
};
120117

121118
methods = {
119+
updateDistanceTop() {
120+
const update = (top: number) => {
121+
this.setData({
122+
distanceTop: top,
123+
});
124+
};
125+
126+
getRect(this, `.${name}`).then((rect) => {
127+
if (rect.top) {
128+
update(rect.top);
129+
return;
130+
}
131+
132+
getObserver(this, `.${name}`).then((res) => {
133+
if (res.intersectionRatio > 0) {
134+
update(res.boundingClientRect.top);
135+
}
136+
});
137+
});
138+
},
139+
122140
resetTimer() {
123141
if (this.refreshStatusTimer) {
124142
clearTimeout(this.refreshStatusTimer);
@@ -129,11 +147,13 @@ export default class PullDownRefresh extends SuperComponent {
129147
onScrollToBottom() {
130148
this.triggerEvent('scrolltolower');
131149
},
150+
132151
onScrollToTop() {
133152
this.setData({
134153
enableToRefresh: true,
135154
});
136155
},
156+
137157
onScroll(e) {
138158
const { scrollTop } = e.detail;
139159

@@ -142,6 +162,7 @@ export default class PullDownRefresh extends SuperComponent {
142162
});
143163
this.triggerEvent('scroll', { scrollTop });
144164
},
165+
145166
onTouchStart(e: WechatMiniprogram.Component.TrivialInstance) {
146167
if (this.isPulling || !this.data.enableToRefresh || this.properties.disabled) return;
147168
const { touches } = e;

0 commit comments

Comments
 (0)