index.js 3.5KB

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