-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useInfiniteScroll 的getScrollTop方法问题 #2283
Comments
不能直接这么改,会有 break change,应该是安卓浏览器不兼容的问题吗? |
安卓浏览器命中了 window.pageYOffset
document.documentElement.scrollTop
document.body.scrollTop, |
应该是安卓下面的 |
|
正常情况下 document.documentElement.scrollTop 是有值的,是不是你所在的浏览器不兼容。可以看看上面提到之前修复一个问题的上下文
|
问题上下文大概是,下面这段逻辑有问题,如果是 document,让其走 document.documentElement,不让进入这个分支 if (el === document || el === document.body) {
return Math.max(
window.pageYOffset,
document.documentElement.scrollTop,
document.body.scrollTop,
);
} |
我看你提到的那个修复,是修复的getClientHeight的问题,但那个改动影响了document 的 getScrollTop的获取, |
el 应该换啥呢?本身 el 就是document,整个页面滚动到底部触发自动加载更多啊,呜呜呜~我其实尝试过用document.body,但 getClientheight又不对了 |
不兼容这种情况比较少见,可能是安卓不兼容,你可以按照下面的方案来,应该是没问题的 const el = useMemo(() => {
const doc = document.documentElement;
Reflect.defineProperty(doc, 'scrollTop', {
get() {
return Math.max(
window.pageYOffset,
document.documentElement.scrollTop,
document.body.scrollTop,
);
},
});
Reflect.defineProperty(doc, 'scrollHeight', {
get() {
return document.documentElement.scrollHeight;
},
});
Reflect.defineProperty(doc, 'clientHeight', {
get() {
return document.documentElement.clientHeight;
},
});
return doc;
}, []); |
我其实没有理解getClientHeight获取的有问题,为什么要更改getScrollTop兼容方法的获取逻辑? |
两者不可兼得吧,绝大部分都是兼容的,document.documentElement.scrollTop 不兼容的情况很少看见 |
可以是可以,不过会出现获取一些信息元素不一致的情况,可能会存在其他场景的 badcase |
getScrollTop 这个方法我搜了一下只有 useInfiniteScroll 这个hook在用,目前 badcase 就在眼前啊,如果有demo的话可以试试安卓机 |
其他场景指的是 document 获取 scrollTop 和 clientHeight 不一致的情况,不是指被引用情况 |
想了一下,应该需要处理一下,可否来个 PR |
如果没时间提 PR 的话说下哈,后续我处理一下 |
我提一个 PR 吧,#2285 |
这个方法可以解决你的问题吗 const el = useMemo(() => {
const doc = document.documentElement;
Reflect.defineProperty(doc, 'scrollTop', {
get() {
return Math.max(
window.pageYOffset,
document.documentElement.scrollTop,
document.body.scrollTop,
);
},
});
Reflect.defineProperty(doc, 'scrollHeight', {
get() {
return document.documentElement.scrollHeight;
},
});
Reflect.defineProperty(doc, 'clientHeight', {
get() {
return document.documentElement.clientHeight;
},
});
return doc;
}, []); |
@LJJCherry 改成这样即可 const el = useMemo(() => {
const doc = document.documentElement;
Reflect.defineProperty(doc, 'scrollTop', {
get() {
return Math.max(
window.pageYOffset,
document.body.scrollTop,
);
},
});
Reflect.defineProperty(doc, 'scrollHeight', {
get() {
return document.documentElement.scrollHeight;
},
});
Reflect.defineProperty(doc, 'clientHeight', {
get() {
return document.documentElement.clientHeight;
},
});
return doc;
}, []); |
也还是不行,下面这行代码会报Maximum call stack size exceeded,屏蔽掉这个, 下面clineHeight也会报这个错,把这俩都屏蔽掉,不报错了,页面也无法滚动加载更多了 Reflect.defineProperty(doc, 'scrollHeight', {
get() {
return document.documentElement.scrollHeight;
},
}); |
把 |
放弃,我直接先写死了3.7.5的版本号 |
相关代码链接:
hooks/packages/hooks/src/useInfiniteScroll/index.tsx
Line 90 in 6579130
导致el = document的时候,在某些安卓浏览器里面没有获取到scrollTop,getScrollTop应该改成
The text was updated successfully, but these errors were encountered: