wSwiper.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. required: true,
  24. type: Number,
  25. default: 0
  26. },
  27. positionIndex: { // 当前的数据的下标,直接定位到某条数据时使用
  28. required: false,
  29. type: Number,
  30. default: 0
  31. },
  32. list: { // 数据数组
  33. required: true,
  34. type: Array,
  35. default: []
  36. },
  37. })
  38. const emits = defineEmits(['change'])
  39. const data = reactive({
  40. current:0,
  41. })
  42. watch(() => props.positionIndex, (val) => positionData(), {
  43. immediate: true
  44. })
  45. watch(() => props.list, (val) => positionData(), {
  46. immediate: true
  47. })
  48. function positionData() {
  49. if (props.list[data.current]) {
  50. props.list[data.current].mta_show = true;
  51. }
  52. if (props.list[data.current+1]) {
  53. props.list[data.current+1].mta_show = true;
  54. }
  55. if (props.list[data.current-1]) {
  56. props.list[data.current-1].mta_show = true;
  57. }
  58. }
  59. function onChangeTab(e) {
  60. data.current = e.detail.current;
  61. if (props.list[data.current+1]) {
  62. props.list[data.current+1].mta_show = true;
  63. }
  64. if (props.list[data.current-1]) {
  65. props.list[data.current-1].mta_show = true;
  66. }
  67. emits('change', data.current)
  68. }
  69. </script>
  70. <style>
  71. </style>