wSwiper.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <swiper :circular="false" @change="onChangeTab" :current="data.current" :duration="data.duration"
  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. nextTick
  18. } from 'vue';
  19. import {
  20. onLoad,
  21. } from "@dcloudio/uni-app";
  22. const props = defineProps({
  23. swiperHeight: { // swiper的高度
  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. duration: 500
  42. })
  43. watch(() => props.positionIndex, (val) => {
  44. data.duration = 0
  45. data.current = props.positionIndex
  46. positionData()
  47. }, {
  48. immediate: true
  49. })
  50. watch(() => props.list, (val) => positionData(), {
  51. immediate: true
  52. })
  53. function positionData() {
  54. if (props.list[data.current]) {
  55. props.list[data.current].mta_show = true;
  56. }
  57. if (props.list[data.current+1]) {
  58. props.list[data.current+1].mta_show = true;
  59. }
  60. if (props.list[data.current-1]) {
  61. props.list[data.current-1].mta_show = true;
  62. }
  63. nextTick(() => {
  64. data.duration = 500;
  65. })
  66. }
  67. function onChangeTab(e) {
  68. data.current = e.detail.current;
  69. if (props.list[data.current+1]) {
  70. props.list[data.current+1].mta_show = true;
  71. }
  72. if (props.list[data.current-1]) {
  73. props.list[data.current-1].mta_show = true;
  74. }
  75. emits('change', data.current)
  76. }
  77. </script>
  78. <style>
  79. </style>