index.js 3.1 KB

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