mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
30 lines
1016 B
JavaScript
30 lines
1016 B
JavaScript
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) * 100)).toFixed(0))
|
||
changePercent(per)
|
||
}
|
||
// console.log('滚动信息', window.scrollY, fullHeight, per)
|
||
}, 1)
|
||
document.addEventListener('scroll', scrollListener)
|
||
return () => document.removeEventListener('scroll', scrollListener)
|
||
}, [percent])
|
||
|
||
return (<div className='h-1.5 fixed top-0 w-full shadow-2xl z-40'>
|
||
<div className='h-1 bg-blue-500 fixed top-0 w-1 duration-200' style={{ width: `${percent}%` }}/>
|
||
</div>)
|
||
}
|
||
|
||
export default Progress
|