dom.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { useRect, useWindowSize } from "@vant/use";
  2. import { unref } from "vue";
  3. import { isIOS as checkIsIOS } from "./validate.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. export {
  56. getElementTop,
  57. getRootScrollTop,
  58. getScrollTop,
  59. isHidden,
  60. preventDefault,
  61. resetScroll,
  62. setRootScrollTop,
  63. setScrollTop,
  64. stopPropagation,
  65. windowHeight,
  66. windowWidth
  67. };