/*
使用方法:
import { useRef } from 'react'; const anchor = useRef<HTMLDivElement>(null); <div ref={anchor} > test </div>
<button onClick={()=> { scrollWithAnimation(anchor.current?.offsetTop || 0);
}} > jump </button>
*/ export const scrollWithAnimation = (dest: number) => { const sub = (target: number) => { const cur = window.scrollY; const offset = cur - target; if (offset > 150) { window.scrollTo(0, cur - 150); setTimeout(() => { sub(target); }, 60); } else { window.scrollTo(0, target); } }; sub(dest - 100); };

 

 posted on 2023-05-30 17:29  laremehpe  阅读(14)  评论(0编辑  收藏  举报