mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 20:04:09 +00:00
feat(Tab): add scrollspy prop (#5273)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { raf } from '../utils/dom/raf';
|
||||
import { getRootScrollTop, setRootScrollTop } from '../utils/dom/scroll';
|
||||
|
||||
export function scrollLeftTo(el: HTMLElement, to: number, duration: number) {
|
||||
let count = 0;
|
||||
@@ -15,3 +16,28 @@ export function scrollLeftTo(el: HTMLElement, to: number, duration: number) {
|
||||
|
||||
animate();
|
||||
}
|
||||
|
||||
export function scrollTopTo(to: number, duration: number, cb: Function) {
|
||||
let current = getRootScrollTop();
|
||||
const toDown = current < to;
|
||||
const frames = duration === 0 ? 1 : Math.round((duration * 1000) / 16);
|
||||
const pxPerFrames = (to - current) / frames;
|
||||
|
||||
function animate() {
|
||||
current += pxPerFrames;
|
||||
|
||||
if ((toDown && current > to) || (!toDown && current < to)) {
|
||||
current = to;
|
||||
}
|
||||
|
||||
setRootScrollTop(current);
|
||||
|
||||
if ((toDown && current < to) || (!toDown && current > to)) {
|
||||
raf(animate);
|
||||
} else {
|
||||
cb && cb();
|
||||
}
|
||||
}
|
||||
|
||||
animate();
|
||||
}
|
||||
|
Reference in New Issue
Block a user