/*
使用方法:
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);
};