【发布时间】:2022-02-09 17:27:06
【问题描述】:
我想确保滚动条位于区域的中间以防滚动。
const scrollRef = useScroll(null);
useEffect(() => {
const wrapper = scrollRef.current;
console.dir(wrapper, 'wrapper');
if (wrapper) {
const hasScroll = wrapper.scrollHeight > wrapper.clientHeight;
console.log(wrapper.scrollHeight, 'scrollheight');
console.log(wrapper.clientHeight, 'clientHeight');
if (hasScroll) {
const scrollTop = (wrapper.scrollHeight - wrapper.clientHeight) / 2;
wrapper.scrollTop = scrollTop;
}
}
}, [isLoading]);
if (isLoading) {
return (
<TradeSection>
<Loader />
</TradeSection>
);
}
<Section>
<TableHeader>
<TableList ref={scrollRef}>
<List />
<CurrentPrice />
<List />
</TableList>
<Section>
此代码总体上运行良好。 但有时它不起作用 根据我的猜测,如果你在屏幕尺寸改变后刷新, 所以我在 useEffect 中添加了控制台
为什么 console.dir > scrollHeight 是不同的结果? 为什么在同一区域显示不同的值?
【问题讨论】:
标签: reactjs scrollview scrollbar use-effect console.log