update
This commit is contained in:
@@ -131,16 +131,11 @@ const scrollToSelected = async (index: number) => { // Accept index as argument
|
|||||||
const selectedItem = listElement.children[index] as HTMLLIElement;
|
const selectedItem = listElement.children[index] as HTMLLIElement;
|
||||||
|
|
||||||
if (selectedItem) {
|
if (selectedItem) {
|
||||||
const listRect = listElement.getBoundingClientRect();
|
// 使用 scrollIntoView 使元素可见,滚动最小距离
|
||||||
const itemRect = selectedItem.getBoundingClientRect();
|
selectedItem.scrollIntoView({
|
||||||
|
behavior: 'smooth', // 可以使用 'auto' 来实现即时滚动
|
||||||
if (itemRect.top < listRect.top) {
|
block: 'nearest',
|
||||||
// Item is above the visible area
|
});
|
||||||
listElement.scrollTop -= listRect.top - itemRect.top;
|
|
||||||
} else if (itemRect.bottom > listRect.bottom) {
|
|
||||||
// Item is below the visible area
|
|
||||||
listElement.scrollTop += itemRect.bottom - listRect.bottom;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -149,18 +149,11 @@ const scrollToSelected = async (index: number) => { // Accept index as argument
|
|||||||
const selectedItem = listElement.children[index] as HTMLLIElement;
|
const selectedItem = listElement.children[index] as HTMLLIElement;
|
||||||
|
|
||||||
if (selectedItem) {
|
if (selectedItem) {
|
||||||
const listRect = listElement.getBoundingClientRect();
|
// 使用 scrollIntoView 使元素可见,滚动最小距离
|
||||||
const itemRect = selectedItem.getBoundingClientRect();
|
selectedItem.scrollIntoView({
|
||||||
|
behavior: 'smooth', // 可以使用 'auto' 来实现即时滚动
|
||||||
if (itemRect.top < listRect.top) {
|
block: 'nearest',
|
||||||
// Item is above the visible area
|
});
|
||||||
listElement.scrollTop -= listRect.top - itemRect.top;
|
|
||||||
} else if (itemRect.bottom > listRect.bottom) {
|
|
||||||
// Item is below the visible area
|
|
||||||
listElement.scrollTop += itemRect.bottom - listRect.bottom;
|
|
||||||
}
|
|
||||||
// For smooth scrolling (optional):
|
|
||||||
// selectedItem.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user