index.vue 5.5 KB

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