swiper.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view>
  3. <cu-custom bgColor="bg-gradual-pink" :isBack="true"><block slot="backText">返回</block>
  4. <block slot="content">轮播图</block>
  5. </cu-custom>
  6. <view class="cu-bar bg-white">
  7. <view class="action">
  8. <text class="cuIcon-title text-pink"></text> 全屏限高轮播
  9. </view>
  10. <view class="action">
  11. <switch @change="DotStyle" :class="dotStyle?'checked':''" :checked="dotStyle?true:false"></switch>
  12. </view>
  13. </view>
  14. <swiper class="screen-swiper" :class="dotStyle?'square-dot':'round-dot'" :indicator-dots="true" :circular="true"
  15. :autoplay="true" interval="5000" duration="500">
  16. <swiper-item v-for="(item,index) in swiperList" :key="index">
  17. <image :src="item.url" mode="aspectFill" v-if="item.type=='image'"></image>
  18. <video :src="item.url" autoplay loop muted :show-play-btn="false" :controls="false" objectFit="cover" v-if="item.type=='video'"></video>
  19. </swiper-item>
  20. </swiper>
  21. <!-- #ifndef MP-ALIPAY -->
  22. <view class="cu-bar bg-white margin-top">
  23. <view class="action">
  24. <text class="cuIcon-title text-pink"></text> 卡片式轮播
  25. </view>
  26. </view>
  27. <swiper class="card-swiper" :class="dotStyle?'square-dot':'round-dot'" :indicator-dots="true" :circular="true"
  28. :autoplay="true" interval="5000" duration="500" @change="cardSwiper" indicator-color="#8799a3"
  29. indicator-active-color="#0081ff">
  30. <swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
  31. <view class="swiper-item">
  32. <image :src="item.url" mode="aspectFill" v-if="item.type=='image'"></image>
  33. <video :src="item.url" autoplay loop muted :show-play-btn="false" :controls="false" objectFit="cover" v-if="item.type=='video'"></video>
  34. </view>
  35. </swiper-item>
  36. </swiper>
  37. <view class="cu-bar bg-white margin-top">
  38. <view class="action">
  39. <text class="cuIcon-title text-pink"></text> 堆叠式轮播
  40. </view>
  41. </view>
  42. <view class="tower-swiper" @touchmove="TowerMove" @touchstart="TowerStart" @touchend="TowerEnd">
  43. <view class="tower-item" :class="item.zIndex==1?'none':''" v-for="(item,index) in swiperList" :key="index" :style="[{'--index': item.zIndex,'--left':item.mLeft}]" :data-direction="direction">
  44. <view class="swiper-item">
  45. <image :src="item.url" mode="aspectFill" v-if="item.type=='image'"></image>
  46. <video :src="item.url" autoplay loop muted :show-play-btn="false" :controls="false" objectFit="cover" v-if="item.type=='video'"></video>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- #endif -->
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. cardCur: 0,
  58. swiperList: [{
  59. id: 0,
  60. type: 'image',
  61. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84000.jpg'
  62. }, {
  63. id: 1,
  64. type: 'image',
  65. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big37006.jpg',
  66. }, {
  67. id: 2,
  68. type: 'image',
  69. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
  70. }, {
  71. id: 3,
  72. type: 'image',
  73. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
  74. }, {
  75. id: 4,
  76. type: 'image',
  77. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
  78. }, {
  79. id: 5,
  80. type: 'image',
  81. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
  82. }, {
  83. id: 6,
  84. type: 'image',
  85. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
  86. }],
  87. dotStyle: false,
  88. towerStart: 0,
  89. direction: ''
  90. };
  91. },
  92. onLoad() {
  93. this.TowerSwiper('swiperList');
  94. // 初始化towerSwiper 传已有的数组名即可
  95. },
  96. methods: {
  97. DotStyle(e) {
  98. this.dotStyle = e.detail.value
  99. },
  100. // cardSwiper
  101. cardSwiper(e) {
  102. this.cardCur = e.detail.current
  103. },
  104. // towerSwiper
  105. // 初始化towerSwiper
  106. TowerSwiper(name) {
  107. let list = this[name];
  108. for (let i = 0; i < list.length; i++) {
  109. list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
  110. list[i].mLeft = i - parseInt(list.length / 2)
  111. }
  112. this.swiperList = list
  113. },
  114. // towerSwiper触摸开始
  115. TowerStart(e) {
  116. this.towerStart = e.touches[0].pageX
  117. },
  118. // towerSwiper计算方向
  119. TowerMove(e) {
  120. this.direction = e.touches[0].pageX - this.towerStart > 0 ? 'right' : 'left'
  121. },
  122. // towerSwiper计算滚动
  123. TowerEnd(e) {
  124. let direction = this.direction;
  125. let list = this.swiperList;
  126. if (direction == 'right') {
  127. let mLeft = list[0].mLeft;
  128. let zIndex = list[0].zIndex;
  129. for (let i = 1; i < this.swiperList.length; i++) {
  130. this.swiperList[i - 1].mLeft = this.swiperList[i].mLeft
  131. this.swiperList[i - 1].zIndex = this.swiperList[i].zIndex
  132. }
  133. this.swiperList[list.length - 1].mLeft = mLeft;
  134. this.swiperList[list.length - 1].zIndex = zIndex;
  135. } else {
  136. let mLeft = list[list.length - 1].mLeft;
  137. let zIndex = list[list.length - 1].zIndex;
  138. for (let i = this.swiperList.length - 1; i > 0; i--) {
  139. this.swiperList[i].mLeft = this.swiperList[i - 1].mLeft
  140. this.swiperList[i].zIndex = this.swiperList[i - 1].zIndex
  141. }
  142. this.swiperList[0].mLeft = mLeft;
  143. this.swiperList[0].zIndex = zIndex;
  144. }
  145. this.direction = ""
  146. this.swiperList = this.swiperList
  147. },
  148. }
  149. }
  150. </script>
  151. <style>
  152. .tower-swiper .tower-item {
  153. transform: scale(calc(0.5 + var(--index) / 10));
  154. margin-left: calc(var(--left) * 100upx - 150upx);
  155. z-index: var(--index);
  156. }
  157. </style>