marquee.js
2.7 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// components/marquee/marquee.js
let timer = null
import util from '../../utils/util.js'
Component({
/**
* 组件的属性列表
*/
properties: {
data: {
type: String,
value: '测试公告'
},
delay: {
type: Number,
value: 1000
},
fontSize: {
type: Number,
value: 12
},
speed: {
type: Number,
value: 5000
}
},
/**
* 组件的初始数据
*/
data: {
flag: true,
},
/**
* 组件的方法列表
*/
methods: {
/**
* 获取宽度
*/
getWidth() {
let boxWidth = 0
let valueWidth = 0
util.getViewInfo('#marquee', res => {
boxWidth = res.width
this.setData({
boxWidth
})
util.getViewInfo('#content', contentInfo => {
valueWidth = contentInfo.width
if (boxWidth - 20 <= valueWidth) {
this.setData({
valueWidth
})
this.run()
}
}, this)
}, this)
},
run() {
clearTimeout(timer)
util.promise(resolve => {
let {
speed,
delay,
valueWidth
} = this.data
let animation = wx.createAnimation({
duration: speed,
timingFunction: 'linear',
delay
})
animation.left(`-${valueWidth}px`).step()
this.setData({
animation: animation.export()
})
let time = Number(delay) + Number(speed) + 1000
timer = setTimeout(() => {
resolve()
}, time)
}).then(() => {
this.setData({
flag: false
})
this.again()
})
},
again() {
clearTimeout(timer)
util.promise(resolve => {
let {
boxWidth
} = this.data
let animation = wx.createAnimation({
duration: 1,
timingFunction: 'linear'
})
animation.left(`${boxWidth}px`).step()
this.setData({
animation: animation.export()
})
timer = setTimeout(() => {
resolve()
}, 500)
}).then(() => {
this.setData({
flag: true
})
this.goZero()
})
},
goZero() {
clearTimeout(timer)
util.promise(resolve => {
let {speed} = this.data
let animation = wx.createAnimation({
duration: speed,
timingFunction: 'linear',
})
animation.left('0%').step()
this.setData({
animation: animation.export()
})
timer = setTimeout(() => {
resolve()
}, Number(speed) + 5000)
}).then(() => {
this.run()
})
}
},
ready(){
this.getWidth()
}
})