gpt4 book ai didi

javascript - react : How to use arrow up/down to select previous/next item in a scroll list and make the list scroll to keep the selected element on screen

转载 作者:行者123 更新时间:2023-11-28 15:23:16 24 4
gpt4 key购买 nike

我正在使用 react-infinite 滚动列表,我实现了一个向上/向下箭头处理程序:

onKeyDown = (e) => {
if(e.keyCode === 40){
if (this.props.onKeyDown) {
e.preventDefault()
this.props.onKeyDown()
}
}
else if (e.keyCode === 38) {
if (this.props.onKeyUp) {
e.preventDefault()
this.props.onKeyUp()
}
}
}

(该组件可在我的整个应用程序中重用,因此我通过 props 传递实际功能)

只要选择列表中的下一个/上一个元素,它就可以工作。但是,当您到达滚动列表的边缘并继续前进时,列表不会滚动以保持所选元素在 View 中。所选元素刚从屏幕上消失。

任何帮助将不胜感激!

最佳答案

您需要方法 scrollIntoView .它存在于每个元素上。您还需要通过获取元素的 offsetTop 并与组件的 offsetHeight 进行比较来检测元素何时在屏幕外呈现,当此数字高于组件的 offsetHeight 时 您需要将其滚动到 View 中。

关于javascript - react : How to use arrow up/down to select previous/next item in a scroll list and make the list scroll to keep the selected element on screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45695491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com