Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

嵌套滚动内容时,内部滚动效果会直接穿透到外层虚拟列表不生效 #292

Open
Guan-Erjia opened this issue Dec 3, 2024 · 0 comments

Comments

@Guan-Erjia
Copy link

目前只能用这种方法在内部滚动区域拦截 wheelCapture 事件,有没有什么更简洁的方法呢

import {UIEvent, useCallback, useEffect, useRef, WheelEvent} from 'react';
export const useProxyWheel = (ref: HTMLElement | null) => {
    const canDeltaUp = useRef(false);
    const canDeltaDown = useRef(false);
    const onWheelCapture = (e: WheelEvent<HTMLElement>) => {
        if (e.deltaY > 0 && !canDeltaDown.current) {
            e.stopPropagation();
        }
        if (e.deltaY < 0 && !canDeltaUp.current) {
            e.stopPropagation();
        }
    };
    const calculate = (target: HTMLElement) => {
        if (target.scrollTop + target.clientHeight >= target.scrollHeight - 2) {
            canDeltaDown.current = true;
            canDeltaUp.current = false;
        } else if (target.scrollTop === 0) {
            canDeltaDown.current = false;
            canDeltaUp.current = true;
        } else {
            canDeltaDown.current = false;
            canDeltaUp.current = false;
        }
    };

    const onScroll = (e: UIEvent<HTMLElement>) => {
        calculate(e.target as HTMLElement);
    };

    const resizeObserver = useRef<ResizeObserver>(
        new ResizeObserver(() => {
            ref && calculate(ref);
        })
    );
    const cleanupFn = useCallback(() => {
        ref && resizeObserver.current.unobserve(ref);
    }, [ref]);

    useEffect(() => {
        if (ref) {
            calculate(ref);
            resizeObserver.current.observe(ref);
        }
        return cleanupFn;
    }, [cleanupFn, ref]);

    return {
        onWheelCapture,
        onScroll,
    };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant