index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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'">
  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: 'aspectFit' // 参考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.data.slice(0, this.limit);
  123. }
  124. },
  125. watch: {
  126. items(value) {
  127. this.data = value
  128. if (this.useComponentName === 'logoSwiper') {
  129. this.getLogoPage()
  130. }
  131. }
  132. },
  133. data() {
  134. return {
  135. data: [],
  136. showOverlay: false,
  137. videoUrl: '',
  138. current: 0,
  139. logoPage: 1,
  140. logoList: [],
  141. limit: 6
  142. }
  143. },
  144. created() {},
  145. mounted() {
  146. if (this.useComponentName === 'logoSwiper') {
  147. this.getLogoPage()
  148. }
  149. },
  150. methods: {
  151. onClickHideOverlay() {
  152. this.showOverlay = false
  153. },
  154. onSwiperChange(e) {
  155. this.current = e.detail.current
  156. },
  157. onImageClick(item) {
  158. if (item.url) {
  159. if (item.url.indexOf('.mp4') !== -1) {
  160. this.videoUrl = item.url
  161. this.showOverlay = true
  162. } else {
  163. this.navigateTo(item.href)
  164. }
  165. }
  166. },
  167. getLogoPage() {
  168. this.logoPage = Math.floor(this.data.length / 6 - 0.000000000001)
  169. console.log(this.data,this.logoPage)
  170. for (let i = 0; i <= this.logoPage; i++) {
  171. let starPage = i * 6
  172. let lastPage = (starPage + 5) < this.data.length - 1 ? (starPage + 5) : this.data.length - 1
  173. this.logoList[i] = []
  174. for (let k = starPage; k <= lastPage; k++) {
  175. this.logoList[i].push(this.data[k])
  176. }
  177. }
  178. console.log(this.logoList,this.useComponentName)
  179. }
  180. }
  181. }
  182. </script>
  183. <style lang="scss" scoped>
  184. .image-wrapper {
  185. position: relative;
  186. .video-play {
  187. @include display-flex-center;
  188. position: absolute;
  189. top: 0;
  190. bottom: 0;
  191. right: 0;
  192. left: 0;
  193. width: 82rpx;
  194. height: 82rpx;
  195. border-radius: 50%;
  196. margin: auto;
  197. background-color: rgba(229, 117, 25, 0.5);
  198. z-index: 1;
  199. .iconfont {
  200. color: #FFFFFF;
  201. font-size: 62rpx;
  202. }
  203. }
  204. image {
  205. width: 100%;
  206. }
  207. }
  208. .overlay-wrapper {
  209. @include display-flex-center;
  210. height: 100%;
  211. video {
  212. width: 100%;
  213. }
  214. }
  215. .swiper-container {
  216. position: relative;
  217. }
  218. /* 自定义指示点样式 */
  219. .custom-indicator {
  220. position: absolute;
  221. bottom: 24rpx;
  222. width: 100%;
  223. display: flex;
  224. justify-content: center;
  225. margin-top: 10rpx;
  226. &.logo-indicator {
  227. bottom: -34rpx;
  228. }
  229. &.morphing {
  230. .dot {
  231. margin: 0 5rpx;
  232. }
  233. .dot.active {
  234. width: 26rpx;
  235. border-radius: 8rpx;
  236. background-color: white;
  237. }
  238. }
  239. }
  240. .dot {
  241. width: 12rpx;
  242. height: 12rpx;
  243. border-radius: 50%;
  244. background-color: #D9D9D9;
  245. margin: 0 2.5rpx;
  246. &.active {
  247. background-color: $buttonPrimaryColor;
  248. /* 当前激活的指示点颜色 */
  249. }
  250. }
  251. </style>