提交 4716e0c2 编写于 作者: H hdx

slider-in-swiper: 适配微信

上级 101fc0c5
...@@ -83,11 +83,14 @@ ...@@ -83,11 +83,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
this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1) return this.cacheTabItemsSize()
}).then(() => {
this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
});
}, },
methods: { methods: {
/** /**
...@@ -139,15 +142,17 @@ ...@@ -139,15 +142,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++) {
this.swiperTabsRect.push({ const element = tabs[i];
x: node.offsetLeft, const rect = await element.getBoundingClientRectAsync()!
w: node.offsetWidth this.swiperTabsRect.push({
} as SwiperTabsItem) x: rect.left,
}) w: rect.width
} as SwiperTabsItem)
}
}, },
/** /**
* 设置当前 swiper 选中的索引值. * 设置当前 swiper 选中的索引值.
...@@ -186,7 +191,7 @@ ...@@ -186,7 +191,7 @@
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
// #ifdef WEB // #ifdef WEB || MP
// TODO chrome windows系统 transform scaleX渲染bug // TODO chrome windows系统 transform scaleX渲染bug
const x = indicator_line_x const x = indicator_line_x
this.indicatorNode?.style?.setProperty('width', `${indicator_line_w}px`) this.indicatorNode?.style?.setProperty('width', `${indicator_line_w}px`)
...@@ -194,8 +199,16 @@ ...@@ -194,8 +199,16 @@
// #endif // #endif
// 滚动到水平中心位置 // 滚动到水平中心位置
const scroll_x = x - this.swiperWidth / 2 const scroll_x = x - this.swiperWidth / 2
this.tabScrollView!.scrollLeft = scroll_x // app 平台后续支持 scrollTo()
// #ifndef MP-WEIXIN
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.
先完成此消息的编辑!
想要评论请 注册