app-swiper.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="u-swiper-wrap" :style="{
  3. borderRadius: `${borderRadius}rpx`
  4. }">
  5. <swiper @change="change" @animationfinish="animationfinish" :interval="interval" :circular="circular" :duration="duration" :autoplay="autoplay"
  6. :previous-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'" :next-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'"
  7. :style="{
  8. height: height + 'rpx'
  9. }">
  10. <swiper-item class="u-swiper-item" v-for="(item, index) in list" :key="index">
  11. <!-- #ifdef MP-->
  12. <view class="u-list-image-wrap" @tap.stop.prevent="listClick(index)" :class="[current != index ? 'u-list-scale' : '']" :style="{
  13. borderRadius: `${borderRadius}rpx`,
  14. transform: effect3d && current != index ? 'scaleY(0.7)' : 'scaleY(1)',
  15. margin: effect3d && current != index ? '0 0' : 0
  16. }">
  17. <app-jump-button :open_type="item.open_type"
  18. :url="item.url ? item.url : item.page_url"
  19. :params="item.params">
  20. <image class="u-swiper-image" :src="item[name]" :mode="imgMode"></image>
  21. <view v-if="title" class="u-swiper-title u-line-1" :style="{
  22. 'padding-bottom': titlePaddingBottom
  23. }">
  24. {{ item.title }}
  25. </view>
  26. </app-jump-button>
  27. </view>
  28. <!-- #endif-->
  29. <!--#ifdef H5-->
  30. <view class="u-list-image-wrap" @tap.stop.prevent="listClick(index)" :class="[current != index ? 'u-list-scale' : '']" :style="{
  31. borderRadius: `${borderRadius}rpx`,
  32. transform: effect3d && current != index ? 'scaleY(0.7)' : 'scaleY(1)',
  33. margin: effect3d && current != index ? '0 0' : 0
  34. }">
  35. <app-jump-button
  36. v-if="item.open_type !== 'app'"
  37. :open_type="item.open_type"
  38. :url="item.url ? item.url : item.page_url"
  39. :params="item.params">
  40. <image class="u-swiper-image" :src="item[name]" :mode="imgMode"></image>
  41. <view v-if="title" class="u-swiper-title u-line-1" :style="{
  42. 'padding-bottom': titlePaddingBottom
  43. }">
  44. {{ item.title }}
  45. </view>
  46. </app-jump-button>
  47. <view :id="item.id" v-if="item.open_type === 'app'" style="height: 100%;">
  48. </view>
  49. </view>
  50. <!--#endif-->
  51. </swiper-item>
  52. </swiper>
  53. <view class="u-swiper-indicator" :style="{
  54. top: indicatorPos == 'topLeft' || indicatorPos == 'topCenter' || indicatorPos == 'topRight' ? '12rpx' : 'auto',
  55. bottom: indicatorPos == 'bottomLeft' || indicatorPos == 'bottomCenter' || indicatorPos == 'bottomRight' ? '12rpx' : 'auto',
  56. justifyContent: justifyContent,
  57. padding: `0 ${effect3d ? '74rpx' : '24rpx'}`
  58. }">
  59. <block v-if="mode == 'rect'">
  60. <view class="u-indicator-item-rect" :class="{ 'u-indicator-item-rect-active': index == current }" v-for="(item, index) in list"
  61. :key="index"></view>
  62. </block>
  63. <block v-if="mode == 'dot'">
  64. <view class="u-indicator-item-dot" :class="{ 'u-indicator-item-dot-active': index == current }" v-for="(item, index) in list"
  65. :key="index"></view>
  66. </block>
  67. <block v-if="mode == 'round'">
  68. <view class="u-indicator-item-round" :class="{ 'u-indicator-item-round-active': index == current }" v-for="(item, index) in list"
  69. :key="index"></view>
  70. </block>
  71. <block v-if="mode == 'number'">
  72. <view class="u-indicator-item-number">{{ current + 1 }}/{{ list.length }}</view>
  73. </block>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. export default {
  79. name: "u-swiper",
  80. props: {
  81. list: {
  82. type: Array,
  83. default () {
  84. return [];
  85. }
  86. },
  87. // 是否显示title标题
  88. title: {
  89. type: Boolean,
  90. default: false
  91. },
  92. // 用户自定义的指示器的样式
  93. indicator: {
  94. type: Object,
  95. default () {
  96. return {};
  97. }
  98. },
  99. // 圆角值
  100. borderRadius: {
  101. type: [Number, String],
  102. default: 0
  103. },
  104. // 隔多久自动切换
  105. interval: {
  106. type: [String, Number],
  107. default: 3000
  108. },
  109. // 指示器的模式,rect|dot|number|round
  110. mode: {
  111. type: String,
  112. default: 'round'
  113. },
  114. // list的高度,单位rpx
  115. height: {
  116. type: [Number, String],
  117. default: 250
  118. },
  119. // 指示器的位置,topLeft|topCenter|topRight|bottomLeft|bottomCenter|bottomRight
  120. indicatorPos: {
  121. type: String,
  122. default: 'bottomCenter'
  123. },
  124. // 是否开启缩放效果
  125. effect3d: {
  126. type: Boolean,
  127. default: false
  128. },
  129. // 3D模式的情况下,激活item与前后item之间的距离,单位rpx
  130. effect3dPreviousMargin: {
  131. type: [Number, String],
  132. default: -10
  133. },
  134. // 是否自动播放
  135. autoplay: {
  136. type: Boolean,
  137. default: true
  138. },
  139. // 自动轮播时间间隔,单位ms
  140. duration: {
  141. type: [Number, String],
  142. default: 500
  143. },
  144. // 是否衔接滑动,即到最后一张时接着滑动,是否自动切换到第一张
  145. circular: {
  146. type: Boolean,
  147. default: true
  148. },
  149. // 图片的裁剪模式
  150. imgMode: {
  151. type: String,
  152. default: 'aspectFill'
  153. },
  154. // 从list数组中读取的图片的属性名
  155. name: {
  156. type: String,
  157. default: 'image'
  158. },
  159. // 背景颜色
  160. bgColor: {
  161. type: String,
  162. default: '#f3f4f6'
  163. }
  164. },
  165. watch: {
  166. // 如果外部的list发生变化,判断长度是否被修改,如果前后长度不一致,重置current值,避免溢出
  167. list: {
  168. handler(nVal, oVal) {
  169. oVal = oVal ? oVal: [];
  170. if(nVal.length !== oVal.length) this.current = 0;
  171. // #ifdef H5
  172. for (let i = 0; i < nVal.length; i++) {
  173. nVal[i].id = this.$utils.guid('app-swiper')
  174. if (nVal[i].open_type === 'app') {
  175. let height = uni.upx2px(this.height)+ 'px';
  176. let username = this.getUrlParam(nVal[i].page_url, 'username');
  177. let path = this.getUrlParam(nVal[i].page_url, 'path');
  178. let text = `<img src="${nVal[i].pic_url}" width="100%" height="${height}" />`;
  179. this.$utils.createWxOpenLaunchWeapp(nVal[i].id, username, path, text);
  180. }
  181. }
  182. // #endif
  183. },
  184. immediate: true
  185. }
  186. },
  187. data() {
  188. return {
  189. current: 0 // 当前活跃的swiper-item的index
  190. };
  191. },
  192. // #ifdef H5
  193. mounted() {
  194. this.$jwx.config({});
  195. },
  196. // #endif
  197. computed: {
  198. justifyContent() {
  199. if (this.indicatorPos == 'topLeft' || this.indicatorPos == 'bottomLeft') return 'flex-start';
  200. if (this.indicatorPos == 'topCenter' || this.indicatorPos == 'bottomCenter') return 'center';
  201. if (this.indicatorPos == 'topRight' || this.indicatorPos == 'bottomRight') return 'flex-end';
  202. },
  203. titlePaddingBottom() {
  204. let tmp = 0;
  205. if (this.mode == 'none') return '12rpx';
  206. if (['bottomLeft', 'bottomCenter', 'bottomRight'].indexOf(this.indicatorPos) >= 0 && this.mode == 'number') {
  207. tmp = '60rpx';
  208. } else if (['bottomLeft', 'bottomCenter', 'bottomRight'].indexOf(this.indicatorPos) >= 0 && this.mode != 'number') {
  209. tmp = '40rpx';
  210. } else {
  211. tmp = '12rpx';
  212. }
  213. return tmp;
  214. },
  215. // #ifdef H5
  216. isWechat: function() {
  217. return this.$jwx.isWechat();
  218. }
  219. // #endif
  220. },
  221. methods: {
  222. listClick(index) {
  223. this.$emit('click', index);
  224. },
  225. change(e) {
  226. let current = e.detail.current;
  227. this.current = current;
  228. // 发出change事件,表示当前自动切换的index,从0开始
  229. this.$emit('change', current);
  230. },
  231. // 头条小程序不支持animationfinish事件,改由change事件
  232. // 暂不监听此事件,因为不再给swiper绑定current属性
  233. animationfinish(e) {
  234. // #ifndef MP-TOUTIAO
  235. // this.current = e.detail.current;
  236. // #endif
  237. },
  238. getUrlParam(url,name) {
  239. let search = url.split('?')[1];
  240. if (search) {
  241. let r = search.substr(0).match(new RegExp('(^|&)' + name + '=([^&]*)(&|$)'))
  242. console.log(r);
  243. if (r !== null) {
  244. console.log(unescape(r[2]));
  245. return unescape(r[2])
  246. }
  247. return null
  248. } else {
  249. return null
  250. }
  251. }
  252. },
  253. filters: {
  254. // getUrlParam(url, name) {
  255. // let search = url.split('?')[1];
  256. // if (search) {
  257. // let r = search.substr(0).match(new RegExp('(^|&)' + name + '=([^&]*)(&|$)'))
  258. // if (r !== null) return unescape(r[2])
  259. // return null
  260. // } else {
  261. // return null
  262. // }
  263. // }
  264. }
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. .u-swiper-wrap {
  269. position: relative;
  270. overflow: hidden;
  271. transform: translateY(0);
  272. }
  273. .u-swiper-image {
  274. width: 100%;
  275. will-change: transform;
  276. height: 100%;
  277. display: block;
  278. /* #ifdef H5 */
  279. pointer-events: none;
  280. /* #endif */
  281. }
  282. .u-swiper-indicator {
  283. padding: 0 24rpx;
  284. position: absolute;
  285. display: flex;
  286. width: 100%;
  287. z-index: 1;
  288. }
  289. .u-indicator-item-rect {
  290. width: 26rpx;
  291. height: 8rpx;
  292. margin: 0 6rpx;
  293. transition: all 0.5s;
  294. background-color: rgba(0, 0, 0, 0.3);
  295. }
  296. .u-indicator-item-rect-active {
  297. background-color: rgba(255, 255, 255, 0.8);
  298. }
  299. .u-indicator-item-dot {
  300. width: 14rpx;
  301. height: 14rpx;
  302. margin: 0 6rpx;
  303. border-radius: 20rpx;
  304. transition: all 0.5s;
  305. background-color: rgba(0, 0, 0, 0.3);
  306. }
  307. .u-indicator-item-dot-active {
  308. background-color: rgba(255, 255, 255, 0.8);
  309. }
  310. .u-indicator-item-round {
  311. width: 14rpx;
  312. height: 14rpx;
  313. margin: 0 6rpx;
  314. border-radius: 20rpx;
  315. transition: all 0.5s;
  316. background-color: rgba(0, 0, 0, 0.3);
  317. }
  318. .u-indicator-item-round-active {
  319. width: 34rpx;
  320. background-color: rgba(255, 255, 255, 0.8);
  321. }
  322. .u-indicator-item-number {
  323. padding: 6rpx 16rpx;
  324. line-height: 1;
  325. background-color: rgba(0, 0, 0, 0.3);
  326. border-radius: 100rpx;
  327. font-size: 26rpx;
  328. color: rgba(255, 255, 255, 0.8);
  329. }
  330. .u-list-scale {
  331. transform-origin: center center;
  332. }
  333. .u-list-image-wrap {
  334. height: 100%;
  335. flex: 1;
  336. transition: all 0.5s;
  337. overflow: hidden;
  338. box-sizing: content-box;
  339. position: relative;
  340. }
  341. .u-swiper-title {
  342. position: absolute;
  343. background-color: rgba(0, 0, 0, 0.3);
  344. bottom: 0;
  345. left: 0;
  346. width: 100%;
  347. font-size: 28rpx;
  348. padding: 12rpx 24rpx;
  349. color: rgba(255, 255, 255, 0.9);
  350. }
  351. .u-swiper-item {
  352. display: flex;
  353. overflow: hidden;
  354. align-items: center;
  355. }
  356. </style>