fs-swipe-action.vue 2.7 KB

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