Sticky.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 name2 in all)
  7. __defProp(target, name2, { get: all[name2], 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. default: () => stdin_default,
  21. stickyProps: () => stickyProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. var import_use = require("@vant/use");
  27. var import_use_visibility_change = require("../composables/use-visibility-change");
  28. const [name, bem] = (0, import_utils.createNamespace)("sticky");
  29. const stickyProps = {
  30. zIndex: import_utils.numericProp,
  31. position: (0, import_utils.makeStringProp)("top"),
  32. container: Object,
  33. offsetTop: (0, import_utils.makeNumericProp)(0),
  34. offsetBottom: (0, import_utils.makeNumericProp)(0)
  35. };
  36. var stdin_default = (0, import_vue.defineComponent)({
  37. name,
  38. props: stickyProps,
  39. emits: ["scroll", "change"],
  40. setup(props, {
  41. emit,
  42. slots
  43. }) {
  44. const root = (0, import_vue.ref)();
  45. const scrollParent = (0, import_use.useScrollParent)(root);
  46. const state = (0, import_vue.reactive)({
  47. fixed: false,
  48. width: 0,
  49. // root width
  50. height: 0,
  51. // root height
  52. transform: 0
  53. });
  54. const isReset = (0, import_vue.ref)(false);
  55. const offset = (0, import_vue.computed)(() => (0, import_utils.unitToPx)(props.position === "top" ? props.offsetTop : props.offsetBottom));
  56. const rootStyle = (0, import_vue.computed)(() => {
  57. if (isReset.value) {
  58. return;
  59. }
  60. const {
  61. fixed,
  62. height,
  63. width
  64. } = state;
  65. if (fixed) {
  66. return {
  67. width: `${width}px`,
  68. height: `${height}px`
  69. };
  70. }
  71. });
  72. const stickyStyle = (0, import_vue.computed)(() => {
  73. if (!state.fixed || isReset.value) {
  74. return;
  75. }
  76. const style = (0, import_utils.extend)((0, import_utils.getZIndexStyle)(props.zIndex), {
  77. width: `${state.width}px`,
  78. height: `${state.height}px`,
  79. [props.position]: `${offset.value}px`
  80. });
  81. if (state.transform) {
  82. style.transform = `translate3d(0, ${state.transform}px, 0)`;
  83. }
  84. return style;
  85. });
  86. const emitScroll = (scrollTop) => emit("scroll", {
  87. scrollTop,
  88. isFixed: state.fixed
  89. });
  90. const onScroll = () => {
  91. if (!root.value || (0, import_utils.isHidden)(root)) {
  92. return;
  93. }
  94. const {
  95. container,
  96. position
  97. } = props;
  98. const rootRect = (0, import_use.useRect)(root);
  99. const scrollTop = (0, import_utils.getScrollTop)(window);
  100. state.width = rootRect.width;
  101. state.height = rootRect.height;
  102. if (position === "top") {
  103. if (container) {
  104. const containerRect = (0, import_use.useRect)(container);
  105. const difference = containerRect.bottom - offset.value - state.height;
  106. state.fixed = offset.value > rootRect.top && containerRect.bottom > 0;
  107. state.transform = difference < 0 ? difference : 0;
  108. } else {
  109. state.fixed = offset.value > rootRect.top;
  110. }
  111. } else {
  112. const {
  113. clientHeight
  114. } = document.documentElement;
  115. if (container) {
  116. const containerRect = (0, import_use.useRect)(container);
  117. const difference = clientHeight - containerRect.top - offset.value - state.height;
  118. state.fixed = clientHeight - offset.value < rootRect.bottom && clientHeight > containerRect.top;
  119. state.transform = difference < 0 ? -difference : 0;
  120. } else {
  121. state.fixed = clientHeight - offset.value < rootRect.bottom;
  122. }
  123. }
  124. emitScroll(scrollTop);
  125. };
  126. (0, import_vue.watch)(() => state.fixed, (value) => emit("change", value));
  127. (0, import_use.useEventListener)("scroll", onScroll, {
  128. target: scrollParent,
  129. passive: true
  130. });
  131. (0, import_use_visibility_change.useVisibilityChange)(root, onScroll);
  132. (0, import_vue.watch)([import_utils.windowWidth, import_utils.windowHeight], () => {
  133. if (!root.value || (0, import_utils.isHidden)(root) || !state.fixed) {
  134. return;
  135. }
  136. isReset.value = true;
  137. (0, import_vue.nextTick)(() => {
  138. const rootRect = (0, import_use.useRect)(root);
  139. state.width = rootRect.width;
  140. state.height = rootRect.height;
  141. isReset.value = false;
  142. });
  143. });
  144. return () => {
  145. var _a;
  146. return (0, import_vue.createVNode)("div", {
  147. "ref": root,
  148. "style": rootStyle.value
  149. }, [(0, import_vue.createVNode)("div", {
  150. "class": bem({
  151. fixed: state.fixed && !isReset.value
  152. }),
  153. "style": stickyStyle.value
  154. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])]);
  155. };
  156. }
  157. });