index.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: 1
  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. wrapable: Boolean
  47. },
  48. data: {
  49. show: true
  50. },
  51. watch: {
  52. text() {
  53. this.set({}, this.init);
  54. }
  55. },
  56. created() {
  57. this.resetAnimation = wx.createAnimation({
  58. duration: 0,
  59. timingFunction: 'linear'
  60. });
  61. },
  62. destroyed() {
  63. this.timer && clearTimeout(this.timer);
  64. },
  65. methods: {
  66. init() {
  67. Promise.all([
  68. this.getRect('.van-notice-bar__content'),
  69. this.getRect('.van-notice-bar__wrap')
  70. ]).then((rects) => {
  71. const [contentRect, wrapRect] = rects;
  72. if (contentRect == null ||
  73. wrapRect == null ||
  74. !contentRect.width ||
  75. !wrapRect.width) {
  76. return;
  77. }
  78. const { speed, scrollable, delay } = this.data;
  79. if (scrollable && wrapRect.width < contentRect.width) {
  80. const duration = (contentRect.width / speed) * 1000;
  81. this.wrapWidth = wrapRect.width;
  82. this.contentWidth = contentRect.width;
  83. this.duration = duration;
  84. this.animation = wx.createAnimation({
  85. duration,
  86. timingFunction: 'linear',
  87. delay
  88. });
  89. this.scroll();
  90. }
  91. });
  92. },
  93. scroll() {
  94. this.timer && clearTimeout(this.timer);
  95. this.timer = null;
  96. this.set({
  97. animationData: this.resetAnimation
  98. .translateX(this.wrapWidth)
  99. .step()
  100. .export()
  101. });
  102. setTimeout(() => {
  103. this.set({
  104. animationData: this.animation
  105. .translateX(-this.contentWidth)
  106. .step()
  107. .export()
  108. });
  109. }, 20);
  110. this.timer = setTimeout(() => {
  111. this.scroll();
  112. }, this.duration);
  113. },
  114. onClickIcon() {
  115. this.timer && clearTimeout(this.timer);
  116. this.timer = null;
  117. this.set({ show: false });
  118. },
  119. onClick(event) {
  120. this.$emit('click', event);
  121. }
  122. }
  123. });