gpt4 book ai didi

javascript - 确保打开的下拉菜单在视口(viewport)中可见

转载 作者:行者123 更新时间:2023-12-03 00:20:24 25 4
gpt4 key购买 nike

我有一个 react 组件,它是一个下拉列表。每次打开下拉菜单时,我都需要检查整个下拉菜单是否在视口(viewport)/可见中(特别是对于移动设备)如果下拉菜单被视口(viewport) chop ,则需要滚动,以便整个下拉菜单可见(话虽如此,dorpdown 的高度会有所不同)。

componentDidUpdate(prevProps, prevState, snapshot) {


if (prevState.isOpen === false && this.state.isOpen === true) {
if (this.node) {
const optionContainer = this.node.querySelector(
'.price-dropdown-options'
);

const recNode = this.node.getBoundingClientRect();
const recOption = optionContainer.getBoundingClientRect();


if ([some logic]) {
//window.scrollBy(0, ?????);
}
}
}
}

PS:this.node是下拉按钮的引用。选项部分也是绝对定位的

最佳答案

我相信有一个名为 scrollIntoView() 的内置方法。 .

应该是这样的:

dropDown.scrollIntoView();

关于javascript - 确保打开的下拉菜单在视口(viewport)中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338544/

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