glTab.js
1.61 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
// components/glTab/glTab.js
import util from '../../utils/util.js'
Component({
/**
* 组件的属性列表
*/
properties: {
tabList: {
type: Array,
value: []
},
activeColor:{
type:String,
value:'#05BAA0'
},
activeDotColor:{
type:String,
value:'#05BAA0'
}
},
/**
* 组件的初始数据
*/
data: {
tabIndex:0
},
/**
* 组件的方法列表
*/
methods: {
change(e) {
let {
index
} = e.currentTarget.dataset
this.setTab(index)
this.triggerEvent('change',index)
},
setTab(index = 0) {
let {
tabIndex,
activeDotColor
} = this.data
util.getViewInfo(`#tab-${tabIndex}`, old => {
let oldLeft = old.left
let oldWidth = old.width
util.getViewInfo(`#tab-${index}`, res => {
let {
left,
width
} = res
if (tabIndex != index) {
let readyLeft = oldLeft > left ? left : oldLeft
let readyWidth = oldLeft > left ? oldLeft - left : left - oldLeft
this.setData({
activeStyle: `width:${readyWidth - oldWidth / 2 + width / 2}px;background-color:${activeDotColor};border-radius:100%;left:${readyLeft + oldWidth / 2}px;transition: all .1s;`
})
}
setTimeout(() => {
this.setData({
activeStyle: `width:${width}px;left:${left}px;background-color:${activeDotColor};`,
tabIndex: index
})
}, 100)
},this)
},this)
},
},
ready(){
this.setTab()
}
})