swiper.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template name="dever-swiper">
  2. <view class="index-box">
  3. <swiper class="swiper" @change="change" :circular="circular" :current="current" :autoplay="autoplay" :interval="interval">
  4. <swiper-item v-for="(v, k) in getData" :key="k" >
  5. <slot :v="v" :k="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. value : []
  18. },
  19. //是否可以循环滚动
  20. circular : {
  21. type : Boolean,
  22. value : true
  23. },
  24. //设置从第几个开始滚动
  25. current : {
  26. type : Number,
  27. value : 0
  28. },
  29. //是否自动切换
  30. autoplay : {
  31. type : Boolean,
  32. value : false
  33. },
  34. //自动切换时间间隔
  35. interval : {
  36. type : Number,
  37. value : 5000
  38. }
  39. },
  40. data() {
  41. return {
  42. swiper_index : 0, // 当前轮播图激活索引
  43. data_index : 0, // 当前展示数据在列表中的索引值
  44. length : 3, // 总的轮播图数量
  45. }
  46. },
  47. mounted() {
  48. this.data_index = this.current;
  49. },
  50. computed: {
  51. getData() {
  52. // 获取当前值、下一个值、上一个值
  53. let current = this.item[this.data_index]
  54. let next = this.item[this.getDataIndex(this.data_index + 1)]
  55. let prev = this.item[this.getDataIndex(this.data_index - 1)]
  56. // 获取当前轮播索引对应的值、下个索引对应的值、上个索引对应的值
  57. let list = new Array(3)
  58. list[this.swiper_index] = current
  59. list[this.getIndex(this.swiper_index + 1)] = next
  60. list[this.getIndex(this.swiper_index - 1)] = prev
  61. return list
  62. }
  63. },
  64. methods: {
  65. change : function(event) {
  66. let current = Number(event.detail.current)
  67. if ([1, 1 - this.length].includes(current - this.swiper_index)) {
  68. // 向左滑动
  69. this.data_index = this.getDataIndex(this.data_index + 1)
  70. } else {
  71. // 向右滑动
  72. this.data_index = this.getDataIndex(this.data_index - 1)
  73. }
  74. this.swiper_index = current
  75. //console.info(this.data_index, this.swiper_index)
  76. this.$emit('change', this.data_index, this.swiper_index);
  77. },
  78. getDataIndex : function(index) {
  79. if (index < 0) {
  80. // 小于零,返回数据列表末尾索引
  81. return this.item.length - 1
  82. } else if (index >= this.item.length) {
  83. // 等于(或大于,一般不会)数据列表长度,返回数据首位索引
  84. return 0
  85. } else {
  86. return index
  87. }
  88. },
  89. getIndex : function(index) {
  90. if (index < 0) {
  91. return this.length - 1
  92. } else if (index >= this.length) {
  93. return 0
  94. } else {
  95. return index
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style scoped lang="scss">
  102. .swiper {
  103. width: 750rpx;
  104. height: 100%;
  105. }
  106. swiper-item>view{
  107. height: 100%;
  108. }
  109. swiper-item image{
  110. width: 750rpx;
  111. height: 100%;
  112. }
  113. </style>