index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { VantComponent } from '../common/component';
  2. import { getRect, requestAnimationFrame } from '../common/utils';
  3. VantComponent({
  4. props: {
  5. text: {
  6. type: String,
  7. value: '',
  8. observer: 'init',
  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. observer: 'init',
  30. },
  31. scrollable: {
  32. type: Boolean,
  33. value: true,
  34. },
  35. leftIcon: {
  36. type: String,
  37. value: '',
  38. },
  39. color: String,
  40. backgroundColor: String,
  41. background: String,
  42. wrapable: Boolean,
  43. },
  44. data: {
  45. show: true,
  46. },
  47. created() {
  48. this.resetAnimation = wx.createAnimation({
  49. duration: 0,
  50. timingFunction: 'linear',
  51. });
  52. },
  53. destroyed() {
  54. this.timer && clearTimeout(this.timer);
  55. },
  56. mounted() {
  57. this.init();
  58. },
  59. methods: {
  60. init() {
  61. requestAnimationFrame(() => {
  62. Promise.all([
  63. getRect(this, '.van-notice-bar__content'),
  64. getRect(this, '.van-notice-bar__wrap'),
  65. ]).then((rects) => {
  66. const [contentRect, wrapRect] = rects;
  67. if (
  68. contentRect == null ||
  69. wrapRect == null ||
  70. !contentRect.width ||
  71. !wrapRect.width
  72. ) {
  73. return;
  74. }
  75. const { speed, scrollable, delay } = this.data;
  76. if (scrollable || wrapRect.width < contentRect.width) {
  77. const duration = (contentRect.width / speed) * 1000;
  78. this.wrapWidth = wrapRect.width;
  79. this.contentWidth = contentRect.width;
  80. this.duration = duration;
  81. this.animation = wx.createAnimation({
  82. duration,
  83. timingFunction: 'linear',
  84. delay,
  85. });
  86. this.scroll();
  87. }
  88. });
  89. });
  90. },
  91. scroll() {
  92. this.timer && clearTimeout(this.timer);
  93. this.timer = null;
  94. this.setData({
  95. animationData: this.resetAnimation
  96. .translateX(this.wrapWidth)
  97. .step()
  98. .export(),
  99. });
  100. requestAnimationFrame(() => {
  101. this.setData({
  102. animationData: this.animation
  103. .translateX(-this.contentWidth)
  104. .step()
  105. .export(),
  106. });
  107. });
  108. this.timer = setTimeout(() => {
  109. this.scroll();
  110. }, this.duration);
  111. },
  112. onClickIcon(event) {
  113. if (this.data.mode === 'closeable') {
  114. this.timer && clearTimeout(this.timer);
  115. this.timer = null;
  116. this.setData({ show: false });
  117. this.$emit('close', event.detail);
  118. }
  119. },
  120. onClick(event) {
  121. this.$emit('click', event);
  122. },
  123. },
  124. });