index.js 3.2 KB

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