fs-swipe-action.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <movable-area class="fs-swipe-box">
  3. <movable-view
  4. v-if="state.swipeViewWidth"
  5. class="fs-swipe-view"
  6. @change="change"
  7. @touchend="touchend"
  8. @touchstart="touchstart"
  9. direction="horizontal"
  10. :disabled="disabled"
  11. :x="state.moveX"
  12. :style="{ width: state.swipeViewWidth }"
  13. >
  14. <view class="fs-swipe-content"><slot></slot></view>
  15. <view
  16. class="fs-swipe-option"
  17. v-for="(item, index) in options"
  18. :key="index"
  19. :style="{ backgroundColor: item.bgColor }"
  20. @click="handleOption(item)"
  21. >
  22. <view class="fs-swipe-option-text" :style="{ width: optionWidth + 'px', backgroundColor: item.bgColor }">
  23. {{ item.name }}
  24. </view>
  25. </view>
  26. </movable-view>
  27. </movable-area>
  28. </template>
  29. <script setup>
  30. import { inject, onMounted, reactive, ref, nextTick, getCurrentInstance } from 'vue'
  31. const props = defineProps({
  32. disabled: Boolean,
  33. optionWidth: {
  34. type: Number,
  35. default: 80
  36. },
  37. options: {
  38. type: Array,
  39. default: () => []
  40. },
  41. optionData: null,
  42. swipeRate: {
  43. type: Number,
  44. default: 1 / 4
  45. }
  46. })
  47. const emits = defineEmits(['clickOption'])
  48. const state = reactive({
  49. moveX: 0,
  50. scrollX: 0,
  51. swipeViewWidth: 0,
  52. status: false,
  53. moving: false,
  54. allOptionWidth: props.optionWidth * props.options.length
  55. })
  56. const swipeGroup = inject('swipeGroup', {})
  57. const updateState = () => {
  58. if (state.moving) {
  59. state.moving = false
  60. } else {
  61. state.moveX = 0
  62. }
  63. }
  64. swipeGroup.updateChildren({
  65. updateState
  66. })
  67. onMounted(() => {
  68. uni
  69. .createSelectorQuery()
  70. .in(getCurrentInstance().ctx)
  71. .select('.fs-swipe-box')
  72. .boundingClientRect(data => {
  73. state.swipeViewWidth = data.width + state.allOptionWidth + 'px'
  74. })
  75. .exec()
  76. })
  77. function change(e) {
  78. state.scrollX = e.detail.x
  79. }
  80. function touchstart() {
  81. state.moving = true
  82. swipeGroup.toggle()
  83. }
  84. function touchend() {
  85. state.moveX = state.scrollX
  86. setTimeout(() => {
  87. if (state.status) {
  88. //打开状态
  89. if (state.moveX >= -state.allOptionWidth * (1 - Number(props.swipeRate))) {
  90. handleClose()
  91. } else {
  92. handleOpen()
  93. }
  94. } else {
  95. if (state.moveX <= -state.allOptionWidth * Number(props.swipeRate)) {
  96. handleOpen()
  97. } else {
  98. handleClose()
  99. }
  100. }
  101. }, 0)
  102. }
  103. function handleOpen() {
  104. state.moveX = -state.allOptionWidth
  105. state.status = true
  106. }
  107. function handleClose() {
  108. state.moveX = 0
  109. state.status = false
  110. }
  111. function handleOption(item) {
  112. emits('clickOption', { option: item, data: props.optionData })
  113. handleClose()
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. .fs-swipe-box {
  118. width: auto;
  119. height: auto;
  120. overflow: hidden;
  121. position: relative;
  122. }
  123. .fs-swipe-view {
  124. display: flex;
  125. position: relative;
  126. height: inherit;
  127. width: 100%;
  128. }
  129. .fs-swipe-content {
  130. flex: 1;
  131. }
  132. .fs-swipe-option {
  133. display: flex;
  134. justify-content: center;
  135. align-items: center;
  136. text-align: center;
  137. color: #fff;
  138. }
  139. </style>