use-lock-scroll.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. useLockScroll: () => useLockScroll
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_use = require("@vant/use");
  25. var import_use_touch = require("./use-touch");
  26. var import_utils = require("../utils");
  27. let totalLockCount = 0;
  28. const BODY_LOCK_CLASS = "van-overflow-hidden";
  29. function useLockScroll(rootRef, shouldLock) {
  30. const touch = (0, import_use_touch.useTouch)();
  31. const DIRECTION_UP = "01";
  32. const DIRECTION_DOWN = "10";
  33. const onTouchMove = (event) => {
  34. touch.move(event);
  35. const direction = touch.deltaY.value > 0 ? DIRECTION_DOWN : DIRECTION_UP;
  36. const el = (0, import_use.getScrollParent)(
  37. event.target,
  38. rootRef.value
  39. );
  40. const { scrollHeight, offsetHeight, scrollTop } = el;
  41. let status = "11";
  42. if (scrollTop === 0) {
  43. status = offsetHeight >= scrollHeight ? "00" : "01";
  44. } else if (scrollTop + offsetHeight >= scrollHeight) {
  45. status = "10";
  46. }
  47. if (status !== "11" && touch.isVertical() && !(parseInt(status, 2) & parseInt(direction, 2))) {
  48. (0, import_utils.preventDefault)(event, true);
  49. }
  50. };
  51. const lock = () => {
  52. document.addEventListener("touchstart", touch.start);
  53. document.addEventListener("touchmove", onTouchMove, { passive: false });
  54. if (!totalLockCount) {
  55. document.body.classList.add(BODY_LOCK_CLASS);
  56. }
  57. totalLockCount++;
  58. };
  59. const unlock = () => {
  60. if (totalLockCount) {
  61. document.removeEventListener("touchstart", touch.start);
  62. document.removeEventListener("touchmove", onTouchMove);
  63. totalLockCount--;
  64. if (!totalLockCount) {
  65. document.body.classList.remove(BODY_LOCK_CLASS);
  66. }
  67. }
  68. };
  69. const init = () => shouldLock() && lock();
  70. const destroy = () => shouldLock() && unlock();
  71. (0, import_use.onMountedOrActivated)(init);
  72. (0, import_vue.onDeactivated)(destroy);
  73. (0, import_vue.onBeforeUnmount)(destroy);
  74. (0, import_vue.watch)(shouldLock, (value) => {
  75. value ? lock() : unlock();
  76. });
  77. }