SlideVertical.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script setup lang="ts">
  2. import { onMounted, reactive, ref, watch } from 'vue'
  3. import {
  4. getSlideOffset,
  5. slideInit,
  6. slideReset,
  7. slideTouchEnd,
  8. slideTouchMove,
  9. slideTouchStart,
  10. } from '@/utils/slide'
  11. import { SlideType } from '@/utils/const_var'
  12. import { _css } from '@/utils/dom'
  13. const props = defineProps({
  14. index: { type: Number, default: 0 },
  15. changeActiveIndexUseAnim: { type: Boolean, default: true },
  16. name: { type: String, default: 'SlideVertical' },
  17. })
  18. const emit = defineEmits<{ 'update:index': [number] }>()
  19. const slideListEl = ref<HTMLElement | null>(null)
  20. const state = reactive({
  21. judgeValue: 20,
  22. type: SlideType.VERTICAL,
  23. name: props.name,
  24. localIndex: props.index,
  25. needCheck: true,
  26. next: false,
  27. isDown: false,
  28. start: { x: 0, y: 0, time: 0 },
  29. move: { x: 0, y: 0 },
  30. wrapper: { width: 0, height: 0, childrenLength: 0 },
  31. })
  32. watch(
  33. () => props.index,
  34. (newVal) => {
  35. if (state.localIndex !== newVal && slideListEl.value) {
  36. state.localIndex = newVal
  37. if (props.changeActiveIndexUseAnim) {
  38. _css(slideListEl.value, 'transition-duration', '300ms')
  39. }
  40. _css(
  41. slideListEl.value,
  42. 'transform',
  43. `translate3d(0, ${getSlideOffset(state, slideListEl.value)}px, 0)`,
  44. )
  45. }
  46. },
  47. )
  48. onMounted(() => {
  49. if (slideListEl.value) slideInit(slideListEl.value, state)
  50. })
  51. function touchStart(e: PointerEvent) {
  52. if (slideListEl.value) slideTouchStart(e, slideListEl.value, state)
  53. }
  54. function touchMove(e: PointerEvent) {
  55. if (slideListEl.value) slideTouchMove(e, slideListEl.value, state)
  56. }
  57. function touchEnd(e: PointerEvent) {
  58. slideTouchEnd(e, state)
  59. if (slideListEl.value) slideReset(e, slideListEl.value, state, emit)
  60. }
  61. </script>
  62. <template>
  63. <div class="slide vertical">
  64. <div
  65. ref="slideListEl"
  66. class="slide-list flex-direction-column"
  67. @pointerdown.prevent="touchStart"
  68. @pointermove.prevent="touchMove"
  69. @pointerup.prevent="touchEnd"
  70. >
  71. <slot />
  72. </div>
  73. </div>
  74. </template>