useSwiper.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. reactive
  3. } from "vue"
  4. import {getUserIdentity} from "@/utils/common.js"
  5. export function useSwiper(Da, noticeYK) {
  6. const useCode = getUserIdentity()
  7. const swiperData = reactive({
  8. startX: 0, // 触摸起点X坐标
  9. isAllowed: false // 是否允许切
  10. })
  11. // 1. 触摸开始:记录起点
  12. function handleTouchStart(e) {
  13. swiperData.startX = e.touches[0].pageX;
  14. }
  15. // 2. 触摸结束:校验距离与业务条件
  16. function handleTouchEnd(e) {
  17. const endX = e.changedTouches[0].pageX;
  18. const deltaX = endX - swiperData.startX;
  19. const absDeltaX = Math.abs(deltaX);
  20. // 校验条件:滑动距离 > 10px 且业务条件通过
  21. if (absDeltaX > 10 && checkBusinessCondition()) {
  22. swiperData.isAllowed = true;
  23. // 根据方向更新索引
  24. if (deltaX > 0 && Da.currentIndex > 0) {
  25. Da.currentIndex--; // 向右滑,上一页
  26. } else if (deltaX < 0 && Da.currentIndex < swiperData.list.length - 1) {
  27. Da.currentIndex++; // 向左滑,下一页
  28. }
  29. } else {
  30. swiperData.isAllowed = false;
  31. noticeYK(useCode)
  32. }
  33. }
  34. // 4. 业务条件校验(示例)
  35. function checkBusinessCondition() {
  36. if (useCode != 'VIP') {
  37. return false
  38. }
  39. return true
  40. }
  41. return {
  42. swiperData,
  43. handleTouchStart,
  44. handleTouchEnd
  45. }
  46. }