1
0

app-swiper.vue 9.6 KB

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