耀实惠小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.9 KiB

  1. <template>
  2. <view class="content">
  3. <view class="screen-swiper-box">
  4. <swiper :current="index" @change="swiperChange" class="screen-swiper" indicator-dots :circular="circular"
  5. :autoplay="autoplay" :interval="interval" duration="500" :style="[whStyle]">
  6. <swiper-item v-for="(item, i) in list" :key="i" :id="i">
  7. <video v-if="item[typeName] == videoValue" :id="'myVideo-'+i" class="myVideo" :src="item[linkName]"
  8. :controls="false" objectFit="cover" :enable-progress-gesture="false" show-loading
  9. :play-btn-position="center" :show-fullscreen-btn="false" :style="[whStyle]"
  10. @click="swiperClick(item,i)"></video>
  11. <image v-if="item[typeName] == imgvalue" class="swiperImage" :src="item[linkName]" mode="aspectFill"
  12. :style="[whStyle]" @click="swiperClick(item,i)">
  13. </image>
  14. </swiper-item>
  15. </swiper>
  16. <view class="screen-swiper-bar flex align-center">
  17. {{ index + 1 }} / {{ list.length }}
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * @property {Array} list 图片/视频列表
  25. * @property {String | Number} width 显示视频/图片的宽度
  26. * @property {String | Number} height 显示视频/图片的高度
  27. * @property {String} linkName url字段字段
  28. * @property {String} typeName 判断视频或者图片的字段
  29. * @property {String} videoValue 判断视频的值
  30. * @property {String} imgvalue 判断图片的值
  31. * @property {Boolean} autoplay swiper是否自动切换
  32. * @property {Boolean} circular swiper是否采用衔接滑动即播放到末尾后重新回到开头
  33. * @property {Boolean} autoPlayVideo swiper切换到视频是是否自动播放视频
  34. * @property {Boolean} interval swiper自动切换时间间隔
  35. * @event {Function} swiperClick 点击swiper时触发
  36. * @event {Function} swiperChange swiper切换时触发
  37. */
  38. export default {
  39. props: {
  40. // 图片/视频列表
  41. list: {
  42. type: Array,
  43. required: true,
  44. default: () => {
  45. return []
  46. }
  47. },
  48. // 用于显示视频/图片的宽度
  49. width: {
  50. type: String,
  51. default: '750rpx'
  52. },
  53. // 用于显示视频/图片的高度
  54. height: {
  55. type: String,
  56. default: '260rpx'
  57. },
  58. // 用与判断视频或者图片的字段
  59. typeName: {
  60. type: String,
  61. default: 'type'
  62. },
  63. // 用于判断视频的值
  64. videoValue: {
  65. type: [String, Number],
  66. default: 'video'
  67. },
  68. // 用于判断图片的值
  69. imgvalue: {
  70. type: [String, Number],
  71. default: 'img'
  72. },
  73. // url字段
  74. linkName: {
  75. type: String,
  76. default: 'link'
  77. },
  78. // swiper是否自动切换
  79. autoplay: {
  80. type: Boolean,
  81. default: false
  82. },
  83. // swiper是否采用衔接滑动,即播放到末尾后重新回到开头
  84. circular: {
  85. type: Boolean,
  86. default: false
  87. },
  88. // swiper切换到视频是是否自动播放视频
  89. autoPlayVideo: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // swiper自动切换时间间隔
  94. interval: {
  95. type: Number,
  96. default: 3500
  97. }
  98. },
  99. data() {
  100. return {
  101. index: 0, // 当前页
  102. videoCtx: ''
  103. }
  104. },
  105. mounted() {
  106. // 如果为true且第一个是视频的话就自动播放
  107. if (this.autoPlayVideo) {
  108. this.videoCtx = uni.createVideoContext('myVideo-' + 0, this)
  109. this.videoCtx.play()
  110. }
  111. },
  112. methods: {
  113. swiperChange(e) {
  114. // 获取上一个
  115. let obj = this.list[this.index]
  116. if (obj[this.typeName] == this.videoValue) {
  117. // console.log('暂停', this.index)
  118. this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
  119. this.videoCtx.pause()
  120. }
  121. // 获取当前
  122. this.index = e.detail.current // 更新当前index
  123. obj = this.list[this.index]
  124. if (obj[this.typeName] == this.videoValue && this.autoPlayVideo) {
  125. // console.log('播放', this.index)
  126. this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
  127. this.videoCtx.play()
  128. }
  129. this.$emit('swiperChange', this.index)
  130. },
  131. // 暂停播放
  132. pause() {
  133. let obj = this.list[this.index]
  134. if (obj[this.typeName] == this.videoValue) {
  135. this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
  136. this.videoCtx.pause()
  137. }
  138. },
  139. // 播放
  140. play() {
  141. let obj = this.list[this.index]
  142. if (obj[this.typeName] == this.videoValue) {
  143. this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
  144. this.videoCtx.play()
  145. }
  146. },
  147. // 点击当前视频或图片的回调
  148. swiperClick(item, i) {
  149. this.$emit('swiperClick', item, i)
  150. }
  151. },
  152. computed: {
  153. // 设置width和height属性
  154. whStyle() {
  155. let style = {}
  156. style.width = this.width
  157. style.height = this.height
  158. return style
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss" scoped>
  164. .content {
  165. .screen-swiper-box {
  166. position: relative;
  167. .screen-swiper {
  168. .swiperImage {}
  169. .myVideo {}
  170. }
  171. }
  172. .screen-swiper-bar {
  173. position: absolute;
  174. bottom: 24rpx;
  175. right: 24rpx;
  176. min-width: 80rpx;
  177. padding: 4rpx 20rpx;
  178. z-index: 1;
  179. background: rgba(0, 0, 0, .3);
  180. border-radius: 22rpx;
  181. color: #fff;
  182. font-size: 24rpx;
  183. }
  184. }
  185. </style>