media-carousel.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // 是否显示指示器
  18. showIndicator: {
  19. type: Boolean,
  20. value: true
  21. },
  22. // 指示器颜色
  23. indicatorColor: {
  24. type: String,
  25. value: 'rgba(255, 255, 255, 0.6)'
  26. },
  27. // 指示器激活颜色
  28. indicatorActiveColor: {
  29. type: String,
  30. value: '#fff'
  31. },
  32. // 是否自动播放
  33. autoplay: {
  34. type: Boolean,
  35. value: true
  36. },
  37. // 自动播放间隔时间
  38. interval: {
  39. type: Number,
  40. value: 3000
  41. },
  42. // 滑动动画时长
  43. duration: {
  44. type: Number,
  45. value: 500
  46. },
  47. // 是否循环播放
  48. circular: {
  49. type: Boolean,
  50. value: true
  51. },
  52. },
  53. /**
  54. * 组件的初始数据
  55. */
  56. data: {
  57. currentIndex: 0,
  58. totalCount: 0
  59. },
  60. /**
  61. * 组件的方法列表
  62. */
  63. methods: {
  64. // 轮播切换事件
  65. onChange(e) {
  66. const { current } = e.detail;
  67. this.setData({
  68. currentIndex: current
  69. });
  70. // 触发自定义事件
  71. this.triggerEvent('change', {
  72. current: current,
  73. total: this.data.totalCount
  74. });
  75. },
  76. // 图片加载成功
  77. onImageLoad(e) {
  78. const { index } = e.currentTarget.dataset;
  79. this.triggerEvent('imageload', {
  80. index: index,
  81. item: this.data.mediaList[index]
  82. });
  83. },
  84. // 图片加载失败
  85. onImageError(e) {
  86. const { index } = e.currentTarget.dataset;
  87. this.triggerEvent('imageerror', {
  88. index: index,
  89. item: this.data.mediaList[index]
  90. });
  91. },
  92. // 视频开始播放
  93. onVideoPlay(e) {
  94. const { index } = e.currentTarget.dataset;
  95. this.triggerEvent('videoplay', {
  96. index: index,
  97. item: this.data.mediaList[index]
  98. });
  99. },
  100. // 视频暂停
  101. onVideoPause(e) {
  102. const { index } = e.currentTarget.dataset;
  103. this.triggerEvent('videopause', {
  104. index: index,
  105. item: this.data.mediaList[index]
  106. });
  107. },
  108. // 视频播放结束
  109. onVideoEnded(e) {
  110. const { index } = e.currentTarget.dataset;
  111. this.triggerEvent('videoended', {
  112. index: index,
  113. item: this.data.mediaList[index]
  114. });
  115. },
  116. // 视频播放错误
  117. onVideoError(e) {
  118. const { index } = e.currentTarget.dataset;
  119. this.triggerEvent('videoerror', {
  120. index: index,
  121. item: this.data.mediaList[index]
  122. });
  123. }
  124. }
  125. });