u-swiper.vue 11 KB

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