index.js 3.6 KB

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