import React, { useEffect, useState } from 'react' import throttle from 'lodash.throttle' /** * 跳转到网页顶部;当屏幕下滑500像素后会出现该控件 * @returns {JSX.Element} * @constructor */ const Progress = ({ targetRef }) => { const [percent, changePercent] = useState(0) useEffect(() => { const scrollListener = throttle(() => { if (targetRef.current) { const fullHeight = targetRef.current.clientHeight const per = parseFloat(((window.scrollY / (fullHeight) * 100)).toFixed(0)) changePercent(per) } // console.log('滚动信息', window.scrollY, fullHeight, per) }, 1) document.addEventListener('scroll', scrollListener) return () => document.removeEventListener('scroll', scrollListener) }, [percent]) return ( <> {/* 顶部进度条 */}
{/*
{percent}
*/}
) } export default Progress