mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-20 15:09:40 +00:00
Code🤣
This commit is contained in:
35
components/Progress.js
Normal file
35
components/Progress.js
Normal file
@@ -0,0 +1,35 @@
|
||||
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 (
|
||||
<>
|
||||
{/* 顶部进度条 */}
|
||||
<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 className='debug'>{percent}</div> */}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default Progress
|
||||
Reference in New Issue
Block a user