index.js 3.2 KB

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