fs-swipe-action.vue 2.7 KB

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