From 9a234979643fa8ec202e8314a7c47b5132e3113d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BF=97=E5=8B=87?= Date: Sun, 8 Sep 2024 20:28:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BA=B5=E5=90=91?= =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=9A=84=E4=B8=8B=E6=A0=87=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../slide/SlideVerticalInfinite.vue | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/components/slide/SlideVerticalInfinite.vue b/src/components/slide/SlideVerticalInfinite.vue index d1ec763..f2551ce 100644 --- a/src/components/slide/SlideVerticalInfinite.vue +++ b/src/components/slide/SlideVerticalInfinite.vue @@ -79,8 +79,8 @@ watch( () => props.list, (newVal, oldVal) => { // console.log('watch-list', newVal.length, oldVal.length, newVal) - //新数据长度比老数据长度小,说明是刷新 - if (newVal.length < oldVal.length) { + //新数据长度小于老数据长度,说明是刷新 + if (newVal.length <= oldVal.length) { insertContent() } else { //没数据就直接插入 @@ -112,10 +112,45 @@ watch( watch( () => props.index, - (newVal, oldVal) => { + (newVal) => { state.localIndex = newVal // console.log('watch-index', newVal, oldVal) - if (!props.list.length) return + if (!props?.list?.length) return + // 父组件强行改变指定可见节点,比如合集中跳转到指定集等 + if ( + slideListEl.value && + slideListEl.value?.innerHTML && + state.localIndex < props?.list?.length + ) { + let startIndex = slideListEl.value + .querySelector(`.${itemClassName}:first-child`) + .getAttribute('data-index') + + let endIndex = slideListEl.value + .querySelector(`.${itemClassName}:last-child`) + .getAttribute('data-index') + + if ( + state.localIndex >= (startIndex as any) * 1 && + state.localIndex <= (endIndex as any) * 1 + ) { + // 在可见范围内 + + touchEnd({}) + } else { + // 不在可见范围内 + insertContent() + } + } + } +) + +/** + * 滑动 + */ +watch( + () => state.localIndex, + (newVal, oldVal) => { bus.emit(EVENT_KEY.CURRENT_ITEM, props.list[newVal]) bus.emit(EVENT_KEY.SINGLE_CLICK_BROADCAST, { uniqueId: props.uniqueId, @@ -131,7 +166,6 @@ watch( }, 200) } ) - watch( () => props.active, (newVal) => { @@ -166,7 +200,7 @@ function insertContent() { if (!props.list.length) return //清空SlideList slideListEl.value.innerHTML = '' - let half = (props.virtualTotal - 1) / 2 + let half = parseInt((props.virtualTotal / 2).toString()) //虚拟列表的一半 //因为我们只渲染 props.virtualTotal 条数据到dom中,并且当前index有可能不是0,所以需要计算出起始下标和结束下标 let start = 0 if (state.localIndex > half) { @@ -272,7 +306,7 @@ function touchEnd(e) { emit('refresh') } slideTouchEnd(e, state, canNext, (isNext) => { - let half = (props.virtualTotal - 1) / 2 + let half = parseInt((props.virtualTotal / 2).toString()) //虚拟列表的一半 if (props.list.length > props.virtualTotal) { //手指往上滑(即列表展示下一条内容) if (isNext) {