su-sticky.js 7.8 KB

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