index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view @touchstart="fingerstart" @touchend="fingerend">
  3. <slot name="content"> </slot>
  4. </view>
  5. </template>
  6. <script setup>
  7. import { onReady, onLoad, onShow, onNavigationBarButtonTap } from "@dcloudio/uni-app";
  8. import { ref, onMounted, inject, shallowRef, reactive, watchEffect, getCurrentInstance, toRefs } from "vue";
  9. const emit = defineEmits(["touchChange"]);
  10. const props = defineProps({});
  11. const {} = toRefs(props);
  12. const startData = ref({
  13. clientX: "",
  14. clientY: "",
  15. });
  16. const updDistance = ref(100);
  17. const lrDistance = ref(50);
  18. const topMed = ref("");
  19. const bottomMed = ref("");
  20. const leftMed = ref("");
  21. const rightMed = ref("");
  22. /**
  23. * @当按下去的时候
  24. */
  25. function fingerstart(e) {
  26. // 记录 距离可视区域左上角 左边距 和 上边距
  27. startData.value.clientX = e.changedTouches[0].clientX;
  28. startData.value.clientY = e.changedTouches[0].clientY;
  29. }
  30. /**
  31. * @当抬起来的时候
  32. */
  33. function fingerend(e) {
  34. // 当前位置 减去 按下位置 计算 距离
  35. const subX = e.changedTouches[0].clientX - startData.value.clientX;
  36. const subY = e.changedTouches[0].clientY - startData.value.clientY;
  37. if (subY > updDistance.value || subY < -updDistance.value) {
  38. if (subY > updDistance.value) {
  39. bottomscroll(subY);
  40. } else if (subY < -updDistance.value) {
  41. topscroll(subY);
  42. }
  43. } else {
  44. if (subX > lrDistance.value) {
  45. rightscroll(subX);
  46. } else if (subX < -lrDistance.value) {
  47. leftscroll(subX);
  48. } else {
  49. console.log("无效操作");
  50. }
  51. }
  52. }
  53. /**
  54. * @上滑触发
  55. */
  56. function topscroll(dista) {
  57. topMed.value ? (topMed.value = dista) : (topMed.value = null);
  58. // console.log("触发了上滑方法!");
  59. emit("change", "上滑");
  60. }
  61. /**
  62. * @下滑触发
  63. */
  64. function bottomscroll(dista) {
  65. bottomMed.value ? (bottomMed.value = dista) : (bottomMed.value = null);
  66. // console.log("触发了下滑方法!");
  67. emit("change", "下滑");
  68. }
  69. /**
  70. * @右滑触发
  71. */
  72. function rightscroll(dista) {
  73. rightMed.value ? (rightMed.value = dista) : (rightMed.value = null);
  74. // console.log("触发了右滑方法!");
  75. emit("change", "右滑");
  76. }
  77. /**
  78. * @左滑触发
  79. */
  80. function leftscroll(dista) {
  81. leftMed.value ? (leftMed.value = dista) : (leftMed.value = null);
  82. // console.log("触发了左滑方法!");
  83. emit("change", "左滑");
  84. }
  85. </script>