From 1dfa1af2889aacf3e7ed409ecfdee8e267ef3d78 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 17 May 2020 20:54:27 +0800 Subject: [PATCH] fix(NoticeBar): replay event only triggered once (#6293) --- src/notice-bar/index.js | 50 ++++++++++--------- src/notice-bar/index.less | 25 +--------- .../test/__snapshots__/demo.spec.js.snap | 12 ++--- .../test/__snapshots__/index.spec.js.snap | 2 +- src/notice-bar/test/index.spec.js | 4 +- 5 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/notice-bar/index.js b/src/notice-bar/index.js index 902d65968..039d3c8bb 100644 --- a/src/notice-bar/index.js +++ b/src/notice-bar/index.js @@ -1,4 +1,5 @@ import { createNamespace } from '../utils'; +import { doubleRaf } from '../utils/dom/raf'; import Icon from '../icon'; const [createComponent, bem] = createNamespace('notice-bar'); @@ -27,12 +28,12 @@ export default createComponent({ data() { return { + show: true, + offset: 0, + duration: 0, wrapWidth: 0, firstRound: true, - duration: 0, - offsetWidth: 0, - showNoticeBar: true, - animationClass: '', + contentWidth: 0, }; }, @@ -47,25 +48,27 @@ export default createComponent({ methods: { onClickIcon(event) { if (this.mode === 'closeable') { - this.showNoticeBar = false; + this.show = false; this.$emit('close', event); } }, - onAnimationEnd() { + onTransitionEnd() { + this.offset = this.wrapWidth; + this.duration = 0; this.firstRound = false; - this.$nextTick(() => { - this.duration = (this.offsetWidth + this.wrapWidth) / this.speed; - this.animationClass = bem('play--infinite'); + + doubleRaf(() => { + this.offset = -this.contentWidth; + this.duration = (this.contentWidth + this.wrapWidth) / this.speed; this.$emit('replay'); }); }, reset() { - this.wrapWidth = 0; - this.offsetWidth = 0; - this.animationClass = ''; this.duration = 0; + this.wrapWidth = 0; + this.contentWidth = 0; }, start() { @@ -76,12 +79,13 @@ export default createComponent({ } const wrapWidth = wrap.getBoundingClientRect().width; - const offsetWidth = content.getBoundingClientRect().width; - if (this.scrollable && offsetWidth > wrapWidth) { + const contentWidth = content.getBoundingClientRect().width; + + if (this.scrollable && contentWidth > wrapWidth) { + this.offset = -contentWidth; + this.duration = contentWidth / this.speed; this.wrapWidth = wrapWidth; - this.offsetWidth = offsetWidth; - this.duration = offsetWidth / this.speed; - this.animationClass = bem('play'); + this.contentWidth = contentWidth; } else { this.reset(); } @@ -98,9 +102,9 @@ export default createComponent({ }; const contentStyle = { - paddingLeft: this.firstRound ? 0 : this.wrapWidth + 'px', - animationDelay: (this.firstRound ? this.delay : 0) + 's', - animationDuration: this.duration + 's', + transform: `translateX(${this.offset}px)`, + transitionDelay: (this.firstRound ? this.delay : 0) + 's', + transitionDuration: this.duration + 's', }; function LeftIcon() { @@ -143,7 +147,7 @@ export default createComponent({ return ( diff --git a/src/notice-bar/index.less b/src/notice-bar/index.less index 0ded50105..34c35d049 100644 --- a/src/notice-bar/index.less +++ b/src/notice-bar/index.less @@ -34,20 +34,13 @@ &__content { position: absolute; white-space: nowrap; + transition-timing-function: linear; &.van-ellipsis { max-width: 100%; } } - &__play { - animation: van-notice-bar-play linear both; - - &--infinite { - animation: van-notice-bar-play-infinite linear infinite both; - } - } - &--wrapable { height: auto; padding: @notice-bar-wrapable-padding; @@ -65,19 +58,3 @@ } } } - -/** - * Declare two same keyframes - * In case that some mobile browsers can continue animation when className changed - */ -@keyframes van-notice-bar-play { - to { - transform: translate3d(-100%, 0, 0); - } -} - -@keyframes van-notice-bar-play-infinite { - to { - transform: translate3d(-100%, 0, 0); - } -} diff --git a/src/notice-bar/test/__snapshots__/demo.spec.js.snap b/src/notice-bar/test/__snapshots__/demo.spec.js.snap index b0b76e7fe..12bbba34e 100644 --- a/src/notice-bar/test/__snapshots__/demo.spec.js.snap +++ b/src/notice-bar/test/__snapshots__/demo.spec.js.snap @@ -6,34 +6,34 @@ exports[`renders demo correctly 1`] = `
@@ -42,7 +42,7 @@ exports[`renders demo correctly 1`] = `
diff --git a/src/notice-bar/test/__snapshots__/index.spec.js.snap b/src/notice-bar/test/__snapshots__/index.spec.js.snap index 5b7145d72..37d112ac5 100644 --- a/src/notice-bar/test/__snapshots__/index.spec.js.snap +++ b/src/notice-bar/test/__snapshots__/index.spec.js.snap @@ -2,7 +2,7 @@ exports[`icon slot 1`] = `