TabBar.js
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// components/TabBar/TabBar.js
Component({
/**
* 组件的属性列表
*/
properties: {
tabList: Array,
activeIndex: {
type: Number,
observer: function (n) {
console.log(this.data.tabIndex, 'activeIndex', n);
console.log(this.data.tabIndex != n);
if (this.data.tabIndex != n) {
this.setData({
tabIndex: this.data.activeIndex
})
this.setTab(this.data.tabIndex)
}
}
},
scroll: Boolean,
notInitialLoading: Boolean
},
/**
* 组件的初始数据
*/
data: {
tabIndex: 0
},
lifetimes: {
ready() {
// console.log('lifetimes',this.data.tabIndex);
// console.log('lifetimes',this.data.activeIndex);
this.setTab(this.data.tabIndex, this.data.notInitialLoading)
let that = this
wx.$eventbus.on('page_show', n => {
!that.data.width && that.setTab(that.data.tabIndex)
})
}
},
/**
* 组件的方法列表
*/
methods: {
change(e) {
let {
index
} = e.currentTarget.dataset
this.setTab(index)
},
setTab(index = 0, notInitialLoading) {
let {
tabIndex,
} = this.data
wx.$api.getViewInfo(`#tab-${tabIndex}`, old => {
let oldLeft = old?.[0]?.left
let oldWidth = old?.width
wx.$api.getViewInfo(`#tab-${index}`, res => {
let left = res?.[0]?.left
let width = res?.[0]?.width
if (tabIndex != index) {
let readyLeft = oldLeft > left ? left : oldLeft
let readyWidth = oldLeft > left ? oldLeft - left : left - oldLeft
this.setData({
activeStyle: `width: 64rpx;background-color:#347BFF;border-radius:3rpx;left:${readyLeft + readyWidth / 2}px;transition: all .1s;`
})
}
setTimeout(() => {
this.setData({
activeStyle: `width: 64rpx;left:${left + (width - 32) / 2}px;background-color:#347BFF;`,
tabIndex: index,
to: `to-${index}`,
width
})
if (!notInitialLoading) this.triggerEvent('click', index)
}, 100)
}, this)
}, this)
}
}
})