From 8c7ac2b5a342f69d66416257a5d21455ca803323 Mon Sep 17 00:00:00 2001 From: hdx Date: Tue, 29 Aug 2023 17:00:48 +0800 Subject: [PATCH] =?UTF-8?q?swiper-list:=20=E4=BC=98=E5=8C=96tab=E5=B8=83?= =?UTF-8?q?=E5=B1=80=EF=BC=9B=E8=B0=83=E6=95=B4=E8=9B=87=E5=BD=A2=E7=BA=BF?= =?UTF-8?q?=E7=AE=97=E6=B3=95,=E5=90=8C=E6=97=B6=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1webview=E5=92=8Cskyline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/template/swiper-list/swiper-list.uvue | 89 +++++++++------------ 1 file changed, 36 insertions(+), 53 deletions(-) diff --git a/pages/template/swiper-list/swiper-list.uvue b/pages/template/swiper-list/swiper-list.uvue index e4f8e485..9babf4e2 100644 --- a/pages/template/swiper-list/swiper-list.uvue +++ b/pages/template/swiper-list/swiper-list.uvue @@ -3,11 +3,10 @@ - - {{item.title}} - + + {{item.title}} + - + {{index}} @@ -31,7 +30,7 @@ } type SwiperViewItem = { - title : string + title : string, } export default { @@ -42,53 +41,49 @@ tabsScrollLeft: 0, swiperIndicatorLineLeft: 0, swiperIndicatorLineWidth: 0, - $lastSwiperIndex: 0, + $animationFinishIndex: 0, $swiperWidth: 0, - $swiperTabsRect: [] as SwiperTabsItem[], - $isTap: false + $swiperTabsRect: [] as SwiperTabsItem[] } }, onLoad() { for (let i = 0; i < 8; i++) { + const space = " ".repeat(i) this.swiperList.push({ - title: "TAB " + i + title: "Tab " + space + i } as SwiperViewItem) } }, onReady() { - this.$swiperWidth = (this.$refs["swiper"]! as INode)!?.offsetWidth! as number; - this.queryTabItemsSize(); - this.setSwiperIndex(0, true); + this.$swiperWidth = (this.$refs["swiper"] as INode)!.offsetWidth! as number + this.queryTabItemsSize() + this.setSwiperIndex(0, true) }, methods: { onTabClick(index : number) { - this.setSwiperIndex(index, false); - }, - onSwiperChange(e : SwiperChangeEvent) { - this.setSwiperIndex(e.detail.current, false); - console.log("onSwiperChange", e.detail.current); + this.setSwiperIndex(index, false) }, onSwiperTransition(e : SwiperTransitionEvent) { - const offsetX = e.detail.dx; + const offsetX = e.detail.dx + // 兼容微信 skyline 和 webview + const offsetIndex = Math.abs(offsetX.toInt()) == this.$swiperWidth ? 0 : offsetX / this.$swiperWidth + const currentIndex = this.$animationFinishIndex + Math.abs(offsetIndex.toInt()) - let moveToIndex = offsetX > 0 ? this.$lastSwiperIndex + 1 : this.$lastSwiperIndex - 1 + let moveToIndex = offsetX > 0 ? currentIndex + 1 : currentIndex - 1 if (moveToIndex < 0) { moveToIndex = 0 } if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 } - const percentage = Math.abs(offsetX) / this.$swiperWidth; - const currentSize = this.$swiperTabsRect[this.$lastSwiperIndex]; - const moveToSize = this.$swiperTabsRect[moveToIndex]; - const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage; - const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage; + const percentage = Math.abs(offsetX) / this.$swiperWidth + const currentSize = this.$swiperTabsRect[currentIndex] + const moveToSize = this.$swiperTabsRect[moveToIndex] + const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage + const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage - this.updateTabIndicator(indicatorlineL, indicatorlineW); - //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage); + this.updateTabIndicator(indicatorlineL, indicatorlineW) }, - // TODO 事件触发时机不对 onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) { - this.$lastSwiperIndex = e.detail.current; - console.log("onSwiperAnimationfinish", e.detail.current); - // this.setSwiperIndex(e.detail.current, true); + this.setSwiperIndex(e.detail.current, true) + this.$animationFinishIndex = e.detail.current }, queryTabItemsSize() { this.$swiperTabsRect.length = 0; @@ -102,21 +97,18 @@ }, setSwiperIndex(index : Number, updateIndicator : Boolean) { if (this.swiperIndex === index) { - return; + return } - this.swiperIndex = index; + this.swiperIndex = index if (updateIndicator) { - this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width); + this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width) } }, updateTabIndicator(left : Number, width : Number) { - this.swiperIndicatorLineLeft = left; - this.swiperIndicatorLineWidth = width; - const offset = left + width / 2; - if (offset > this.$swiperWidth / 2) { - this.tabsScrollLeft = offset - this.$swiperWidth / 2; - } + this.swiperIndicatorLineLeft = left + this.swiperIndicatorLineWidth = width + this.tabsScrollLeft = left + width / 2 - this.$swiperWidth / 2 } } } @@ -136,15 +128,12 @@ } .swiper-tabs-item { - padding: 12px 25px; - } - - .swiper-tabs-item-text { color: #555; font-size: 16px; + padding: 12px 25px; } - .swiper-tabs-item-text-active { + .swiper-tabs-item-active { color: #007AFF; } @@ -161,11 +150,6 @@ background-color: #007AFF; } - .swiper-tabs-animation { - transition-duration: 0.2s; - transition-property: left; - } - .swiper-view { flex: 1; } @@ -179,6 +163,5 @@ .swiper-item-text { font-size: 72px; font-weight: bold; - margin: auto; } \ No newline at end of file -- GitLab