index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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], wrapRect = rects[1];
  82. if (contentRect == null ||
  83. wrapRect == null ||
  84. !contentRect.width ||
  85. !wrapRect.width) {
  86. return;
  87. }
  88. var _a = _this.data, speed = _a.speed, scrollable = _a.scrollable, delay = _a.delay;
  89. if (scrollable && wrapRect.width < contentRect.width) {
  90. var duration = (contentRect.width / speed) * 1000;
  91. _this.wrapWidth = wrapRect.width;
  92. _this.contentWidth = contentRect.width;
  93. _this.duration = duration;
  94. _this.animation = wx.createAnimation({
  95. duration: duration,
  96. timingFunction: 'linear',
  97. delay: delay
  98. });
  99. _this.scroll();
  100. }
  101. });
  102. },
  103. scroll: function () {
  104. var _this = this;
  105. this.timer && clearTimeout(this.timer);
  106. this.timer = null;
  107. this.setData({
  108. animationData: this.resetAnimation
  109. .translateX(this.wrapWidth)
  110. .step()
  111. .export()
  112. });
  113. setTimeout(function () {
  114. _this.setData({
  115. animationData: _this.animation
  116. .translateX(-_this.contentWidth)
  117. .step()
  118. .export()
  119. });
  120. }, 20);
  121. this.timer = setTimeout(function () {
  122. _this.scroll();
  123. }, this.duration);
  124. },
  125. onClickIcon: function () {
  126. this.timer && clearTimeout(this.timer);
  127. this.timer = null;
  128. this.setData({ show: false });
  129. },
  130. onClick: function (event) {
  131. this.$emit('click', event);
  132. }
  133. }
  134. });