Skip to content

Commit 27d3a14

Browse files
authored
Merge pull request #1923 from didi/beta-2.10.1-fix-scroll-lower
fix: scrollView lower、upper事件添加滚动方向判断
2 parents 8e51826 + 2bc2242 commit 27d3a14

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/webpack-plugin/lib/runtime/components/react/mpx-scroll-view.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
202202

203203
const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef, onLayout })
204204

205+
const lastOffset = useRef(0)
206+
205207
if (scrollX && scrollY) {
206208
warn('scroll-x and scroll-y cannot be set to true at the same time, Mpx will use the value of scroll-y as the criterion')
207209
}
@@ -259,7 +261,8 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
259261
function onStartReached (e: NativeSyntheticEvent<NativeScrollEvent>) {
260262
const { bindscrolltoupper } = props
261263
const { offset } = scrollOptions.current
262-
if (bindscrolltoupper && (offset <= upperThreshold)) {
264+
const isScrollingBackward = offset < lastOffset.current
265+
if (bindscrolltoupper && (offset <= upperThreshold) && isScrollingBackward) {
263266
if (!hasCallScrollToUpper.current) {
264267
bindscrolltoupper(
265268
getCustomEvent('scrolltoupper', e, {
@@ -280,13 +283,14 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
280283
const { bindscrolltolower } = props
281284
const { contentLength, visibleLength, offset } = scrollOptions.current
282285
const distanceFromEnd = contentLength - visibleLength - offset
283-
if (bindscrolltolower && (distanceFromEnd < lowerThreshold)) {
286+
const isScrollingForward = offset > lastOffset.current
287+
if (bindscrolltolower && (distanceFromEnd < lowerThreshold) && isScrollingForward) {
284288
if (!hasCallScrollToLower.current) {
285289
hasCallScrollToLower.current = true
286290
bindscrolltolower(
287291
getCustomEvent('scrolltolower', e, {
288292
detail: {
289-
direction: scrollX ? 'right' : 'botttom'
293+
direction: scrollX ? 'right' : 'bottom'
290294
},
291295
layoutRef
292296
}, props)
@@ -341,6 +345,8 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
341345
onStartReached(e)
342346
onEndReached(e)
343347
updateIntersection()
348+
// 在 onStartReached、onEndReached 执行完后更新 lastOffset
349+
lastOffset.current = scrollOptions.current.offset
344350
}
345351

346352
function onScrollEnd (e: NativeSyntheticEvent<NativeScrollEvent>) {
@@ -363,6 +369,7 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
363369
onStartReached(e)
364370
onEndReached(e)
365371
updateIntersection()
372+
lastOffset.current = scrollOptions.current.offset
366373
}
367374
function updateIntersection () {
368375
if (enableTriggerIntersectionObserver && intersectionObservers) {

0 commit comments

Comments
 (0)