wSwiper.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <swiper :circular="false" @change="onChangeTab" :current="data.current"
  3. :style="{'height': swiperHeight + 'px'}">
  4. <swiper-item v-for="(cItem,index) in props.list" :key="cItem">
  5. <view class="item flex-col flex-center">
  6. <slot :item="cItem"></slot>
  7. </view>
  8. </swiper-item>
  9. </swiper>
  10. </template>
  11. <script setup>
  12. import {
  13. ref,
  14. reactive,
  15. watch,
  16. onMounted
  17. } from 'vue';
  18. import {
  19. onLoad,
  20. } from "@dcloudio/uni-app";
  21. const props = defineProps({
  22. swiperHeight: { // swiper的高度
  23. type: Number,
  24. default: 0
  25. },
  26. positionIndex: { // 当前的数据的下标,直接定位到某条数据时使用
  27. required: false,
  28. type: Number,
  29. default: 0
  30. },
  31. list: { // 数据数组
  32. required: true,
  33. type: Array,
  34. default: []
  35. },
  36. })
  37. const emits = defineEmits(['change'])
  38. const data = reactive({
  39. current:0,
  40. })
  41. watch(() => props.positionIndex, (val) => positionData(), {
  42. immediate: true
  43. })
  44. watch(() => props.list, (val) => positionData(), {
  45. immediate: true
  46. })
  47. function positionData() {
  48. if (props.list[data.current]) {
  49. props.list[data.current].mta_show = true;
  50. }
  51. if (props.list[data.current+1]) {
  52. props.list[data.current+1].mta_show = true;
  53. }
  54. if (props.list[data.current-1]) {
  55. props.list[data.current-1].mta_show = true;
  56. }
  57. }
  58. function onChangeTab(e) {
  59. data.current = e.detail.current;
  60. if (props.list[data.current+1]) {
  61. props.list[data.current+1].mta_show = true;
  62. }
  63. if (props.list[data.current-1]) {
  64. props.list[data.current-1].mta_show = true;
  65. }
  66. emits('change', data.current)
  67. }
  68. </script>
  69. <style>
  70. </style>