swiper.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template name="dever-swiper">
  2. <view class="index-box">
  3. <swiper class="swiper" :display-multiple-items="num" @change="change" :vertical="vertical" :circular="circular" :current="swiper_current" :autoplay="autoplay" :interval="interval" :previous-margin="previous_margin" :next-margin="next_margin">
  4. <swiper-item v-for="(v, k) in getData" :key="k" style="overflow: unset;">
  5. <slot :k="getKey(k)" :v="v" :i="k"></slot>
  6. </swiper-item>
  7. </swiper>
  8. </view>
  9. </template>
  10. <script>
  11. // 无限轮播组件,参考https://blog.csdn.net/qq_40026668/article/details/122236668
  12. export default {
  13. props: {
  14. //传入的数据
  15. item : {
  16. type : Array,
  17. default : []
  18. },
  19. //同时显示的滑块数量
  20. num : {
  21. type : Number,
  22. default : 1
  23. },
  24. //是否可以循环滚动
  25. circular : {
  26. type : Boolean,
  27. default : true
  28. },
  29. //设置从第几个开始滚动
  30. current : {
  31. type : Number,
  32. default : 0
  33. },
  34. //是否自动切换
  35. autoplay : {
  36. type : Boolean,
  37. default : false
  38. },
  39. //自动切换时间间隔
  40. interval : {
  41. type : Number,
  42. default : 5000
  43. },
  44. //是否竖向
  45. vertical : {
  46. type : Boolean,
  47. default : false
  48. },
  49. //前边距
  50. previous_margin : {
  51. type : String,
  52. default : '0px'
  53. },
  54. //后边距
  55. next_margin : {
  56. type : String,
  57. default : '0px'
  58. },
  59. },
  60. watch: {
  61. current: {
  62. handler(current, old) {
  63. this.init(current);
  64. this.finish();
  65. },
  66. //deep: true // 深度监听父组件传过来对象变化
  67. }
  68. },
  69. data() {
  70. return {
  71. length : 3, // 总的轮播图数量
  72. swiper_current : 0,
  73. swiper_index : 0, // 当前轮播图激活索引
  74. data_index : 0, // 当前展示数据在列表中的索引值
  75. data : new Array(3),
  76. }
  77. },
  78. created() {
  79. this.init(this.current);
  80. },
  81. computed: {
  82. getData() {
  83. // 获取当前值、下一个值、上一个值
  84. var current_k = this.data_index;
  85. var next_k = this.getDataIndex(this.data_index + 1);
  86. var prev_k = this.getDataIndex(this.data_index - 1);
  87. let current = this.item[current_k]
  88. let next = this.item[next_k]
  89. let prev = this.item[prev_k]
  90. if (typeof(current) == 'object') {
  91. current.k = current_k
  92. next.k = next_k
  93. prev.k = prev_k
  94. }
  95. // 获取当前轮播索引对应的值、下个索引对应的值、上个索引对应的值
  96. this.data[this.swiper_index] = current
  97. this.data[this.getIndex(this.swiper_index + 1)] = next
  98. this.data[this.getIndex(this.swiper_index - 1)] = prev
  99. return this.data
  100. },
  101. },
  102. methods: {
  103. init : function(current) {
  104. this.swiper_index = this.getSwiperIndex(current, 3);
  105. this.data_index = current;
  106. this.swiper_current = this.swiper_index;
  107. },
  108. getKey : function(k) {
  109. if (typeof(this.data[k]) == 'object') {
  110. return this.data[k].k;
  111. } else {
  112. return k;
  113. }
  114. },
  115. change : function(event) {
  116. let current = Number(event.detail.current)
  117. if (current != this.swiper_index) {
  118. if ([1, 1 - this.length].includes(current - this.swiper_index)) {
  119. // 向左滑动
  120. this.data_index = this.getDataIndex(this.data_index + 1)
  121. } else {
  122. // 向右滑动
  123. this.data_index = this.getDataIndex(this.data_index - 1)
  124. }
  125. }
  126. this.swiper_index = current
  127. this.finish()
  128. },
  129. finish : function() {
  130. this.$emit('change', this.data_index, this.swiper_index);
  131. },
  132. getDataIndex : function(index) {
  133. if (index < 0) {
  134. // 小于零,返回数据列表末尾索引
  135. return this.item.length - 1
  136. } else if (index >= this.item.length) {
  137. // 等于(或大于,一般不会)数据列表长度,返回数据首位索引
  138. return 0
  139. } else {
  140. return index
  141. }
  142. },
  143. getIndex : function(index) {
  144. if (index < 0) {
  145. return this.length - 1
  146. } else if (index >= this.length) {
  147. return 0
  148. } else {
  149. return index
  150. }
  151. },
  152. getSwiperIndex : function(current, length) {
  153. if (current < (length - 1)) {
  154. return current
  155. } else {
  156. return current % length
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped lang="scss">
  163. .swiper {
  164. width: 750rpx;
  165. height: 100%;
  166. }
  167. swiper-item>view{
  168. height: 100%;
  169. }
  170. swiper-item image{
  171. width: 750rpx;
  172. height: 100%;
  173. }
  174. </style>