[英]How to get an element in center of list at scroll event in React?
这是一个使用 api 的演示- elementFromPoint
export default function App() {
const refList = useRef();
function onScroll({ target }) {
// get the computed dimension of the list
const { height, width, x, y } = target.getBoundingClientRect();
// adding x and y as offsets. You may skip this
const x1 = (width + x) / 2;
const y1 = (height + y) / 2;
console.log(document.elementFromPoint(x1, y1)); // prints the element from given point
}
return (
<div className="App">
<ul
ref={refList}
onScroll={onScroll}
>
{NUMBERS.map((i) => (
<li key={i}>{i}</li>
))}
</ul>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.