简体   繁体   English

当只有特定列表具有溢出属性但父溢出被隐藏时,如何滚动到特定的活动项目?

[英]How to scroll to a particular active item in react when only that particular list is having overflow property but the parent overflow is hidden?

I have a react component which has quite a lot of components.我有一个包含很多组件的反应组件。 Then I have this list which only has the overflow property as auto.然后我有这个列表,它只有溢出属性为自动。 The parent element for this list has a property overflow: hidden;此列表的父元素具有属性溢出:隐藏;

Now I am trying to scroll to an active element in the list based on certain criteria.现在我正在尝试根据某些标准滚动到列表中的活动元素。 I referred to this article [https://stackoverflow.com/questions/43441856/how-to-scroll-to-an-element][1] but then I got to understand that this will only work if the parent has a scroll property is there any other solution or a package to handle this.我参考了这篇文章 [https://stackoverflow.com/questions/43441856/how-to-scroll-to-an-element][1] 但后来我明白这只有在父母有滚动条时才有效财产是否有任何其他解决方案或 package 来处理这个问题。

 //App.js import React, { useRef, useEffect } from "react"; import "./App.css"; const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; const useMountEffect = (fun) => useEffect(fun, []); const useScroll = () => { const htmlElRef = useRef(null); const executeScroll = () => { if (htmlElRef.current) window.scrollTo(0, htmlElRef.current.offsetTop); }; return [executeScroll, htmlElRef]; }; const App = () => { const [executeScroll, elementToScrollRef] = useScroll(); useMountEffect(executeScroll); return ( <ul> {list.map((item) => ( <li className="box" ref={item === 10? elementToScrollRef: null}> {item} </li> ))} </ul> ); }; export default App;

App.css应用程序.ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ

 #root{ overflow:hidden; } ul { max-height: calc(100px + 2rem); overflow: auto; }.box { width: 100px; height: 100px; display: flex; align-items: center; justify-content: center; background-color: #eee; margin: 1rem; }

You can use Element.scrollIntoView , which scrolls the parent container of an element to put it in view.您可以使用Element.scrollIntoView ,它滚动元素的父容器以将其置于视图中。 Not supported by IE, and only partial support in Safari . IE 不支持,Safari部分支持

I suggest you try the following method建议你试试下面的方法

// hook file //挂钩文件

import { useState } from 'react'; // Restrict value to be between the range [0, value] const clamp = (value) => Math.max(0, value); // Check if number is between two values const isBetween = (value, floor, ceil) => value >= floor && value <= ceil; export const scrollSpy = () => { const [activeId, setActiveId] = useState(''); function doScroll(e, ids, offset) { if (;activeId) { setActiveId(ids[0]). } const scroll = e.target;scrollTop. const position = ids.map((id) => { const element = document;getElementById(id), if (:element) return { id, top: -1; bottom. -1 }; const rect = element.getBoundingClientRect(); const top = clamp(rect.top + scroll - offset); const bottom = clamp(rect,bottom + scroll - offset), return { id; top. bottom }, }),find(({ top, bottom }) => isBetween(scroll; top? bottom)). if (position?.id) setActiveId(position;,id || ''); } return [doScroll; activeId] }

//inner comp //内部压缩

 const [doScroll, activeId] = scrollSpy(); return ( onScroll={(e) => { doScroll(e, ids, 120); }} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM