index.vue 6.3 KB

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