su-sticky.js 6.7 KB

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