index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view class="swiper-container">
  3. <swiper class="swiper" circular :indicator-dots="false" :circular="circular" :next-margin="nextMargin" :autoplay="autoplay" :interval="interval"
  4. :duration="duration" :style="style" @change="onSwiperChange">
  5. <template v-for="(item, index) in items">
  6. <swiper-item>
  7. <view class="image-wrapper">
  8. <image v-if="item.src" :mode="imageMode" :src="item.src" :style="imageStyle" @tap="onImageClick(item)"/>
  9. <template v-else>
  10. <slot :scope="item" :style="style"></slot>
  11. </template>
  12. <view class="video-play" v-if="item.url && item.url.indexOf('.mp4') !== -1">
  13. </view>
  14. </view>
  15. </swiper-item>
  16. </template>
  17. </swiper>
  18. <!-- 自定义指示点 -->
  19. <view v-if="indicatorDots" class="custom-indicator" :class="{ 'morphing': indicatoDotsStyle === 'morphing' }">
  20. <view
  21. v-for="(item, index) in items"
  22. :key="index"
  23. class="dot"
  24. :class="{ 'active': current === index }"
  25. ></view>
  26. </view>
  27. <van-overlay :show="showOverlay" @tap="onClickHideOverlay">
  28. <view class="overlay-wrapper display-flex-center">
  29. <video v-if="videoUrl" :src="videoUrl" controls autoplay loop @tap.stop></video>
  30. </view>
  31. </van-overlay>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. options: {
  37. styleIsolation: 'shared'
  38. },
  39. props: {
  40. items: Array,
  41. borderRadius: {
  42. type: Number
  43. },
  44. nextMargin: {
  45. type: String,
  46. default: ''
  47. },
  48. height: {
  49. type: Number,
  50. default: 400
  51. },
  52. imageWidth: {
  53. type: Number,
  54. default: 0
  55. },
  56. imageHeight: {
  57. type: Number,
  58. default: 0
  59. },
  60. imageMode: {
  61. type: String,
  62. default: 'aspectFill' // 参考https://zh.uniapp.dcloud.io/component/image.html
  63. },
  64. indicatoDotsStyle: {
  65. type: String,
  66. default: 'default' // or 'morphing'
  67. },
  68. indicatorDots: {
  69. type: Boolean,
  70. default: true
  71. },
  72. circular: {
  73. type: Boolean,
  74. default: true
  75. },
  76. autoplay: {
  77. type: Boolean,
  78. default: true
  79. },
  80. interval: {
  81. type: Number,
  82. default: 5000
  83. },
  84. duration: {
  85. type: Number,
  86. default: 500
  87. }
  88. },
  89. computed: {
  90. style() {
  91. return 'height:' + this.height + 'rpx;'
  92. },
  93. imageStyle() {
  94. let style = 'height:' + (this.imageHeight || this.height) + 'rpx;'
  95. if (this.imageWidth) {
  96. style += 'width:' + this.imageWidth + 'rpx;'
  97. }
  98. if (this.borderRadius) {
  99. style += 'border-radius:' + this.borderRadius + 'rpx;'
  100. }
  101. return style
  102. }
  103. },
  104. watch: {
  105. },
  106. data() {
  107. return {
  108. showOverlay: false,
  109. videoUrl: '',
  110. current: 0
  111. }
  112. },
  113. created() {
  114. },
  115. mounted() {
  116. },
  117. methods: {
  118. onClickHideOverlay() {
  119. this.showOverlay = false
  120. },
  121. onSwiperChange(e) {
  122. this.current = e.detail.current
  123. },
  124. onImageClick(item) {
  125. if (item.url) {
  126. if (item.url.indexOf('.mp4') !== -1) {
  127. this.videoUrl = item.url
  128. this.showOverlay = true
  129. } else if (item.url.indexOf('http') === -1) {
  130. uni.navigateTo({
  131. url: item.url
  132. })
  133. } else {
  134. uni.navigateTo({
  135. url: '/pages/index/webview?url=' + item.url
  136. })
  137. }
  138. }
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. .image-wrapper {
  145. position: relative;
  146. .video-play{
  147. position: absolute;
  148. top: 0;
  149. bottom: 0;
  150. right: 0;
  151. left: 0;
  152. width: 82rpx;
  153. height: 82rpx;
  154. border-radius: 50%;
  155. margin: auto;
  156. background-color: rgba(229, 117, 25, 0.5);
  157. z-index: 1;
  158. }
  159. image{
  160. width: 100%;
  161. }
  162. }
  163. .overlay-wrapper{
  164. height: 100%;
  165. video{
  166. width: 100%;
  167. }
  168. }
  169. .swiper-container{
  170. position: relative;
  171. }
  172. /* 自定义指示点样式 */
  173. .custom-indicator {
  174. position: absolute;
  175. bottom: 24rpx;
  176. width: 100%;
  177. display: flex;
  178. justify-content: center;
  179. margin-top: 10rpx;
  180. &.morphing {
  181. margin: 0 4rpx;
  182. .dot.active{
  183. width: 26rpx;
  184. border-radius: 4rpx;
  185. background-color: white;
  186. }
  187. }
  188. }
  189. .dot {
  190. width: 10rpx;
  191. height: 10rpx;
  192. border-radius: 50%;
  193. background-color: #D9D9D9;
  194. margin: 0 2.5rpx;
  195. &.active {
  196. background-color: $buttonPrimaryColor; /* 当前激活的指示点颜色 */
  197. }
  198. }
  199. </style>