mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
[improvement] NoticeBar: 性能优化
This commit is contained in:
parent
baf1c91a75
commit
cc783fc86f
@ -30,10 +30,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
&__content-wrap {
|
&__content-wrap {
|
||||||
flex: 1;
|
position: relative;
|
||||||
height: 24px;
|
height: 24px;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
position: relative;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
&__content {
|
&__content {
|
||||||
|
@ -49,13 +49,7 @@ VantComponent({
|
|||||||
|
|
||||||
data: {
|
data: {
|
||||||
show: true,
|
show: true,
|
||||||
hasRightIcon: false,
|
hasRightIcon: false
|
||||||
width: undefined,
|
|
||||||
wrapWidth: undefined,
|
|
||||||
elapse: undefined,
|
|
||||||
animation: null,
|
|
||||||
resetAnimation: null,
|
|
||||||
timer: null
|
|
||||||
},
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
@ -70,89 +64,82 @@ VantComponent({
|
|||||||
hasRightIcon: true
|
hasRightIcon: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.resetAnimation = wx.createAnimation({
|
||||||
|
duration: 0,
|
||||||
|
timingFunction: 'linear'
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
destroyed() {
|
destroyed() {
|
||||||
const { timer } = this.data;
|
this.timer && clearTimeout(this.timer);
|
||||||
timer && clearTimeout(timer);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
init() {
|
init() {
|
||||||
this.getRect('.van-notice-bar__content').then(rect => {
|
Promise.all([
|
||||||
if (!rect || !rect.width) {
|
this.getRect('.van-notice-bar__content'),
|
||||||
|
this.getRect('.van-notice-bar__content-wrap')
|
||||||
|
]).then((rects: BoundingClientRect[]) => {
|
||||||
|
const [contentRect, wrapRect] = rects;
|
||||||
|
if (
|
||||||
|
contentRect == null ||
|
||||||
|
wrapRect == null ||
|
||||||
|
!contentRect.width ||
|
||||||
|
!wrapRect.width
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.set({
|
|
||||||
width: rect.width
|
|
||||||
});
|
|
||||||
|
|
||||||
this.getRect('.van-notice-bar__content-wrap').then(rect => {
|
const { speed, scrollable, delay } = this.data;
|
||||||
if (!rect || !rect.width) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrapWidth = rect.width;
|
if (scrollable && wrapRect.width < contentRect.width) {
|
||||||
const {
|
const duration = (contentRect.width / speed) * 1000;
|
||||||
width, speed, scrollable, delay
|
|
||||||
} = this.data;
|
|
||||||
|
|
||||||
if (scrollable && wrapWidth < width) {
|
this.wrapWidth = wrapRect.width;
|
||||||
const elapse = width / speed * 1000;
|
this.contentWidth = contentRect.width;
|
||||||
const animation = wx.createAnimation({
|
this.duration = duration;
|
||||||
duration: elapse,
|
this.animation = wx.createAnimation({
|
||||||
timeingFunction: 'linear',
|
duration,
|
||||||
delay
|
timingFunction: 'linear',
|
||||||
});
|
delay
|
||||||
const resetAnimation = wx.createAnimation({
|
});
|
||||||
duration: 0,
|
|
||||||
timeingFunction: 'linear'
|
|
||||||
});
|
|
||||||
|
|
||||||
this.set({
|
this.scroll();
|
||||||
elapse,
|
}
|
||||||
wrapWidth,
|
|
||||||
animation,
|
|
||||||
resetAnimation
|
|
||||||
}, () => {
|
|
||||||
this.scroll();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
scroll() {
|
scroll() {
|
||||||
const {
|
this.timer && clearTimeout(this.timer);
|
||||||
animation, resetAnimation, wrapWidth, elapse, speed
|
this.timer = null;
|
||||||
} = this.data;
|
|
||||||
resetAnimation.translateX(wrapWidth).step();
|
|
||||||
const animationData = animation.translateX(-(elapse * speed) / 1000).step();
|
|
||||||
this.set({
|
this.set({
|
||||||
animationData: resetAnimation.export()
|
animationData: this.resetAnimation
|
||||||
|
.translateX(this.wrapWidth)
|
||||||
|
.step()
|
||||||
|
.export()
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.set({
|
this.set({
|
||||||
animationData: animationData.export()
|
animationData: this.animation
|
||||||
|
.translateX(-this.contentWidth)
|
||||||
|
.step()
|
||||||
|
.export()
|
||||||
});
|
});
|
||||||
}, 100);
|
}, 20);
|
||||||
|
|
||||||
const timer = setTimeout(() => {
|
this.timer = setTimeout(() => {
|
||||||
this.scroll();
|
this.scroll();
|
||||||
}, elapse);
|
}, this.duration);
|
||||||
|
|
||||||
this.set({
|
|
||||||
timer
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onClickIcon() {
|
onClickIcon() {
|
||||||
const { timer } = this.data;
|
this.timer && clearTimeout(this.timer);
|
||||||
timer && clearTimeout(timer);
|
this.timer = null;
|
||||||
this.set({
|
|
||||||
show: false,
|
this.set({ show: false });
|
||||||
timer: null
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onClick(event: Weapp.Event) {
|
onClick(event: Weapp.Event) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<view
|
<view
|
||||||
wx:if="{{ show }}"
|
wx:if="{{ show }}"
|
||||||
class="custom-class van-notice-bar {{ hasRightIcon ? 'van-notice-bar--within-icon' : '' }}"
|
class="custom-class van-notice-bar {{ hasRightIcon ? 'van-notice-bar--within-icon' : '' }}"
|
||||||
style="color: {{ color }};background-color: {{ backgroundColor }}"
|
style="color: {{ color }}; background-color: {{ backgroundColor }};"
|
||||||
bind:tap="onClick"
|
bind:tap="onClick"
|
||||||
>
|
>
|
||||||
<view wx:if="{{ leftIcon }}" class="van-notice-bar__left-icon">
|
<view wx:if="{{ leftIcon }}" class="van-notice-bar__left-icon">
|
||||||
@ -13,19 +13,17 @@
|
|||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<block wx:if="{{ mode }}">
|
<van-icon
|
||||||
<van-icon
|
wx:if="{{ mode === 'closeable' }}"
|
||||||
wx:if="{{ mode === 'closeable' }}"
|
class="van-notice-bar__right-icon"
|
||||||
class="van-notice-bar__right-icon"
|
name="cross"
|
||||||
name="cross"
|
bind:tap="onClickIcon"
|
||||||
bind:tap="onClickIcon"
|
/>
|
||||||
/>
|
<navigator
|
||||||
<navigator
|
wx:if="{{ mode === 'link' }}"
|
||||||
wx:if="{{ mode === 'link' }}"
|
url="{{ url }}"
|
||||||
url="{{ url }}"
|
open-type="{{ openType }}"
|
||||||
open-type="{{ openType }}"
|
>
|
||||||
>
|
<van-icon class="van-notice-bar__right-icon" name="arrow" />
|
||||||
<van-icon class="van-notice-bar__right-icon" name="arrow" />
|
</navigator>
|
||||||
</navigator>
|
|
||||||
</block>
|
|
||||||
</view>
|
</view>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user