index.js 2.8 KB

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