index.js 3.1 KB

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