su-sticky.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const sheep_helper_index = require("../../helper/index.js");
  4. const sheep_index = require("../../index.js");
  5. require("../../helper/test.js");
  6. require("../../helper/digit.js");
  7. require("../../url/index.js");
  8. require("../../store/index.js");
  9. require("../../store/app.js");
  10. require("../../api/promotion/diy.js");
  11. require("../../request/index.js");
  12. require("../../config/index.js");
  13. require("../../platform/index.js");
  14. require("../../platform/provider/wechat/index.js");
  15. require("../../platform/provider/wechat/miniProgram.js");
  16. require("../../api/member/auth.js");
  17. require("../../api/member/social.js");
  18. require("../../api/member/user.js");
  19. require("../../platform/provider/apple/index.js");
  20. require("../../platform/share.js");
  21. require("../../router/index.js");
  22. require("../../hooks/useModal.js");
  23. require("../../helper/throttle.js");
  24. require("../../platform/pay.js");
  25. require("../../api/pay/order.js");
  26. require("../../store/user.js");
  27. require("../../store/cart.js");
  28. require("../../api/trade/cart.js");
  29. require("../../api/pay/wallet.js");
  30. require("../../api/trade/order.js");
  31. require("../../api/promotion/coupon.js");
  32. require("../../store/sys.js");
  33. require("../../store/modal.js");
  34. require("../../config/zIndex.js");
  35. const _sfc_main = {
  36. name: "su-sticky",
  37. props: {
  38. // 吸顶容器到顶部某个距离的时候,进行吸顶,在H5平台,NavigationBar为44px
  39. offsetTop: {
  40. type: [String, Number],
  41. default: 0
  42. },
  43. // 自定义导航栏的高度
  44. customNavHeight: {
  45. type: [String, Number],
  46. default: sheep_index.sheep.$platform.navbar
  47. },
  48. // 是否开启吸顶功能
  49. stickyToTop: {
  50. type: Boolean,
  51. default: false
  52. },
  53. // 吸顶区域的背景颜色
  54. bgColor: {
  55. type: String,
  56. default: "transparent"
  57. },
  58. // z-index值
  59. zIndex: {
  60. type: [String, Number],
  61. default: ""
  62. },
  63. // 列表中的索引值
  64. index: {
  65. type: [String, Number],
  66. default: ""
  67. },
  68. customStyle: {
  69. type: [Object, String],
  70. default: () => ({})
  71. }
  72. },
  73. data() {
  74. return {
  75. cssSticky: false,
  76. // 是否使用css的sticky实现
  77. stickyTop: 0,
  78. // 吸顶的top值,因为可能受自定义导航栏影响,最终的吸顶值非offsetTop值
  79. elId: sheep_helper_index.guid(),
  80. left: 0,
  81. // js模式时,吸顶的内容因为处于postition: fixed模式,为了和原来保持一致的样式,需要记录并重新设置它的left,height,width属性
  82. width: "auto",
  83. height: "auto",
  84. fixed: false
  85. // js模式时,是否处于吸顶模式
  86. };
  87. },
  88. computed: {
  89. style() {
  90. const style = {};
  91. if (!this.stickyToTop) {
  92. if (this.cssSticky) {
  93. style.position = "sticky";
  94. style.zIndex = this.uZindex;
  95. style.top = sheep_helper_index.addUnit(this.stickyTop);
  96. } else {
  97. style.height = this.fixed ? this.height + "px" : "auto";
  98. }
  99. } else {
  100. style.position = "static";
  101. }
  102. style.backgroundColor = this.bgColor;
  103. return sheep_helper_index.deepMerge(sheep_helper_index.addStyle(this.customStyle), style);
  104. },
  105. // 吸顶内容的样式
  106. stickyContent() {
  107. const style = {};
  108. if (!this.cssSticky) {
  109. style.position = this.fixed ? "fixed" : "static";
  110. style.top = this.stickyTop + "px";
  111. style.left = this.left + "px";
  112. style.width = this.width == "auto" ? "auto" : this.width + "px";
  113. style.zIndex = this.uZindex;
  114. }
  115. return style;
  116. },
  117. uZindex() {
  118. return this.zIndex ? this.zIndex : 970;
  119. }
  120. },
  121. mounted() {
  122. this.init();
  123. },
  124. methods: {
  125. init() {
  126. this.getStickyTop();
  127. this.checkSupportCssSticky();
  128. if (!this.cssSticky) {
  129. !this.stickyToTop && this.initObserveContent();
  130. }
  131. },
  132. $uGetRect(selector, all) {
  133. return new Promise((resolve) => {
  134. common_vendor.index.createSelectorQuery().in(this)[all ? "selectAll" : "select"](selector).boundingClientRect((rect) => {
  135. if (all && Array.isArray(rect) && rect.length) {
  136. resolve(rect);
  137. }
  138. if (!all && rect) {
  139. resolve(rect);
  140. }
  141. }).exec();
  142. });
  143. },
  144. initObserveContent() {
  145. this.$uGetRect("#" + this.elId).then((res) => {
  146. this.height = res.height;
  147. this.left = res.left;
  148. this.width = res.width;
  149. this.$nextTick(() => {
  150. this.observeContent();
  151. });
  152. });
  153. },
  154. observeContent() {
  155. this.disconnectObserver("contentObserver");
  156. const contentObserver = common_vendor.index.createIntersectionObserver({
  157. // 检测的区间范围
  158. thresholds: [0.95, 0.98, 1]
  159. });
  160. contentObserver.relativeToViewport({
  161. top: -this.stickyTop
  162. });
  163. contentObserver.observe(`#${this.elId}`, (res) => {
  164. this.setFixed(res.boundingClientRect.top);
  165. });
  166. this.contentObserver = contentObserver;
  167. },
  168. setFixed(top) {
  169. const fixed = top <= this.stickyTop;
  170. this.fixed = fixed;
  171. },
  172. disconnectObserver(observerName) {
  173. const observer = this[observerName];
  174. observer && observer.disconnect();
  175. },
  176. getStickyTop() {
  177. this.stickyTop = sheep_helper_index.getPx(this.offsetTop) + sheep_helper_index.getPx(this.customNavHeight);
  178. },
  179. async checkSupportCssSticky() {
  180. if (sheep_helper_index.os() === "android" && Number(sheep_helper_index.sys().system) > 8) {
  181. this.cssSticky = true;
  182. }
  183. this.cssSticky = await this.checkComputedStyle();
  184. if (sheep_helper_index.os() === "ios") {
  185. this.cssSticky = true;
  186. }
  187. },
  188. // 在APP和微信小程序上,通过uni.createSelectorQuery可以判断是否支持css sticky
  189. checkComputedStyle() {
  190. return new Promise((resolve) => {
  191. common_vendor.index.createSelectorQuery().in(this).select(".u-sticky").fields({
  192. computedStyle: ["position"]
  193. }).exec((e) => {
  194. resolve("sticky" === e[0].position);
  195. });
  196. });
  197. },
  198. // H5通过创建元素的形式嗅探是否支持css sticky
  199. // 判断浏览器是否支持sticky属性
  200. checkCssStickyForH5() {
  201. }
  202. },
  203. beforeDestroy() {
  204. this.disconnectObserver("contentObserver");
  205. }
  206. };
  207. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  208. return {
  209. a: common_vendor.s($options.stickyContent),
  210. b: $data.elId,
  211. c: common_vendor.s($options.style)
  212. };
  213. }
  214. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-453bc176"], ["__file", "/Users/RuHu.Xu/Desktop/mall-newfeifan-zx-app/sheep/ui/su-sticky/su-sticky.vue"]]);
  215. wx.createComponent(Component);