Skip to content

Commit 8ed735e

Browse files
committed
fix(notice-bar): fixe inconsistent rolling rates
1 parent d25ded5 commit 8ed735e

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

packages/notice-bar/index.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ VantComponent({
6767
getRect(this, '.van-notice-bar__wrap'),
6868
]).then((rects) => {
6969
const [contentRect, wrapRect] = rects;
70-
const { speed, scrollable, delay } = this.data;
70+
const { scrollable } = this.data;
7171
if (
7272
contentRect == null ||
7373
wrapRect == null ||
@@ -79,24 +79,32 @@ VantComponent({
7979
}
8080

8181
if (scrollable || wrapRect.width < contentRect.width) {
82-
const duration =
83-
((wrapRect.width + contentRect.width) / speed) * 1000;
84-
85-
this.wrapWidth = wrapRect.width;
86-
this.contentWidth = contentRect.width;
87-
this.duration = duration;
88-
this.animation = wx.createAnimation({
89-
duration,
90-
timingFunction: 'linear',
91-
delay,
92-
});
82+
this.initAnimation(wrapRect.width, contentRect.width);
9383

9484
this.scroll(true);
9585
}
9686
});
9787
});
9888
},
9989

90+
initAnimation(warpWidth: number, contentWidth: number) {
91+
const { speed, delay } = this.data;
92+
93+
this.wrapWidth = warpWidth;
94+
this.contentWidth = contentWidth;
95+
96+
// begin 0
97+
this.contentDuration = (contentWidth / speed) * 1000;
98+
// begin -wrapWidth
99+
this.duration = ((warpWidth + contentWidth) / speed) * 1000;
100+
101+
this.animation = wx.createAnimation({
102+
duration: this.contentDuration,
103+
timingFunction: 'linear',
104+
delay,
105+
});
106+
},
107+
100108
scroll(isInit = false) {
101109
this.timer && clearTimeout(this.timer);
102110
this.timer = null;
@@ -108,18 +116,20 @@ VantComponent({
108116
.export(),
109117
});
110118

119+
const duration = isInit ? this.contentDuration : this.duration;
120+
111121
requestAnimationFrame(() => {
112122
this.setData({
113123
animationData: this.animation
114124
.translateX(-this.contentWidth)
115-
.step()
125+
.step({ duration })
116126
.export(),
117127
});
118128
});
119129

120130
this.timer = setTimeout(() => {
121131
this.scroll();
122-
}, this.duration + this.data.delay);
132+
}, duration + this.data.delay);
123133
},
124134

125135
onClickIcon(event) {

0 commit comments

Comments
 (0)