u-swiper.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view
  3. class="u-swiper"
  4. :style="{
  5. backgroundColor: bgColor,
  6. height: $u.addUnit(height),
  7. borderRadius: $u.addUnit(radius)
  8. }"
  9. >
  10. <view class="u-swiper__loading" v-if="loading">
  11. <u-loading-icon mode="circle"></u-loading-icon>
  12. </view>
  13. <swiper
  14. v-else
  15. class="u-swiper__wrapper"
  16. :style="{
  17. height: $u.addUnit(height)
  18. }"
  19. @change="change"
  20. :circular="circular"
  21. :interval="interval"
  22. :duration="duration"
  23. :autoplay="autoplay"
  24. :current="current"
  25. :currentItemId="currentItemId"
  26. :previousMargin="$u.addUnit(previousMargin)"
  27. :nextMargin="$u.addUnit(nextMargin)"
  28. :acceleration="acceleration"
  29. :displayMultipleItems="displayMultipleItems"
  30. :easingFunction="easingFunction"
  31. >
  32. <swiper-item class="u-swiper__wrapper__item" v-for="(item, index) in list" :key="index">
  33. <view class="u-swiper__wrapper__item__wrapper" :style="[itemStyle(index)]">
  34. <!-- 在nvue中,image图片的宽度默认为屏幕宽度,需要通过flex:1撑开,另外必须设置高度才能显示图片 -->
  35. <image
  36. class="u-swiper__wrapper__item__wrapper__image"
  37. v-if="getItemType(item) === 'image'"
  38. :src="getSource(item)"
  39. :mode="imgMode"
  40. @tap="clickHandler(index)"
  41. :style="{
  42. height: $u.addUnit(height),
  43. borderRadius: $u.addUnit(radius)
  44. }"
  45. ></image>
  46. <video
  47. class="u-swiper__wrapper__item__wrapper__video"
  48. v-if="getItemType(item) === 'video'"
  49. :id="`video-${index}`"
  50. :enable-progress-gesture="false"
  51. :src="getSource(item)"
  52. :poster="getPoster(item)"
  53. :title="showTitle && $u.test.object(item) && item.title ? item.title : ''"
  54. :style="{
  55. height: $u.addUnit(height)
  56. }"
  57. controls
  58. @tap="clickHandler(index)"
  59. ></video>
  60. <text v-if="showTitle && $u.test.object(item) && item.title && $u.test.image(getSource(item))" class="u-swiper__wrapper__item__wrapper__title u-line-1">
  61. {{ item.title }}
  62. </text>
  63. <view v-if="showContent" class="u-swiper__wrapper__item__wrapper__content" @click="toDetail(index)">
  64. <view class="fine_bottom">
  65. <view class="fine_bottom_content">
  66. <view class="fine_name u-line-1">{{ item.name }}</view>
  67. <view class="fine_user">
  68. <u-avatar-group :urls="item.member.map(item => item.avatar)" size="30rpx" gap="0.4"></u-avatar-group>
  69. <view class="fine_user_text">{{ item.number_num }}人已加入</view>
  70. </view>
  71. </view>
  72. <view @click.stop="circleEnter(index)" class="fine_btn" v-if="item.is_enter === 0">加入</view>
  73. </view>
  74. </view>
  75. </view>
  76. </swiper-item>
  77. </swiper>
  78. <view class="u-swiper__indicator" :style="[$u.addStyle(indicatorStyle)]">
  79. <slot name="indicator">
  80. <u-swiper-indicator
  81. v-if="!loading && indicator && !showTitle"
  82. :indicatorActiveColor="indicatorActiveColor"
  83. :indicatorInactiveColor="indicatorInactiveColor"
  84. :length="list.length"
  85. :current="currentIndex"
  86. :indicatorMode="indicatorMode"
  87. ></u-swiper-indicator>
  88. </slot>
  89. </view>
  90. </view>
  91. </template>
  92. <script>
  93. import props from './props.js'
  94. /**
  95. * Swiper 轮播图
  96. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  97. * @tutorial https://www.uviewui.com/components/swiper.html
  98. * @property {Array} list 轮播图数据
  99. * @property {Boolean} indicator 是否显示面板指示器(默认 false )
  100. * @property {String} indicatorActiveColor 指示器非激活颜色(默认 '#FFFFFF' )
  101. * @property {String} indicatorInactiveColor 指示器的激活颜色(默认 'rgba(255, 255, 255, 0.35)' )
  102. * @property {String | Object} indicatorStyle 指示器样式,可通过bottom,left,right进行定位
  103. * @property {String} indicatorMode 指示器模式(默认 'line' )
  104. * @property {Boolean} autoplay 是否自动切换(默认 true )
  105. * @property {String | Number} current 当前所在滑块的 index(默认 0 )
  106. * @property {String} currentItemId 当前所在滑块的 item-id ,不能与 current 被同时指定
  107. * @property {String | Number} interval 滑块自动切换时间间隔(ms)(默认 3000 )
  108. * @property {String | Number} duration 滑块切换过程所需时间(ms)(默认 300 )
  109. * @property {Boolean} circular 播放到末尾后是否重新回到开头(默认 false )
  110. * @property {String | Number} previousMargin 前边距,可用于露出前一项的一小部分,nvue和支付宝不支持(默认 0 )
  111. * @property {String | Number} nextMargin 后边距,可用于露出后一项的一小部分,nvue和支付宝不支持(默认 0 )
  112. * @property {Boolean} acceleration 当开启时,会根据滑动速度,连续滑动多屏,支付宝不支持(默认 false )
  113. * @property {Number} displayMultipleItems 同时显示的滑块数量,nvue、支付宝小程序不支持(默认 1 )
  114. * @property {String} easingFunction 指定swiper切换缓动动画类型, 只对微信小程序有效(默认 'default' )
  115. * @property {String} keyName list数组中指定对象的目标属性名(默认 'url' )
  116. * @property {String} imgMode 图片的裁剪模式(默认 'aspectFill' )
  117. * @property {String | Number} height 组件高度(默认 130 )
  118. * @property {String} bgColor 背景颜色(默认 '#f3f4f6' )
  119. * @property {String | Number} radius 组件圆角,数值或带单位的字符串(默认 4 )
  120. * @property {Boolean} loading 是否加载中(默认 false )
  121. * @property {Boolean} showTitle 是否显示标题,要求数组对象中有title属性(默认 false )
  122. * @event {Function(index)} click 点击轮播图时触发 index:点击了第几张图片,从0开始
  123. * @event {Function(index)} change 轮播图切换时触发(自动或者手动切换) index:切换到了第几张图片,从0开始
  124. * @example <u-swiper :list="list4" keyName="url" :autoplay="false"></u-swiper>
  125. */
  126. export default {
  127. name: 'u-swiper',
  128. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  129. data() {
  130. return {
  131. currentIndex: 0
  132. }
  133. },
  134. watch: {
  135. current(val, preVal) {
  136. if (val === preVal) return
  137. this.currentIndex = val // 和上游数据关联上
  138. }
  139. },
  140. computed: {
  141. itemStyle() {
  142. return (index) => {
  143. const style = {}
  144. // #ifndef APP-NVUE || MP-TOUTIAO
  145. // 左右流出空间的写法不支持nvue和头条
  146. // 只有配置了此二值,才加上对应的圆角,以及缩放
  147. if (this.nextMargin && this.previousMargin) {
  148. style.borderRadius = uni.$u.addUnit(this.radius)
  149. if (index !== this.currentIndex) style.transform = 'scale(0.92)'
  150. }
  151. // #endif
  152. return style
  153. }
  154. }
  155. },
  156. methods: {
  157. getItemType(item) {
  158. if (typeof item === 'string') return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  159. if (typeof item === 'object' && this.keyName) {
  160. if (!item.type) return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  161. if (item.type === 'image') return 'image'
  162. if (item.type === 'video') return 'video'
  163. return 'image'
  164. }
  165. },
  166. // 获取目标路径,可能数组中为字符串,对象的形式,额外可指定对象的目标属性名keyName
  167. getSource(item) {
  168. if (typeof item === 'string') return item
  169. if (typeof item === 'object' && this.keyName) return item[this.keyName]
  170. else uni.$u.error('请按格式传递列表参数')
  171. return ''
  172. },
  173. // 轮播切换事件
  174. change(e) {
  175. // 当前的激活索引
  176. const { current } = e.detail
  177. this.pauseVideo(this.currentIndex)
  178. this.currentIndex = current
  179. this.$emit('change', e.detail)
  180. },
  181. // 切换轮播时,暂停视频播放
  182. pauseVideo(index) {
  183. const lastItem = this.getSource(this.list[index])
  184. if (uni.$u.test.video(lastItem)) {
  185. // 当视频隐藏时,暂停播放
  186. const video = uni.createVideoContext(`video-${index}`, this)
  187. video.pause()
  188. }
  189. },
  190. // 当一个轮播item为视频时,获取它的视频海报
  191. getPoster(item) {
  192. return typeof item === 'object' && item.poster ? item.poster : ''
  193. },
  194. // 点击某个item
  195. clickHandler(index) {
  196. this.$emit('click', index)
  197. },
  198. // 详情
  199. toDetail(index) {
  200. this.$emit('toDetail', index)
  201. },
  202. // 新增加入
  203. circleEnter(index) {
  204. this.$emit('circleEnter', index)
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. @import '../../libs/css/components.scss';
  211. .u-swiper {
  212. @include flex;
  213. justify-content: center;
  214. align-items: center;
  215. position: relative;
  216. overflow: hidden;
  217. &__wrapper {
  218. flex: 1;
  219. &__item {
  220. flex: 1;
  221. &__wrapper {
  222. @include flex;
  223. position: relative;
  224. overflow: hidden;
  225. transition: transform 0.3s;
  226. flex: 1;
  227. &__image {
  228. flex: 1;
  229. }
  230. &__video {
  231. flex: 1;
  232. }
  233. &__title {
  234. position: absolute;
  235. background-color: rgba(0, 0, 0, 0.3);
  236. bottom: 0;
  237. left: 0;
  238. right: 0;
  239. font-size: 28rpx;
  240. padding: 12rpx 24rpx;
  241. color: #ffffff;
  242. flex: 1;
  243. }
  244. &__content {
  245. position: absolute;
  246. top: 0;
  247. left: 0;
  248. z-index: 10;
  249. width: 100%;
  250. height: 100%;
  251. display: flex;
  252. flex-direction: column;
  253. justify-content: flex-end;
  254. padding: 12rpx 0 10rpx 0;
  255. .fine_top {
  256. display: flex;
  257. flex-direction: row;
  258. justify-content: center;
  259. align-items: center;
  260. width: 100rpx;
  261. height: 48rpx;
  262. background: rgba(0, 0, 0, 0.6);
  263. border-radius: 24rpx;
  264. margin-left: 16rpx;
  265. .fine_top_text {
  266. font-size: 20rpx;
  267. color: #ffffff;
  268. margin-left: 10rpx;
  269. }
  270. }
  271. .fine_bottom {
  272. margin: 0 auto;
  273. width: 580rpx;
  274. height: 100rpx;
  275. background: #ffffff;
  276. border-radius: 20rpx;
  277. padding: 0 20rpx;
  278. display: flex;
  279. flex-direction: row;
  280. align-items: center;
  281. justify-content: space-between;
  282. .fine_bottom_content {
  283. width: 400rpx;
  284. .fine_name {
  285. font-weight: bold;
  286. font-size: 26rpx;
  287. color: #323232;
  288. line-height: 36rpx;
  289. margin-bottom: 4rpx;
  290. }
  291. .fine_user {
  292. display: flex;
  293. flex-direction: row;
  294. align-items: center;
  295. height: 40rpx;
  296. font-size: 20rpx;
  297. color: #323232;
  298. .fine_user_text {
  299. margin-left: 10rpx;
  300. }
  301. }
  302. }
  303. .fine_btn {
  304. width: 110rpx;
  305. height: 54rpx;
  306. background: #d59b4d;
  307. border-radius: 27rpx;
  308. text-align: center;
  309. line-height: 54rpx;
  310. font-size: 24rpx;
  311. font-weight: bold;
  312. color: #ffffff;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. &__indicator {
  320. position: absolute;
  321. bottom: 10px;
  322. }
  323. }
  324. </style>