dom.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { useRect, useWindowSize } from "@vant/use";
  2. import { unref } from "vue";
  3. import { isIOS as checkIsIOS } from "./basic.mjs";
  4. function getScrollTop(el) {
  5. const top = "scrollTop" in el ? el.scrollTop : el.pageYOffset;
  6. return Math.max(top, 0);
  7. }
  8. function setScrollTop(el, value) {
  9. if ("scrollTop" in el) {
  10. el.scrollTop = value;
  11. } else {
  12. el.scrollTo(el.scrollX, value);
  13. }
  14. }
  15. function getRootScrollTop() {
  16. return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  17. }
  18. function setRootScrollTop(value) {
  19. setScrollTop(window, value);
  20. setScrollTop(document.body, value);
  21. }
  22. function getElementTop(el, scroller) {
  23. if (el === window) {
  24. return 0;
  25. }
  26. const scrollTop = scroller ? getScrollTop(scroller) : getRootScrollTop();
  27. return useRect(el).top + scrollTop;
  28. }
  29. const isIOS = checkIsIOS();
  30. function resetScroll() {
  31. if (isIOS) {
  32. setRootScrollTop(getRootScrollTop());
  33. }
  34. }
  35. const stopPropagation = (event) => event.stopPropagation();
  36. function preventDefault(event, isStopPropagation) {
  37. if (typeof event.cancelable !== "boolean" || event.cancelable) {
  38. event.preventDefault();
  39. }
  40. if (isStopPropagation) {
  41. stopPropagation(event);
  42. }
  43. }
  44. function isHidden(elementRef) {
  45. const el = unref(elementRef);
  46. if (!el) {
  47. return false;
  48. }
  49. const style = window.getComputedStyle(el);
  50. const hidden = style.display === "none";
  51. const parentHidden = el.offsetParent === null && style.position !== "fixed";
  52. return hidden || parentHidden;
  53. }
  54. const { width: windowWidth, height: windowHeight } = useWindowSize();
  55. function isContainingBlock(el) {
  56. const css = window.getComputedStyle(el);
  57. return css.transform !== "none" || css.perspective !== "none" || ["transform", "perspective", "filter"].some(
  58. (value) => (css.willChange || "").includes(value)
  59. );
  60. }
  61. function getContainingBlock(el) {
  62. let node = el.parentElement;
  63. while (node) {
  64. if (node && node.tagName !== "HTML" && node.tagName !== "BODY" && isContainingBlock(node)) {
  65. return node;
  66. }
  67. node = node.parentElement;
  68. }
  69. return null;
  70. }
  71. export {
  72. getContainingBlock,
  73. getElementTop,
  74. getRootScrollTop,
  75. getScrollTop,
  76. isHidden,
  77. preventDefault,
  78. resetScroll,
  79. setRootScrollTop,
  80. setScrollTop,
  81. stopPropagation,
  82. windowHeight,
  83. windowWidth
  84. };