【发布时间】:2021-08-25 14:10:50
【问题描述】:
我有这段代码
import React from 'react'
const App = () => {
function getScrollPercent() {
var h = document.documentElement,
b = document.body,
st = 'scrollTop',
sh = 'scrollHeight';
var scrollPercent = Math.round((h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100);
//get the percentage of scroll
return (isNaN(scrollPercent) ? '' : scrollPercent)
}
return (
<p>{`${getScrollPercent()}%`}</p>
)
问题是当我滚动时,scrollPercent 不会实时刷新,而只会在滚动停止时刷新,所以 scrollPercent 可以从 0% 传递到 100% 并且不显示 0 到 100 之间的所有数字,所以问题是即使滚动,我如何修改我的代码以显示 scrollPercent 的实际值
【问题讨论】:
标签: javascript reactjs animation scroll