Sticky.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { ref, watch, computed, nextTick, reactive, defineComponent, createVNode as _createVNode } from "vue";
  2. import { extend, isHidden, unitToPx, numericProp, windowWidth, windowHeight, getScrollTop, getZIndexStyle, makeStringProp, makeNumericProp, createNamespace } from "../utils/index.mjs";
  3. import { useRect, useEventListener, useScrollParent } from "@vant/use";
  4. import { useVisibilityChange } from "../composables/use-visibility-change.mjs";
  5. const [name, bem] = createNamespace("sticky");
  6. const stickyProps = {
  7. zIndex: numericProp,
  8. position: makeStringProp("top"),
  9. container: Object,
  10. offsetTop: makeNumericProp(0),
  11. offsetBottom: makeNumericProp(0)
  12. };
  13. var stdin_default = defineComponent({
  14. name,
  15. props: stickyProps,
  16. emits: ["scroll", "change"],
  17. setup(props, {
  18. emit,
  19. slots
  20. }) {
  21. const root = ref();
  22. const scrollParent = useScrollParent(root);
  23. const state = reactive({
  24. fixed: false,
  25. width: 0,
  26. // root width
  27. height: 0,
  28. // root height
  29. transform: 0
  30. });
  31. const isReset = ref(false);
  32. const offset = computed(() => unitToPx(props.position === "top" ? props.offsetTop : props.offsetBottom));
  33. const rootStyle = computed(() => {
  34. if (isReset.value) {
  35. return;
  36. }
  37. const {
  38. fixed,
  39. height,
  40. width
  41. } = state;
  42. if (fixed) {
  43. return {
  44. width: `${width}px`,
  45. height: `${height}px`
  46. };
  47. }
  48. });
  49. const stickyStyle = computed(() => {
  50. if (!state.fixed || isReset.value) {
  51. return;
  52. }
  53. const style = extend(getZIndexStyle(props.zIndex), {
  54. width: `${state.width}px`,
  55. height: `${state.height}px`,
  56. [props.position]: `${offset.value}px`
  57. });
  58. if (state.transform) {
  59. style.transform = `translate3d(0, ${state.transform}px, 0)`;
  60. }
  61. return style;
  62. });
  63. const emitScroll = (scrollTop) => emit("scroll", {
  64. scrollTop,
  65. isFixed: state.fixed
  66. });
  67. const onScroll = () => {
  68. if (!root.value || isHidden(root)) {
  69. return;
  70. }
  71. const {
  72. container,
  73. position
  74. } = props;
  75. const rootRect = useRect(root);
  76. const scrollTop = getScrollTop(window);
  77. state.width = rootRect.width;
  78. state.height = rootRect.height;
  79. if (position === "top") {
  80. if (container) {
  81. const containerRect = useRect(container);
  82. const difference = containerRect.bottom - offset.value - state.height;
  83. state.fixed = offset.value > rootRect.top && containerRect.bottom > 0;
  84. state.transform = difference < 0 ? difference : 0;
  85. } else {
  86. state.fixed = offset.value > rootRect.top;
  87. }
  88. } else {
  89. const {
  90. clientHeight
  91. } = document.documentElement;
  92. if (container) {
  93. const containerRect = useRect(container);
  94. const difference = clientHeight - containerRect.top - offset.value - state.height;
  95. state.fixed = clientHeight - offset.value < rootRect.bottom && clientHeight > containerRect.top;
  96. state.transform = difference < 0 ? -difference : 0;
  97. } else {
  98. state.fixed = clientHeight - offset.value < rootRect.bottom;
  99. }
  100. }
  101. emitScroll(scrollTop);
  102. };
  103. watch(() => state.fixed, (value) => emit("change", value));
  104. useEventListener("scroll", onScroll, {
  105. target: scrollParent,
  106. passive: true
  107. });
  108. useVisibilityChange(root, onScroll);
  109. watch([windowWidth, windowHeight], () => {
  110. if (!root.value || isHidden(root) || !state.fixed) {
  111. return;
  112. }
  113. isReset.value = true;
  114. nextTick(() => {
  115. const rootRect = useRect(root);
  116. state.width = rootRect.width;
  117. state.height = rootRect.height;
  118. isReset.value = false;
  119. });
  120. });
  121. return () => {
  122. var _a;
  123. return _createVNode("div", {
  124. "ref": root,
  125. "style": rootStyle.value
  126. }, [_createVNode("div", {
  127. "class": bem({
  128. fixed: state.fixed && !isReset.value
  129. }),
  130. "style": stickyStyle.value
  131. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])]);
  132. };
  133. }
  134. });
  135. export {
  136. stdin_default as default,
  137. stickyProps
  138. };