index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. var VALID_MODE = ['closeable'];
  3. var FONT_COLOR = '#f60';
  4. var BG_COLOR = '#fff7cc';
  5. Component({
  6. properties: {
  7. text: {
  8. type: String,
  9. value: '',
  10. observer: function observer(newVal) {
  11. this.setData({}, this._init);
  12. }
  13. },
  14. mode: {
  15. type: String,
  16. value: ''
  17. },
  18. url: {
  19. type: String,
  20. value: ''
  21. },
  22. openType: {
  23. type: String,
  24. value: 'navigate'
  25. },
  26. delay: {
  27. type: Number,
  28. value: 0
  29. },
  30. speed: {
  31. type: Number,
  32. value: 40
  33. },
  34. scrollable: {
  35. type: Boolean,
  36. value: false
  37. },
  38. leftIcon: {
  39. type: String,
  40. value: ''
  41. },
  42. color: {
  43. type: String,
  44. value: FONT_COLOR
  45. },
  46. backgroundColor: {
  47. type: String,
  48. value: BG_COLOR
  49. },
  50. paddingTop: {
  51. type: String,
  52. value: 4
  53. }
  54. },
  55. data: {
  56. show: true,
  57. hasRightIcon: false,
  58. width: undefined,
  59. wrapWidth: undefined,
  60. elapse: undefined,
  61. animation: null,
  62. resetAnimation: null,
  63. timer: null
  64. },
  65. attached: function attached() {
  66. var mode = this.data.mode;
  67. if (mode && this._checkMode(mode)) {
  68. this.setData({
  69. hasRightIcon: true
  70. });
  71. }
  72. },
  73. detached: function detached() {
  74. var timer = this.data.timer;
  75. timer && clearTimeout(timer);
  76. },
  77. methods: {
  78. _checkMode: function _checkMode(val) {
  79. var isValidMode = ~VALID_MODE.indexOf(val);
  80. if (!isValidMode) {
  81. console.warn('mode only accept value of ' + VALID_MODE + ', now get ' + val + '.');
  82. }
  83. return isValidMode;
  84. },
  85. _init: function _init() {
  86. var _this = this;
  87. wx.createSelectorQuery().in(this).select('.zan-noticebar__content').boundingClientRect(function(rect) {
  88. if (!rect || !rect.width) {
  89. return;
  90. }
  91. _this.setData({
  92. width: rect.width
  93. });
  94. wx.createSelectorQuery().in(_this).select('.zan-noticebar__content-wrap').boundingClientRect(function(rect) {
  95. if (!rect || !rect.width) {
  96. return;
  97. }
  98. var wrapWidth = rect.width;
  99. var _data = _this.data,
  100. width = _data.width,
  101. speed = _data.speed,
  102. scrollable = _data.scrollable,
  103. delay = _data.delay;
  104. if (scrollable && wrapWidth < width) {
  105. var elapse = width / speed * 1000;
  106. var animation = wx.createAnimation({
  107. duration: elapse,
  108. timeingFunction: 'linear',
  109. delay: delay
  110. });
  111. var resetAnimation = wx.createAnimation({
  112. duration: 0,
  113. timeingFunction: 'linear'
  114. });
  115. _this.setData({
  116. elapse: elapse,
  117. wrapWidth: wrapWidth,
  118. animation: animation,
  119. resetAnimation: resetAnimation
  120. }, function() {
  121. _this._scroll();
  122. });
  123. }
  124. }).exec();
  125. }).exec();
  126. },
  127. _scroll: function _scroll() {
  128. var _this2 = this;
  129. var _data2 = this.data,
  130. animation = _data2.animation,
  131. resetAnimation = _data2.resetAnimation,
  132. wrapWidth = _data2.wrapWidth,
  133. elapse = _data2.elapse,
  134. speed = _data2.speed;
  135. resetAnimation.translateX(wrapWidth).step();
  136. var animationData = animation.translateX(-(elapse * speed) / 1000).step();
  137. this.setData({
  138. animationData: resetAnimation.export()
  139. });
  140. setTimeout(function() {
  141. _this2.setData({
  142. animationData: animationData.export()
  143. });
  144. }, 100);
  145. var timer = setTimeout(function() {
  146. _this2._scroll();
  147. }, elapse);
  148. this.setData({
  149. timer: timer
  150. });
  151. },
  152. _handleButtonClick: function _handleButtonClick() {
  153. var timer = this.data.timer;
  154. timer && clearTimeout(timer);
  155. this.setData({
  156. show: false,
  157. timer: null
  158. });
  159. }
  160. }
  161. });