index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Component({
  2. externalClasses: ['l-class'],
  3. properties: {
  4. type: {
  5. type: String,
  6. value: 'still'
  7. },
  8. // 轮播数组
  9. swipArr: Array,
  10. // 前置图标
  11. frontIconName: {
  12. type: String,
  13. value: ''
  14. },
  15. frontIconSize: {
  16. type: Number,
  17. value: 28
  18. },
  19. frontIconColor: {
  20. type: String,
  21. value: '#3683D6'
  22. },
  23. endIconName: {
  24. type: String,
  25. value: ''
  26. },
  27. endIconSize: {
  28. type: Number,
  29. value: 28
  30. },
  31. endIconColor: {
  32. type: String,
  33. value: '#3683D6'
  34. },
  35. // 背景颜色
  36. backgroundcolor: {
  37. type: String,
  38. value: '#DFEDFF'
  39. },
  40. // 字体及图标颜色
  41. color: {
  42. type: String,
  43. value: '#3683D6'
  44. },
  45. // 滚动速度
  46. speed: {
  47. type: Number,
  48. value: 1500
  49. },
  50. show: {
  51. type: Boolean,
  52. value: true
  53. },
  54. close: {
  55. type: Boolean,
  56. value: false
  57. }
  58. },
  59. data: {
  60. wrapWidth: 0,
  61. width: 0,
  62. duration: 0,
  63. animation: null,
  64. timer: null,
  65. },
  66. detached() {
  67. this.destroyTimer();
  68. },
  69. ready() {
  70. if (this.properties.type == 'roll' && this.properties.show) {
  71. this.initAnimation();
  72. }
  73. },
  74. methods: {
  75. initAnimation() {
  76. wx.createSelectorQuery().in(this).select('.l-noticebar-content-wrap').boundingClientRect((wrapRect) => {
  77. wx.createSelectorQuery().in(this).select('.l-noticebar-content').boundingClientRect((rect) => {
  78. const duration = rect.width / 40 * this.data.speed;
  79. const animation = wx.createAnimation({
  80. duration: duration,
  81. timingFunction: 'linear',
  82. });
  83. this.setData({
  84. wrapWidth: wrapRect.width,
  85. width: rect.width,
  86. duration: duration,
  87. animation: animation
  88. }, () => {
  89. this.startAnimation();
  90. });
  91. }).exec();
  92. }).exec();
  93. },
  94. startAnimation() {
  95. //reset
  96. if (this.data.animation.option.transition.duration !== 0) {
  97. this.data.animation.option.transition.duration = 0;
  98. const resetAnimation = this.data.animation.translateX(this.data.wrapWidth).step();
  99. this.setData({
  100. animationData: resetAnimation.export()
  101. });
  102. }
  103. this.data.animation.option.transition.duration = this.data.duration;
  104. const animationData = this.data.animation.translateX(-this.data.width).step();
  105. setTimeout(() => {
  106. this.setData({
  107. animationData: animationData.export()
  108. });
  109. }, 100);
  110. const timer = setTimeout(() => {
  111. this.startAnimation();
  112. }, this.data.duration);
  113. this.setData({
  114. timer,
  115. });
  116. },
  117. destroyTimer() {
  118. if (this.data.timer) {
  119. clearTimeout(this.data.timer);
  120. }
  121. },
  122. handleTap() {
  123. this.triggerEvent('lintap',{},{ bubbles: true, composed: true });
  124. this.setData({
  125. timer: null
  126. });
  127. },
  128. onSwip(e) {
  129. this.triggerEvent('lintap', {
  130. ...e.currentTarget.dataset
  131. },{ bubbles: true, composed: true });
  132. },
  133. onIconTap(){
  134. this.triggerEvent('linicontap',{},{ bubbles: true, composed: true });
  135. this.setData({
  136. timer: null
  137. });
  138. },
  139. onClose() {
  140. this.setData({
  141. timer: null,
  142. show: false
  143. });
  144. },
  145. }
  146. });