From 42b825c6962a7a3dbfd1a1db3fccfcdcce6b74e1 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Wed, 11 Dec 2024 13:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=99=9A=E6=8B=9F=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E7=BB=84=E4=BB=B6=E7=9A=84=E7=82=B9=E5=87=BB=E5=92=8C?= =?UTF-8?q?=E9=94=AE=E7=9B=98=E5=AF=BC=E8=88=AA=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=9C=A8=E5=88=97=E8=A1=A8=E8=A2=AB=E5=86=BB?= =?UTF-8?q?=E7=BB=93=E6=97=B6=E4=B8=8D=E5=93=8D=E5=BA=94=E7=82=B9=E5=87=BB?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E5=A4=84=E7=90=86=E8=BE=B9=E7=95=8C=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E4=BB=A5=E9=81=BF=E5=85=8D=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E8=A1=8C=E4=B8=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/VirtualList.vue | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/components/VirtualList.vue b/src/components/VirtualList.vue index cc0a12e..f6bea0e 100644 --- a/src/components/VirtualList.vue +++ b/src/components/VirtualList.vue @@ -93,7 +93,7 @@ const handleSelect = (item) => { // 点击处理 const handleClick = (item) => { - if (props.frozen) return // 如果列表被冻��,不响应点击 + if (props.frozen) return // 如果列表被冻结,不响应点击 handleSelect(item) // 点击时同时触发选中 emit('click', item) } @@ -155,21 +155,28 @@ function handleKeyDown(e) { } else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { e.preventDefault() + const isFirstItem = selectedIndex.value === 0 + const isLastItem = selectedIndex.value === props.data.length - 1 + + // 如果已经在第一项还按上键,或在最后一项还按下键,则不处理 + if ((e.key === 'ArrowUp' && isFirstItem) || + (e.key === 'ArrowDown' && isLastItem)) { + return + } + isKeyboardNavigating.value = true if (keyboardTimer) clearTimeout(keyboardTimer) - const containerTop = containerRef.value.scrollTop - const containerHeight = containerRef.value.clientHeight + const containerTop = containerRef.value?.scrollTop || 0 + const containerHeight = containerRef.value?.clientHeight || 0 if (e.key === 'ArrowUp') { const nextIndex = Math.max(0, selectedIndex.value - 1) selectedIndex.value = nextIndex emit('select', { ...props.data[nextIndex], index: nextIndex }) - // 使用与向下相同的逻辑,计算项目顶部位置 const itemTop = nextIndex * totalItemHeight.value - - if (itemTop < containerTop) { + if (itemTop < containerTop && containerRef.value) { containerRef.value.scrollTop = itemTop } } else { @@ -177,11 +184,9 @@ function handleKeyDown(e) { selectedIndex.value = nextIndex emit('select', { ...props.data[nextIndex], index: nextIndex }) - // 计算项目底部位置 const itemBottom = (nextIndex + 1) * totalItemHeight.value const scrollBottom = containerTop + containerHeight - - if (itemBottom > scrollBottom) { + if (itemBottom > scrollBottom && containerRef.value) { containerRef.value.scrollTop = itemBottom - containerHeight } }