From cc783fc86f1f17aeddcfc2d5ea02af3848cf044a Mon Sep 17 00:00:00 2001 From: rex Date: Sun, 3 Feb 2019 21:29:11 +0800 Subject: [PATCH] =?UTF-8?q?[improvement]=20NoticeBar:=20=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/notice-bar/index.less | 4 +- packages/notice-bar/index.ts | 113 +++++++++++++++------------------ packages/notice-bar/index.wxml | 30 ++++----- 3 files changed, 66 insertions(+), 81 deletions(-) diff --git a/packages/notice-bar/index.less b/packages/notice-bar/index.less index ee38226c..8336b03d 100644 --- a/packages/notice-bar/index.less +++ b/packages/notice-bar/index.less @@ -30,10 +30,10 @@ } &__content-wrap { - flex: 1; + position: relative; height: 24px; overflow: hidden; - position: relative; + flex: 1; } &__content { diff --git a/packages/notice-bar/index.ts b/packages/notice-bar/index.ts index 42b88b3a..2f6f0dc7 100644 --- a/packages/notice-bar/index.ts +++ b/packages/notice-bar/index.ts @@ -49,13 +49,7 @@ VantComponent({ data: { show: true, - hasRightIcon: false, - width: undefined, - wrapWidth: undefined, - elapse: undefined, - animation: null, - resetAnimation: null, - timer: null + hasRightIcon: false }, watch: { @@ -70,89 +64,82 @@ VantComponent({ hasRightIcon: true }); } + + this.resetAnimation = wx.createAnimation({ + duration: 0, + timingFunction: 'linear' + }); }, destroyed() { - const { timer } = this.data; - timer && clearTimeout(timer); + this.timer && clearTimeout(this.timer); }, methods: { init() { - this.getRect('.van-notice-bar__content').then(rect => { - if (!rect || !rect.width) { + Promise.all([ + 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; } - this.set({ - width: rect.width - }); - this.getRect('.van-notice-bar__content-wrap').then(rect => { - if (!rect || !rect.width) { - return; - } + const { speed, scrollable, delay } = this.data; - const wrapWidth = rect.width; - const { - width, speed, scrollable, delay - } = this.data; + if (scrollable && wrapRect.width < contentRect.width) { + const duration = (contentRect.width / speed) * 1000; - if (scrollable && wrapWidth < width) { - const elapse = width / speed * 1000; - const animation = wx.createAnimation({ - duration: elapse, - timeingFunction: 'linear', - delay - }); - const resetAnimation = wx.createAnimation({ - duration: 0, - timeingFunction: 'linear' - }); + this.wrapWidth = wrapRect.width; + this.contentWidth = contentRect.width; + this.duration = duration; + this.animation = wx.createAnimation({ + duration, + timingFunction: 'linear', + delay + }); - this.set({ - elapse, - wrapWidth, - animation, - resetAnimation - }, () => { - this.scroll(); - }); - } - }); + this.scroll(); + } }); }, scroll() { - const { - animation, resetAnimation, wrapWidth, elapse, speed - } = this.data; - resetAnimation.translateX(wrapWidth).step(); - const animationData = animation.translateX(-(elapse * speed) / 1000).step(); + this.timer && clearTimeout(this.timer); + this.timer = null; + this.set({ - animationData: resetAnimation.export() + animationData: this.resetAnimation + .translateX(this.wrapWidth) + .step() + .export() }); + setTimeout(() => { this.set({ - animationData: animationData.export() + animationData: this.animation + .translateX(-this.contentWidth) + .step() + .export() }); - }, 100); + }, 20); - const timer = setTimeout(() => { + this.timer = setTimeout(() => { this.scroll(); - }, elapse); - - this.set({ - timer - }); + }, this.duration); }, onClickIcon() { - const { timer } = this.data; - timer && clearTimeout(timer); - this.set({ - show: false, - timer: null - }); + this.timer && clearTimeout(this.timer); + this.timer = null; + + this.set({ show: false }); }, onClick(event: Weapp.Event) { diff --git a/packages/notice-bar/index.wxml b/packages/notice-bar/index.wxml index 67428222..d391ad95 100644 --- a/packages/notice-bar/index.wxml +++ b/packages/notice-bar/index.wxml @@ -1,7 +1,7 @@ @@ -13,19 +13,17 @@ - - - - - - + + + +