index.vue 5.8 KB

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