Skip to content

Commit 5a0ced9

Browse files
committedSep 2, 2019
feat(wx-react-native): ScrollView 添加onContentSizeChange, scrollToEnd 方法
fix #22
1 parent c408139 commit 5a0ced9

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed
 

‎packages/wx-react-native/miniprogram_dist/component/WXScrollView/index.comp.js

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ export default class WXScrollView extends RNBaseComponent{
2020
wxInst.scrollTo(position)
2121
}
2222

23+
scrollToEnd() {
24+
const wxInst = instanceManager.getWxInstByUUID(this.__diuu__)
25+
wxInst.scrollTo({
26+
x: 9999999,
27+
y: 9999999
28+
})
29+
}
30+
31+
2332
getStyle(props) {
2433
return {
2534
style: tackleWithStyleObj(props.style, SCROLL),

‎packages/wx-react-native/miniprogram_dist/component/WXScrollView/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,53 @@ Component({
5151
ready: function() {
5252
instanceManager.setWxCompInst(this.data.diuu, this)
5353
this.onScrollFunc = getPropsMethod(this, 'onScroll')
54+
55+
//onContentSizeChange
56+
if (getPropsMethod(this, 'onContentSizeChange')) {
57+
const query = wx.createSelectorQuery().in(this)
58+
query.select('.scroll-area').boundingClientRect((res) => {
59+
this.setData({
60+
contentHeight: res.height
61+
})
62+
63+
this.watchContentSize()
64+
}).exec()
65+
}
5466
},
5567

5668
methods: {
69+
// 监听contentSize的变化
70+
watchContentSize() {
71+
const observer = wx.createIntersectionObserver(this, {observeAll: true})
72+
observer.relativeTo('.scroll-area')
73+
.observe('.ball', (res) => {
74+
const {id, intersectionRatio, relativeRect} = res
75+
if (id === 'above' && intersectionRatio === 0) {
76+
const onContentSizeChangeFunc = getPropsMethod(this, 'onContentSizeChange')
77+
onContentSizeChangeFunc(relativeRect.right - relativeRect.left, relativeRect.bottom - relativeRect.top)
78+
79+
const query = wx.createSelectorQuery().in(this)
80+
query.select('.scroll-area').boundingClientRect((res) => {
81+
this.setData({
82+
contentHeight: res.height
83+
})
84+
}).exec()
85+
}
86+
87+
if (id === 'below' && intersectionRatio === 1) {
88+
const onContentSizeChangeFunc = getPropsMethod(this, 'onContentSizeChange')
89+
onContentSizeChangeFunc(relativeRect.right - relativeRect.left, relativeRect.bottom - relativeRect.top)
90+
91+
const query = wx.createSelectorQuery().in(this)
92+
query.select('.scroll-area').boundingClientRect((res) => {
93+
this.setData({
94+
contentHeight: res.height
95+
})
96+
}).exec()
97+
}
98+
})
99+
},
100+
57101
scrollTo(pos) {
58102
const { x, y } = pos;
59103
this.setData({ outTop: y, outLeft: x })
@@ -88,5 +132,6 @@ Component({
88132
withAni: true,
89133
outLeft: 0,
90134
outTop: 0,
135+
contentHeight: 0,
91136
}
92137
});

‎packages/wx-react-native/miniprogram_dist/component/WXScrollView/index.wxml

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
bindtouchstart='startTouch'
1010
bindtouchend='leaveTouch'
1111
>
12-
<view style="{{contentContainerStyle}}min-height: 100%;">
12+
<view class="scroll-area" style="{{contentContainerStyle}}min-height: 100%;">
1313
<slot/>
1414
</view>
15+
<!--ball 用来检查 contentSizeChange事件-->
16+
<view id="above" class="ball" style="width: 1px; height: 1px; visibility: hidden; position: absolute; top: {{contentHeight - 1}}px;"/>
17+
<view id="below" class="ball" style="width: 1px; height: 1px; visibility: hidden; position: absolute; top: {{contentHeight + 1}}px;"/>
1518
</scroll-view>
1619
</block>
1720
<block wx:else>

‎packages/wx-react-native/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@areslabs/wx-react-native",
3-
"version": "1.0.26",
3+
"version": "1.0.27",
44
"description": "微信版本的React Native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)