media-carousel.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // components/media-carousel/media-carousel.ts
  2. // 定义微信小程序组件的类型
  3. interface IComponentInstance {
  4. setData: (data: any) => void;
  5. triggerEvent: (name: string, detail: any) => void;
  6. data: any;
  7. properties: any;
  8. }
  9. interface IComponentOptions {
  10. properties: Record<string, any>;
  11. data: any;
  12. methods: Record<string, Function>;
  13. }
  14. // 声明全局的 Component 函数
  15. declare function Component(options: IComponentOptions): void;
  16. Component({
  17. /**
  18. * 组件的属性列表
  19. */
  20. properties: {
  21. // 媒体列表
  22. mediaList: {
  23. type: Array,
  24. value: [],
  25. observer: function(this: IComponentInstance, newVal: any[]) {
  26. this.setData({
  27. totalCount: newVal.length
  28. });
  29. }
  30. },
  31. // 项目ID,用于标识轮播图属于哪个项目
  32. itemId: {
  33. type: String,
  34. value: ''
  35. },
  36. // 是否显示指示器
  37. showIndicator: {
  38. type: Boolean,
  39. value: true
  40. },
  41. // 指示器颜色
  42. indicatorColor: {
  43. type: String,
  44. value: 'rgba(255, 255, 255, 0.6)'
  45. },
  46. // 指示器激活颜色
  47. indicatorActiveColor: {
  48. type: String,
  49. value: '#fff'
  50. },
  51. // 是否自动播放
  52. autoplay: {
  53. type: Boolean,
  54. value: true
  55. },
  56. // 自动播放间隔时间
  57. interval: {
  58. type: Number,
  59. value: 3000
  60. },
  61. // 滑动动画时长
  62. duration: {
  63. type: Number,
  64. value: 500
  65. },
  66. // 是否循环播放
  67. circular: {
  68. type: Boolean,
  69. value: true
  70. }
  71. },
  72. /**
  73. * 组件的初始数据
  74. */
  75. data: {
  76. currentIndex: 0,
  77. totalCount: 0
  78. },
  79. /**
  80. * 组件的方法列表
  81. */
  82. methods: {
  83. // 图片加载成功
  84. onImageLoad(this: IComponentInstance, e: any) {
  85. const { index } = e.currentTarget.dataset;
  86. this.triggerEvent('imageload', {
  87. index: index,
  88. item: this.data.mediaList[index],
  89. itemId: this.properties.itemId
  90. });
  91. },
  92. // 图片加载失败
  93. onImageError(this: IComponentInstance, e: any) {
  94. const { index } = e.currentTarget.dataset;
  95. this.triggerEvent('imageerror', {
  96. index: index,
  97. item: this.data.mediaList[index],
  98. itemId: this.properties.itemId
  99. });
  100. },
  101. // 视频播放错误
  102. onVideoError(this: IComponentInstance, e: any) {
  103. const { index } = e.currentTarget.dataset;
  104. this.triggerEvent('videoerror', {
  105. index: index,
  106. item: this.data.mediaList[index],
  107. itemId: this.properties.itemId
  108. });
  109. }
  110. }
  111. });