|
|
@@ -47,17 +47,20 @@ Component({
|
|
|
setActive() {
|
|
|
const that = this,
|
|
|
active = this.data.active,
|
|
|
- query = wx.createSelectorQuery().in(this)
|
|
|
- query.select('#active' + active).boundingClientRect(function (res) {
|
|
|
- let index = res.dataset.index,
|
|
|
- scrollLeft = res.left;
|
|
|
- if (index >= 6) scrollLeft += ((index - 4) * res.width);
|
|
|
- if (that.data.scrollLeft > scrollLeft && index >= 4) scrollLeft += (2 * res.width)
|
|
|
+ query = wx.createSelectorQuery().in(this);
|
|
|
+
|
|
|
+ query.select('#active' + active).boundingClientRect((res) => {
|
|
|
+ if (!res) return; // 防止未找到元素时报错
|
|
|
+ const tabWidth = res.width; // 获取 tab 的宽度
|
|
|
+ const screenWidth = wx.getSystemInfoSync().windowWidth; // 获取屏幕宽度(px)
|
|
|
+ const scrollLeft = Math.max(0, res.left + that.data.scrollLeft - (screenWidth / 2) + (tabWidth / 2)); // 居中计算并确保不小于 0
|
|
|
+
|
|
|
that.setData({
|
|
|
scrollLeft
|
|
|
- })
|
|
|
+ });
|
|
|
}).exec();
|
|
|
- this.triggerEvent("onChenge", active)
|
|
|
+
|
|
|
+ this.triggerEvent("onChenge", active);
|
|
|
},
|
|
|
myTouchStart(e) {
|
|
|
return;
|