Download ant-design/components/_util/scrollTo.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/ant-design/components/_util/scrollTo.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/ant-design/components/_util/scrollTo.ts
-
curl -L -o scrollTo.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/ant-design/components/_util/scrollTo.ts
1.35 kB
| import raf from 'rc-util/lib/raf'; | |
| import { easeInOutCubic } from './easings'; | |
| import getScroll, { isWindow } from './getScroll'; | |
| interface ScrollToOptions { | |
| /** Scroll container, default as window */ | |
| getContainer?: () => HTMLElement | Window | Document; | |
| /** Scroll end callback */ | |
| callback?: () => void; | |
| /** Animation duration, default as 450 */ | |
| duration?: number; | |
| } | |
| export default function scrollTo(y: number, options: ScrollToOptions = {}) { | |
| const { getContainer = () => window, callback, duration = 450 } = options; | |
| const container = getContainer(); | |
| const scrollTop = getScroll(container); | |
| const startTime = Date.now(); | |
| const frameFunc = () => { | |
| const timestamp = Date.now(); | |
| const time = timestamp - startTime; | |
| const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration); | |
| if (isWindow(container)) { | |
| (container as Window).scrollTo(window.pageXOffset, nextScrollTop); | |
| } else if (container instanceof Document || container.constructor.name === 'HTMLDocument') { | |
| (container as Document).documentElement.scrollTop = nextScrollTop; | |
| } else { | |
| (container as HTMLElement).scrollTop = nextScrollTop; | |
| } | |
| if (time < duration) { | |
| raf(frameFunc); | |
| } else if (typeof callback === 'function') { | |
| callback(); | |
| } | |
| }; | |
| raf(frameFunc); | |
| } | |