fs-swipe-action.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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>
  30. /**
  31. * 滑动面板组件
  32. * @description 滑动面板组件
  33. * @property {Array} options 操作选项
  34. * @property {null} optionData 操作选项数据
  35. * @property {Number} optionWidth 操作选项宽度(单位px)
  36. * @property {Boolean} disabled 操作选项数据
  37. * @property {Number} swipeRate 滑动比例阈值,大于此值会自动打开或关闭
  38. */
  39. export default {
  40. name: 'fs-swipe-action'
  41. }
  42. </script>
  43. <script setup>
  44. import { inject, onMounted, reactive, ref, nextTick, getCurrentInstance } from 'vue'
  45. const props = defineProps({
  46. disabled: Boolean,
  47. optionWidth: {
  48. type: Number,
  49. default: 80
  50. },
  51. options: {
  52. type: Array,
  53. default: () => []
  54. },
  55. optionData: null,
  56. swipeRate: {
  57. type: Number,
  58. default: 1 / 4
  59. }
  60. })
  61. const emits = defineEmits(['clickOption'])
  62. const state = reactive({
  63. moveX: 0,
  64. scrollX: 0,
  65. swipeViewWidth: 0,
  66. status: false,
  67. moving: false,
  68. allOptionWidth: props.optionWidth * props.options.length
  69. })
  70. const swipeGroup = inject('swipeGroup', {})
  71. const updateState = () => {
  72. if (state.moving) {
  73. state.moving = false
  74. } else {
  75. state.moveX = 0
  76. }
  77. }
  78. swipeGroup.updateChildren({
  79. updateState
  80. })
  81. onMounted(() => {
  82. uni
  83. .createSelectorQuery()
  84. .in(getCurrentInstance().ctx)
  85. .select('.fs-swipe-box')
  86. .boundingClientRect(data => {
  87. state.swipeViewWidth = data.width + state.allOptionWidth + 'px'
  88. })
  89. .exec()
  90. })
  91. function change(e) {
  92. state.scrollX = e.detail.x
  93. }
  94. function touchstart() {
  95. state.moving = true
  96. swipeGroup.toggle()
  97. }
  98. function touchend() {
  99. state.moveX = state.scrollX
  100. setTimeout(() => {
  101. if (state.status) {
  102. //打开状态
  103. if (state.moveX >= -state.allOptionWidth * (1 - Number(props.swipeRate))) {
  104. handleClose()
  105. } else {
  106. handleOpen()
  107. }
  108. } else {
  109. if (state.moveX <= -state.allOptionWidth * Number(props.swipeRate)) {
  110. handleOpen()
  111. } else {
  112. handleClose()
  113. }
  114. }
  115. }, 0)
  116. }
  117. function handleOpen() {
  118. state.moveX = -state.allOptionWidth
  119. state.status = true
  120. }
  121. function handleClose() {
  122. state.moveX = 0
  123. state.status = false
  124. }
  125. function handleOption(item) {
  126. emits('clickOption', { option: item, data: props.optionData })
  127. handleClose()
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. .fs-swipe-box {
  132. width: auto;
  133. height: auto;
  134. overflow: hidden;
  135. position: relative;
  136. }
  137. .fs-swipe-view {
  138. display: flex;
  139. position: relative;
  140. height: inherit;
  141. width: 100%;
  142. }
  143. .fs-swipe-content {
  144. flex: 1;
  145. }
  146. .fs-swipe-option {
  147. display: flex;
  148. justify-content: center;
  149. align-items: center;
  150. text-align: center;
  151. color: #fff;
  152. }
  153. </style>