提交 8ffb4954 编写于 作者: H hdx

swiper-list: 适配微信

上级 e16dcf5e
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
<view class="swiper-list"> <view class="swiper-list">
<scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false"> <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
<view class="flex-row"> <view class="flex-row">
<text ref="swipertab" space="nbsp" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''" <text ref="swipertab" space="nbsp" class="swiper-tabs-item"
v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)"> :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''" v-for="(item, index) in swiperList" :key="index"
@click="onTabClick(index)">
{{item.title}} {{item.title}}
</text> </text>
</view> </view>
...@@ -63,11 +64,14 @@ ...@@ -63,11 +64,14 @@
} }
}, },
onReady() { onReady() {
this.tabScrollView = this.$refs['tabScroll'] as UniElement this.tabScrollView = this.$refs['tabScroll'] as UniElement;
this.indicatorNode = this.$refs['indicator'] as UniElement this.indicatorNode = this.$refs['indicator'] as UniElement;
this.swiperWidth = (this.$refs["swiper"] as UniElement).getBoundingClientRect().width (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync().then((res : DOMRect) : Promise<void> => {
this.cacheTabItemsSize() this.swiperWidth = res.width
return this.cacheTabItemsSize()
}).then(() => {
this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1) this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
});
}, },
methods: { methods: {
/** /**
...@@ -119,15 +123,17 @@ ...@@ -119,15 +123,17 @@
/** /**
* 缓存所有页签的左边距和宽度,用于计算指示线在移动过程中的线性变化. * 缓存所有页签的左边距和宽度,用于计算指示线在移动过程中的线性变化.
*/ */
cacheTabItemsSize() { async cacheTabItemsSize() {
this.swiperTabsRect.length = 0; this.swiperTabsRect.length = 0;
const tabs = this.$refs["swipertab"] as UniElement[] const tabs = this.$refs["swipertab"] as UniElement[]
tabs.forEach((node) => { for (let i = 0; i < tabs.length; i++) {
const element = tabs[i];
const rect = await element.getBoundingClientRectAsync()
this.swiperTabsRect.push({ this.swiperTabsRect.push({
x: node.offsetLeft, x: rect.left,
w: node.offsetWidth w: rect.width
} as SwiperTabsItem) } as SwiperTabsItem)
}) }
}, },
/** /**
* 设置当前 swiper 选中的索引值. * 设置当前 swiper 选中的索引值.
...@@ -162,7 +168,7 @@ ...@@ -162,7 +168,7 @@
const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage) const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
// 通过 transform 更新指示线,避免重排版 // 通过 transform 更新指示线,避免重排版
// #ifdef APP // #ifdef APP || MP
const x = indicator_line_x + indicator_line_w / 2 const x = indicator_line_x + indicator_line_w / 2
this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`) this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
// #endif // #endif
...@@ -175,7 +181,15 @@ ...@@ -175,7 +181,15 @@
// 滚动到水平中心位置 // 滚动到水平中心位置
const scroll_x = x - this.swiperWidth / 2 const scroll_x = x - this.swiperWidth / 2
// app 平台后续支持 scrollTo()
// #ifndef MP-WEIXIN
this.tabScrollView!.scrollLeft = scroll_x this.tabScrollView!.scrollLeft = scroll_x
// #endif
// #ifdef MP-WEIXIN
this.tabScrollView!.scrollTo({
left: scroll_x
})
// #endif
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册