swiper.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view class="banner" :style="{height: height + 'rpx'}">
  3. <view v-for="(item, index) in list" :key="index" class="pic"
  4. :animation="num === index ?showpic:hidepic"
  5. :style="{zIndex: zIndex === index ?1:0}"
  6. @touchstart="touchstart" @touchend="touchend">
  7. <app-jump-button
  8. :open_type="item.open_type"
  9. :url="item.url ? item.url : item.page_url"
  10. :params="item.params">
  11. <image :style="{height: height + 'rpx', width: '100%'}" :mode="imgMode" :src="item[name]" />
  12. </app-jump-button>
  13. </view>
  14. <view class="u-swiper-indicator"
  15. :style="{
  16. bottom: '12rpx',
  17. justifyContent: 'center'
  18. }"
  19. >
  20. <block v-if="mode === 'rect'">
  21. <view class="u-indicator-item-rect" :class="{ 'u-indicator-item-rect-active': index === num }" v-for="(item, index) in list"
  22. :key="index"></view>
  23. </block>
  24. <block v-if="mode === 'round'">
  25. <view class="u-indicator-item-round" :class="{ 'u-indicator-item-round-active': index === num }" v-for="(item, index) in list"
  26. :key="index"></view>
  27. </block>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. name:'u-swiper',
  34. props: {
  35. list: {
  36. type: Array,
  37. default () {
  38. return [];
  39. }
  40. },
  41. // 图片的裁剪模式
  42. imgMode: {
  43. type: String,
  44. default: 'aspectFill'
  45. },
  46. // 从list数组中读取的图片的属性名
  47. name: {
  48. type: String,
  49. default: 'image'
  50. },
  51. // list的高度,单位rpx
  52. height: {
  53. type: [Number, String],
  54. default: 250
  55. },
  56. // 自动轮播时间间隔,单位ms
  57. duration: {
  58. type: [Number, String],
  59. default: 500
  60. },
  61. // 隔多久自动切换
  62. interval: {
  63. type: [String, Number],
  64. default: 3000
  65. },
  66. // 指示器的模式,rect|dot|number|round
  67. mode: {
  68. type: String,
  69. default: 'round'
  70. },
  71. // 指示器的位置,topLeft|topCenter|topRight|bottomLeft|bottomCenter|bottomRight
  72. indicatorPos: {
  73. type: String,
  74. default: 'bottomCenter'
  75. },
  76. // 是否自动播放
  77. autoplay: {
  78. type: Boolean,
  79. default: true
  80. },
  81. },
  82. data() {
  83. return {
  84. num: 0,
  85. zIndex: 0,
  86. showpic:null,
  87. hidepic:null,
  88. setTime: 0,
  89. touchDot: 0
  90. }
  91. },
  92. methods: {
  93. touchstart(e) {
  94. this.touchDot = e.touches[0].pageX;
  95. },
  96. touchend(e) {
  97. let touchMove = e.changedTouches[0].pageX;
  98. if (touchMove - this.touchDot <= -40) {
  99. this.num = this.num - 1;
  100. if(this.num < 0){
  101. this.num=this.list.length-1;
  102. }
  103. setTimeout(() => {
  104. this.zIndex = this.num;
  105. }, 1000);
  106. }
  107. if (touchMove - this.touchDot >= 40) {
  108. this.num = this.num + 1;
  109. if(this.num>=this.list.length){
  110. this.num=0;
  111. }
  112. setTimeout(() => {
  113. this.zIndex = this.num;
  114. }, 1000);
  115. }
  116. }
  117. },
  118. destroyed() {
  119. clearInterval(this.setTime);
  120. },
  121. watch: {
  122. list: {
  123. handler(newVal) {
  124. if (newVal.length === 0) return;
  125. let animation= uni.createAnimation({}) //创建一个动画实例
  126. //淡入
  127. animation.opacity(1).step({
  128. duration:this.duration
  129. }) //描述动画
  130. this.showpic = animation.export();
  131. //淡出
  132. animation.opacity(0).step({duration:this.duration})
  133. this.hidepic = animation.export();
  134. if (this.autoplay) {
  135. this.setTime = setInterval(() => {
  136. this.num = this.num+1;
  137. if(this.num>=this.list.length){
  138. this.num=0;
  139. }
  140. setTimeout(() => {
  141. this.zIndex = this.num;
  142. }, this.interval + 1000)
  143. }, this.interval);
  144. }
  145. },
  146. immediate: true
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss">
  152. .banner {
  153. width: 750rpx;
  154. position: relative;
  155. }
  156. .pic {
  157. width:750rpx;
  158. position:absolute;
  159. top:0;
  160. left:0;
  161. }
  162. .u-swiper-indicator {
  163. padding: 0 24rpx;
  164. position: absolute;
  165. display: flex;
  166. width: 100%;
  167. z-index: 1;
  168. }
  169. .u-indicator-item-round-active {
  170. width: 34rpx !important;
  171. background-color: rgba(255, 255, 255, 0.8) !important;
  172. }
  173. .u-indicator-item-round {
  174. width: 14rpx;
  175. height: 14rpx;
  176. margin: 0 6rpx;
  177. border-radius: 20rpx;
  178. transition: all 0.5s;
  179. background-color: rgba(0, 0, 0, 0.3);
  180. }
  181. .u-indicator-item-rect {
  182. width: 26rpx;
  183. height: 8rpx;
  184. margin: 0 6rpx;
  185. transition: all 0.5s;
  186. background-color: rgba(0, 0, 0, 0.3);
  187. }
  188. .u-indicator-item-rect-active {
  189. background-color: rgba(255, 255, 255, 0.8);
  190. }
  191. </style>