react将选中本文自动滑动到容器可视区域内

    // 自动滚动到可视区域内
    useEffect(() => {
   
        const target = ref;
        const wrapper = wrapperRef?.current;
        if (target && wrapperRef) {
   
            const rect = target.getBoundingClientRect();
            const wrapperRect = wrapper.getBoundingClientRect();
            const isVisible = rect.bottom <= wrapperRect.bottom && rect.top >= wrapperRect.top;
            if (!isVisible) {
   
                wrapper.scrollTo({
    top: rect.top, behavior: 'smooth' });
            }
            console.log('isVisible', isVisible);
        }
    }, [avaliableIndex]);

 <div className={ve.defaultWrapper} ref={wrapperRef}>
            {content.map((text, index) => (
                <p
        
                    ref={(r) => {
                        if (isSelectedRow(index)) {
                            ref = r;
                        }
                    }}
                ></p>)}
                </div>
              

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-05 06:18:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-05 06:18:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-05 06:18:02       82 阅读
  4. Python语言-面向对象

    2024-02-05 06:18:02       91 阅读

热门阅读

  1. centos7安装google chrome和chromium

    2024-02-05 06:18:02       50 阅读
  2. Pinia的使用与原理

    2024-02-05 06:18:02       48 阅读
  3. CentOS磁盘扩容

    2024-02-05 06:18:02       58 阅读
  4. shell脚本初始化mysql密码

    2024-02-05 06:18:02       51 阅读
  5. 开源软件,推动技术创新

    2024-02-05 06:18:02       55 阅读
  6. Redis实现:每个进程每30秒执行一次任务

    2024-02-05 06:18:02       54 阅读
  7. 2024 极术通讯-安谋科技2023精彩征程回顾

    2024-02-05 06:18:02       49 阅读
  8. 负载均衡SLB

    2024-02-05 06:18:02       41 阅读