feat(Tab): add scrollspy prop (#5273)

This commit is contained in:
健忘症患者丶
2019-12-16 20:17:09 +08:00
committed by neverland
parent 9cd06f3b20
commit 4603e1633c
12 changed files with 266 additions and 10 deletions

View File

@@ -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();
}