javascript - 在溢出的 React-Select 多选输入中滚动到底部
问题描述
我正在使用 React-Select v2.4.4。我正在尝试各种方法来将滚动条放在多选输入的底部,但它们似乎都不起作用。
沙盒链接:https ://codesandbox.io/s/react-codesandboxer-example-89cry
在上面的链接中,当我们Select All
从下拉列表中选择选项时,滚动条应自动移动到底部并显示以下输入:
我尝试了 2 件事,但没有一个有效。
我尝试使用
Ref
以下代码:const scrollHeight = this.node.scrollHeight; const height = this.node.clientHeight; const maxScrollTop = scrollHeight - height; this.node.scrollTop = maxScrollTop > 0 ? maxScrollTop : 0;
通过 using 得到元素
document.getElementById()
,然后 usedscrollIntoView
。const element = document.getElementById("select"); element.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
上述两种方法都存在于scrollToBottom
函数中。
我有什么遗漏或做错了吗。
解决方案
我没有使用任何scrollHeight
or就解决了它document.getElementById
。
我确实通过了ref
to Select
。
ref={component => {
this.node = component;
}}
然后我过去常常this.node.blur()
把焦点移开,Input
然后再把this.node.focus()
焦点放在它上面。这样,当focus
被带回 时Input
,滚动将自动移至底部。
更新的代码在相同的代码沙箱中: https ://codesandbox.io/s/react-codesandboxer-example-89cry
您可以通过提供以下输入来尝试:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
然后按tab
或enter
。您会注意到滚动条位于Input
.
推荐阅读
- android-studio - 如何在 Android Studio 的包中添加新目录/文件夹
- express - 在 Express 中使用 supertest 时如何模拟 redis?
- linux - SECTION .DATA 中存在变量的分段错误
- php - 循环遍历 html 表并使用简单的 dom 解析器在 php 中获取 tr、th 和 td
- c# - 如何在 Asp.net C# 的另一个页面上显示 ViewDetail
- java - Java + spring - 在无限循环中以随机间隔运行动作
- java - apache Storm - 在单节点中正确运行 jar,但在多节点中不正确
- maven - 从 maven 构建 jar 后无法运行 spark-submit
- python - 使 matplotlib 图部分不可见
- r - 芝加哥犯罪数据集的线性回归可能吗?