【发布时间】:2020-05-01 04:03:03
【问题描述】:
有一个天花板tabBar,代表我页面的不同部分,我尝试在componentDidMount中获取每个部分的offsetTop,然后我可以通过比较document.element.scrollTop和offsetTop来切换tabBar。但是我发现我无法在 componengtDidMount 中获取每个部分的正确 offsetTop,因为元素没有被完全渲染。
这里有一些相关代码
const tabList = [
{
title: 'xxxx1',
id: 'feature',
offsetTop: 0
},
{
title: 'xxxx2',
id: 'compensation',
offsetTop: 0
},
{
title: 'xxxx3',
id: 'faq',
offsetTop: 0
},
{
title: 'xxxx4',
id: 'form',
offsetTop: 0
}
]
componentDidMount() {
this.getEleOffsetTop()
}
getEleOffsetTop = () => {
tabList.forEach(item => {
let node = document.getElementById(`${item.id}`)
if (node) {
item.offsetTop = node.offsetTop
}
})
console.log('tabList', tabList)
}
【问题讨论】:
标签: javascript html reactjs frontend