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

js 复制代码
    // 自动滚动到可视区域内
    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]);
html 复制代码
 <div className={ve.defaultWrapper} ref={wrapperRef}>
            {content.map((text, index) => (
                <p
        
                    ref={(r) => {
                        if (isSelectedRow(index)) {
                            ref = r;
                        }
                    }}
                ></p>)}
                </div>
              
相关推荐
林恒smileZAZ4 小时前
Vue<前端页面版本检测>
前端·javascript·vue.js
码事漫谈7 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
许杰小刀8 小时前
ctfshow-web文件包含(web78-web86)
android·前端·android studio
我是Superman丶8 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭8 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
xiaokuangren_9 小时前
前端css颜色
前端·css
Huanzhi_Lin9 小时前
关于V8/MajorGC/MinorGC——性能优化
javascript·性能优化·ts·js·v8·新生代·老生代
hoiii1879 小时前
C# 基于 LumiSoft 实现 SIP 客户端方案
前端·c#
anOnion9 小时前
构建无障碍组件之Meter Pattern
前端·html·交互设计
小码哥_常10 小时前
Spring Boot配置diff:解锁配置管理新姿势
前端