index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const VALID_MODE = ['closeable'];
  2. const FONT_COLOR = '#f60';
  3. const BG_COLOR = '#fff7cc';
  4. Component({
  5. externalClasses: ['i-class'],
  6. properties: {
  7. closable: {
  8. type: Boolean,
  9. value: false
  10. },
  11. icon: {
  12. type: String,
  13. value: ''
  14. },
  15. loop: {
  16. type: Boolean,
  17. value: false
  18. },
  19. // 背景颜色
  20. backgroundcolor: {
  21. type: String,
  22. value: '#fefcec'
  23. },
  24. // 字体及图标颜色
  25. color: {
  26. type: String,
  27. value: '#f76a24'
  28. },
  29. // 滚动速度
  30. speed: {
  31. type: Number,
  32. value: 1000
  33. }
  34. },
  35. data: {
  36. show: true,
  37. wrapWidth: 0,
  38. width: 0,
  39. duration: 0,
  40. animation: null,
  41. timer: null,
  42. },
  43. detached() {
  44. this.destroyTimer();
  45. },
  46. ready() {
  47. if (this.data.loop) {
  48. this.initAnimation();
  49. }
  50. },
  51. methods: {
  52. initAnimation() {
  53. wx.createSelectorQuery().in(this).select('.i-noticebar-content-wrap').boundingClientRect((wrapRect) => {
  54. wx.createSelectorQuery().in(this).select('.i-noticebar-content').boundingClientRect((rect) => {
  55. const duration = rect.width / 40 * this.data.speed;
  56. const animation = wx.createAnimation({
  57. duration: duration,
  58. timingFunction: "linear",
  59. });
  60. this.setData({
  61. wrapWidth: wrapRect.width,
  62. width: rect.width,
  63. duration: duration,
  64. animation: animation
  65. }, () => {
  66. this.startAnimation();
  67. });
  68. }).exec();
  69. }).exec();
  70. },
  71. startAnimation() {
  72. //reset
  73. if (this.data.animation.option.transition.duration !== 0) {
  74. this.data.animation.option.transition.duration = 0;
  75. const resetAnimation = this.data.animation.translateX(this.data.wrapWidth).step();
  76. this.setData({
  77. animationData: resetAnimation.export()
  78. });
  79. }
  80. this.data.animation.option.transition.duration = this.data.duration;
  81. const animationData = this.data.animation.translateX(-this.data.width).step();
  82. setTimeout(() => {
  83. this.setData({
  84. animationData: animationData.export()
  85. });
  86. }, 100);
  87. const timer = setTimeout(() => {
  88. this.startAnimation();
  89. }, this.data.duration);
  90. this.setData({
  91. timer,
  92. });
  93. },
  94. destroyTimer() {
  95. if (this.data.timer) {
  96. clearTimeout(this.data.timer);
  97. }
  98. },
  99. handleClose() {
  100. this.destroyTimer();
  101. this.setData({
  102. show: false,
  103. timer: null
  104. });
  105. }
  106. }
  107. });