From b412c43b56f17b315837c026327f6dbef9d57175 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 13 Aug 2021 18:49:43 +0800 Subject: [PATCH] fix: createIntersectionObserver rectify intersectionRatio --- .../api/request-component-observer.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/view/bridge/subscribe/api/request-component-observer.js b/src/core/view/bridge/subscribe/api/request-component-observer.js index afa7b1512..dc0fe6291 100644 --- a/src/core/view/bridge/subscribe/api/request-component-observer.js +++ b/src/core/view/bridge/subscribe/api/request-component-observer.js @@ -19,6 +19,22 @@ function getRect (rect) { } } +// 在相交比很小的情况下,Chrome会返回相交为0 +function rectifyIntersectionRatio(entrie) { + const { + intersectionRatio, + boundingClientRect: { height: overAllHeight, width: overAllWidth }, + intersectionRect: { height: intersectionHeight, width: intersectionWidth }, + } = entrie + + if (intersectionRatio !== 0) return intersectionRatio + + return intersectionHeight === overAllHeight + ? intersectionWidth / overAllWidth + : intersectionHeight / overAllHeight +} + + const intersectionObservers = {} export function requestComponentObserver ({ @@ -44,7 +60,7 @@ export function requestComponentObserver ({ UniViewJSBridge.publishHandler('onRequestComponentObserver', { reqId, res: { - intersectionRatio: entrie.intersectionRatio, + intersectionRatio: rectifyIntersectionRatio(entrie), intersectionRect: getRect(entrie.intersectionRect), boundingClientRect: getRect(entrie.boundingClientRect), relativeRect: getRect(entrie.rootBounds), -- GitLab