u-sticky.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const uni_modules_uviewPlus_components_uSticky_props = require("./props.js");
  4. const uni_modules_uviewPlus_libs_mixin_mpMixin = require("../../libs/mixin/mpMixin.js");
  5. const uni_modules_uviewPlus_libs_mixin_mixin = require("../../libs/mixin/mixin.js");
  6. const uni_modules_uviewPlus_libs_function_index = require("../../libs/function/index.js");
  7. const uni_modules_uviewPlus_libs_config_zIndex = require("../../libs/config/zIndex.js");
  8. const _sfc_main = {
  9. name: "u-sticky",
  10. mixins: [uni_modules_uviewPlus_libs_mixin_mpMixin.mpMixin, uni_modules_uviewPlus_libs_mixin_mixin.mixin, uni_modules_uviewPlus_components_uSticky_props.props],
  11. data() {
  12. return {
  13. cssSticky: false,
  14. // 是否使用css的sticky实现
  15. stickyTop: 0,
  16. // 吸顶的top值,因为可能受自定义导航栏影响,最终的吸顶值非offsetTop值
  17. elId: uni_modules_uviewPlus_libs_function_index.guid(),
  18. left: 0,
  19. // js模式时,吸顶的内容因为处于postition: fixed模式,为了和原来保持一致的样式,需要记录并重新设置它的left,height,width属性
  20. width: "auto",
  21. height: "auto",
  22. fixed: false
  23. // js模式时,是否处于吸顶模式
  24. };
  25. },
  26. computed: {
  27. style() {
  28. const style = {};
  29. if (!this.disabled) {
  30. if (this.cssSticky) {
  31. style.position = "sticky";
  32. style.zIndex = this.uZindex;
  33. style.top = uni_modules_uviewPlus_libs_function_index.addUnit(this.stickyTop);
  34. } else {
  35. style.height = this.fixed ? this.height + "px" : "auto";
  36. }
  37. } else {
  38. style.position = "static";
  39. }
  40. style.backgroundColor = this.bgColor;
  41. return uni_modules_uviewPlus_libs_function_index.deepMerge(uni_modules_uviewPlus_libs_function_index.addStyle(this.customStyle), style);
  42. },
  43. // 吸顶内容的样式
  44. stickyContent() {
  45. const style = {};
  46. if (!this.cssSticky) {
  47. style.position = this.fixed ? "fixed" : "static";
  48. style.top = this.stickyTop + "px";
  49. style.left = this.left + "px";
  50. style.width = this.width == "auto" ? "auto" : this.width + "px";
  51. style.zIndex = this.uZindex;
  52. }
  53. return style;
  54. },
  55. uZindex() {
  56. return this.zIndex ? this.zIndex : uni_modules_uviewPlus_libs_config_zIndex.zIndex.sticky;
  57. }
  58. },
  59. mounted() {
  60. this.init();
  61. },
  62. methods: {
  63. init() {
  64. this.getStickyTop();
  65. this.checkSupportCssSticky();
  66. if (!this.cssSticky) {
  67. !this.disabled && this.initObserveContent();
  68. }
  69. },
  70. initObserveContent() {
  71. this.$uGetRect("#" + this.elId).then((res) => {
  72. this.height = res.height;
  73. this.left = res.left;
  74. this.width = res.width;
  75. this.$nextTick(() => {
  76. this.observeContent();
  77. });
  78. });
  79. },
  80. observeContent() {
  81. this.disconnectObserver("contentObserver");
  82. const contentObserver = common_vendor.index.createIntersectionObserver({
  83. // 检测的区间范围
  84. thresholds: [0.95, 0.98, 1]
  85. });
  86. contentObserver.relativeToViewport({
  87. top: -this.stickyTop
  88. });
  89. contentObserver.observe(`#${this.elId}`, (res) => {
  90. this.setFixed(res.boundingClientRect.top);
  91. });
  92. this.contentObserver = contentObserver;
  93. },
  94. setFixed(top) {
  95. const fixed = top <= this.stickyTop;
  96. this.fixed = fixed;
  97. },
  98. disconnectObserver(observerName) {
  99. const observer = this[observerName];
  100. observer && observer.disconnect();
  101. },
  102. getStickyTop() {
  103. this.stickyTop = uni_modules_uviewPlus_libs_function_index.getPx(this.offsetTop) + uni_modules_uviewPlus_libs_function_index.getPx(this.customNavHeight);
  104. },
  105. async checkSupportCssSticky() {
  106. if (uni_modules_uviewPlus_libs_function_index.os() === "android" && Number(uni_modules_uviewPlus_libs_function_index.sys().system) > 8) {
  107. this.cssSticky = true;
  108. }
  109. this.cssSticky = await this.checkComputedStyle();
  110. if (uni_modules_uviewPlus_libs_function_index.os() === "ios") {
  111. this.cssSticky = true;
  112. }
  113. },
  114. // 在APP和微信小程序上,通过uni.createSelectorQuery可以判断是否支持css sticky
  115. checkComputedStyle() {
  116. return new Promise((resolve) => {
  117. common_vendor.index.createSelectorQuery().in(this).select(".u-sticky").fields({
  118. computedStyle: ["position"]
  119. }).exec((e) => {
  120. resolve("sticky" === e[0].position);
  121. });
  122. });
  123. },
  124. // H5通过创建元素的形式嗅探是否支持css sticky
  125. // 判断浏览器是否支持sticky属性
  126. checkCssStickyForH5() {
  127. }
  128. },
  129. beforeUnmount() {
  130. this.disconnectObserver("contentObserver");
  131. }
  132. };
  133. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  134. return {
  135. a: common_vendor.s($options.stickyContent),
  136. b: $data.elId,
  137. c: common_vendor.s($options.style)
  138. };
  139. }
  140. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-8b303089"]]);
  141. wx.createComponent(Component);