123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { VantComponent } from '../common/component';
  2. const FONT_COLOR = '#ed6a0c';
  3. const BG_COLOR = '#fffbe8';
  4. VantComponent({
  5. props: {
  6. text: {
  7. type: String,
  8. value: ''
  9. },
  10. mode: {
  11. type: String,
  12. value: ''
  13. },
  14. url: {
  15. type: String,
  16. value: ''
  17. },
  18. openType: {
  19. type: String,
  20. value: 'navigate'
  21. },
  22. delay: {
  23. type: Number,
  24. value: 0
  25. },
  26. speed: {
  27. type: Number,
  28. value: 50
  29. },
  30. scrollable: {
  31. type: Boolean,
  32. value: true
  33. },
  34. leftIcon: {
  35. type: String,
  36. value: ''
  37. },
  38. color: {
  39. type: String,
  40. value: FONT_COLOR
  41. },
  42. backgroundColor: {
  43. type: String,
  44. value: BG_COLOR
  45. }
  46. },
  47. data: {
  48. show: true,
  49. hasRightIcon: false
  50. },
  51. watch: {
  52. text() {
  53. this.set({}, this.init);
  54. }
  55. },
  56. created() {
  57. if (this.data.mode) {
  58. this.set({
  59. hasRightIcon: true
  60. });
  61. }
  62. this.resetAnimation = wx.createAnimation({
  63. duration: 0,
  64. timingFunction: 'linear'
  65. });
  66. },
  67. destroyed() {
  68. this.timer && clearTimeout(this.timer);
  69. },
  70. methods: {
  71. init() {
  72. Promise.all([
  73. this.getRect('.van-notice-bar__content'),
  74. this.getRect('.van-notice-bar__content-wrap')
  75. ]).then((rects) => {
  76. const [contentRect, wrapRect] = rects;
  77. if (contentRect == null ||
  78. wrapRect == null ||
  79. !contentRect.width ||
  80. !wrapRect.width) {
  81. return;
  82. }
  83. const { speed, scrollable, delay } = this.data;
  84. if (scrollable && wrapRect.width < contentRect.width) {
  85. const duration = (contentRect.width / speed) * 1000;
  86. this.wrapWidth = wrapRect.width;
  87. this.contentWidth = contentRect.width;
  88. this.duration = duration;
  89. this.animation = wx.createAnimation({
  90. duration,
  91. timingFunction: 'linear',
  92. delay
  93. });
  94. this.scroll();
  95. }
  96. });
  97. },
  98. scroll() {
  99. this.timer && clearTimeout(this.timer);
  100. this.timer = null;
  101. this.set({
  102. animationData: this.resetAnimation
  103. .translateX(this.wrapWidth)
  104. .step()
  105. .export()
  106. });
  107. setTimeout(() => {
  108. this.set({
  109. animationData: this.animation
  110. .translateX(-this.contentWidth)
  111. .step()
  112. .export()
  113. });
  114. }, 20);
  115. this.timer = setTimeout(() => {
  116. this.scroll();
  117. }, this.duration);
  118. },
  119. onClickIcon() {
  120. this.timer && clearTimeout(this.timer);
  121. this.timer = null;
  122. this.set({ show: false });
  123. },
  124. onClick(event) {
  125. this.$emit('click', event);
  126. }
  127. }
  128. });