TypeScript ux/dom setScroll示例

说明

typescript ux/dom setscroll示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: ux/dom

示例#1
文件: scroll.ts 项目: Teal/TealUI

export function scrollTo(elem: HTMLElement | Document, value: Partial<Point>, duration = 150, callback?: (value: Point) => void) {
    let last = getScroll(elem);
    if (duration === 0) {
        setScroll(elem, value);
    } else if (value.x != null || value.y != null) {
        let leftX = (value.x! - last.x) || 0;
        let leftY = (value.y! - last.y) || 0;
        const count = duration / 10;
        const deltaX = leftX / count;
        const deltaY = leftY / count;
        const step = () => {
            const current = getScroll(elem);
            if (current.x === last.x && current.y === last.y) {
                if (Math.abs(leftX) > Math.abs(deltaX)) {
                    current.x += deltaX;
                    leftX -= deltaX;
                } else {
                    current.x = value.x!;
                }

                if (Math.abs(leftY) > Math.abs(deltaY)) {
                    current.y += deltaY;
                    leftY -= deltaY;
                } else {
                    current.y = value.y!;
                }

                setScroll(elem, current);
                last = getScroll(elem);
                setTimeout(step, 10);
            } else {
                callback && callback(last);
            }
        };
        step();
        return;
    }
    callback && callback(last);
}

示例#2
文件: scroll.ts 项目: Teal/TealUI

        const step = () => {
            const current = getScroll(elem);
            if (current.x === last.x && current.y === last.y) {
                if (Math.abs(leftX) > Math.abs(deltaX)) {
                    current.x += deltaX;
                    leftX -= deltaX;
                } else {
                    current.x = value.x!;
                }

                if (Math.abs(leftY) > Math.abs(deltaY)) {
                    current.y += deltaY;
                    leftY -= deltaY;
                } else {
                    current.y = value.y!;
                }

                setScroll(elem, current);
                last = getScroll(elem);
                setTimeout(step, 10);
            } else {
                callback && callback(last);
            }
        };

展开阅读全文