Skip to content

Commit 488d844

Browse files
committed
refactor(NoticeBar): 重构内部逻辑
使用 NodeUtil 重构查询节点信息的逻辑,减少嵌套
1 parent 7f75053 commit 488d844

1 file changed

Lines changed: 22 additions & 24 deletions

File tree

src/notice-bar/index.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import nodeUtil from '../core/utils/node-util';
12
Component({
2-
externalClasses: ['l-class','l-icon-class'],
3+
externalClasses: ['l-class', 'l-icon-class'],
34

45
properties: {
56
type: {
@@ -77,25 +78,22 @@ Component({
7778
},
7879

7980
methods: {
80-
initAnimation() {
81-
wx.createSelectorQuery().in(this).select('.l-noticebar-content-wrap').boundingClientRect((wrapRect) => {
82-
wx.createSelectorQuery().in(this).select('.l-noticebar-content').boundingClientRect((rect) => {
83-
const duration = rect.width / 40 * this.data.speed;
84-
const animation = wx.createAnimation({
85-
duration: duration,
86-
timingFunction: 'linear',
87-
});
88-
this.setData({
89-
wrapWidth: wrapRect.width,
90-
width: rect.width,
91-
duration: duration,
92-
animation: animation
93-
}, () => {
94-
this.startAnimation();
95-
});
96-
}).exec();
97-
98-
}).exec();
81+
async initAnimation() {
82+
const rect = await nodeUtil.getNodeRectFromComponent(this, '.l-noticebar-content');
83+
const wrapRect = await nodeUtil.getNodeRectFromComponent(this, '.l-noticebar-content-wrap');
84+
const duration = rect.width / 40 * this.data.speed;
85+
const animation = wx.createAnimation({
86+
duration: duration,
87+
timingFunction: 'linear',
88+
});
89+
this.setData({
90+
wrapWidth: wrapRect.width,
91+
width: rect.width,
92+
duration: duration,
93+
animation: animation
94+
}, () => {
95+
this.startAnimation();
96+
});
9997
},
10098
startAnimation() {
10199
//reset
@@ -126,18 +124,18 @@ Component({
126124
}
127125
},
128126
handleTap() {
129-
this.triggerEvent('lintap',{},{ bubbles: true, composed: true });
127+
this.triggerEvent('lintap', {}, { bubbles: true, composed: true });
130128
this.setData({
131129
timer: null
132130
});
133131
},
134132
onSwip(e) {
135133
this.triggerEvent('lintap', {
136134
...e.currentTarget.dataset
137-
},{ bubbles: true, composed: true });
135+
}, { bubbles: true, composed: true });
138136
},
139-
onIconTap(){
140-
this.triggerEvent('linicontap',{},{ bubbles: true, composed: true });
137+
onIconTap() {
138+
this.triggerEvent('linicontap', {}, { bubbles: true, composed: true });
141139
this.setData({
142140
timer: null
143141
});

0 commit comments

Comments
 (0)