media-carousel.js 2.0 KB

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