swiper.vue 5.4 KB

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