classify.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const pages_template_common_classify_data = require("../pages_template/common/classify.data.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. scrollTop: 0,
  8. //tab标题的滚动条位置
  9. oldScrollTop: 0,
  10. current: 0,
  11. // 预设当前项的值
  12. menuHeight: 0,
  13. // 左边菜单的高度
  14. menuItemHeight: 0,
  15. // 左边菜单item的高度
  16. itemId: "",
  17. // 栏目右边scroll-view用于滚动的id
  18. tabbar: pages_template_common_classify_data.classifyData,
  19. arr: [],
  20. scrollRightTop: 0,
  21. // 右边栏目scroll-view的滚动条高度
  22. timer: null
  23. // 定时器
  24. };
  25. },
  26. onReady() {
  27. this.getMenuItemTop();
  28. },
  29. methods: {
  30. // 点击左边的栏目切换
  31. async swichMenu(index) {
  32. if (this.arr.length == 0) {
  33. await this.getMenuItemTop();
  34. }
  35. if (index == this.current)
  36. return;
  37. this.scrollRightTop = this.oldScrollTop;
  38. this.$nextTick(() => {
  39. this.scrollRightTop = this.arr[index];
  40. this.current = index;
  41. this.leftMenuStatus(index);
  42. });
  43. },
  44. // 获取一个目标元素的高度
  45. getElRect(elClass, dataVal) {
  46. new Promise((resolve, reject) => {
  47. const query = common_vendor.index.createSelectorQuery().in(this);
  48. query.select("." + elClass).fields({
  49. size: true
  50. }, (res) => {
  51. if (!res) {
  52. setTimeout(() => {
  53. this.getElRect(elClass);
  54. }, 10);
  55. return;
  56. }
  57. this[dataVal] = res.height;
  58. resolve();
  59. }).exec();
  60. });
  61. },
  62. // 观测元素相交状态
  63. async observer() {
  64. this.tabbar.map((val, index) => {
  65. let observer = common_vendor.index.createIntersectionObserver(this);
  66. observer.relativeTo(".right-box", {
  67. top: 0
  68. }).observe("#item" + index, (res) => {
  69. if (res.intersectionRatio > 0) {
  70. let id = res.id.substring(4);
  71. this.leftMenuStatus(id);
  72. }
  73. });
  74. });
  75. },
  76. // 设置左边菜单的滚动状态
  77. async leftMenuStatus(index) {
  78. this.current = index;
  79. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  80. await this.getElRect("menu-scroll-view", "menuHeight");
  81. await this.getElRect("u-tab-item", "menuItemHeight");
  82. }
  83. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  84. },
  85. // 获取右边菜单每个item到顶部的距离
  86. getMenuItemTop() {
  87. new Promise((resolve) => {
  88. let selectorQuery = common_vendor.index.createSelectorQuery();
  89. selectorQuery.selectAll(".class-item").boundingClientRect((rects) => {
  90. if (!rects.length) {
  91. setTimeout(() => {
  92. this.getMenuItemTop();
  93. }, 10);
  94. return;
  95. }
  96. rects.forEach((rect) => {
  97. this.arr.push(rect.top - rects[0].top);
  98. resolve();
  99. });
  100. }).exec();
  101. });
  102. },
  103. // 右边菜单滚动
  104. async rightScroll(e) {
  105. this.oldScrollTop = e.detail.scrollTop;
  106. if (this.arr.length == 0) {
  107. await this.getMenuItemTop();
  108. }
  109. if (this.timer)
  110. return;
  111. if (!this.menuHeight) {
  112. await this.getElRect("menu-scroll-view", "menuHeight");
  113. }
  114. setTimeout(() => {
  115. this.timer = null;
  116. let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  117. for (let i = 0; i < this.arr.length; i++) {
  118. let height1 = this.arr[i];
  119. let height2 = this.arr[i + 1];
  120. if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
  121. this.leftMenuStatus(i);
  122. return;
  123. }
  124. }
  125. }, 10);
  126. },
  127. clickMenu(menu) {
  128. console.log(menu);
  129. }
  130. }
  131. };
  132. if (!Array) {
  133. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  134. _easycom_u_icon2();
  135. }
  136. const _easycom_u_icon = () => "../node-modules/uview-plus/components/u-icon/u-icon.js";
  137. if (!Math) {
  138. _easycom_u_icon();
  139. }
  140. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  141. return {
  142. a: common_vendor.p({
  143. name: "search",
  144. color: "#909399",
  145. size: 28
  146. }),
  147. b: common_vendor.f($data.tabbar, (item, index, i0) => {
  148. return {
  149. a: common_vendor.t(item.name),
  150. b: index,
  151. c: common_vendor.n($data.current == index ? "u-tab-item-active" : ""),
  152. d: common_vendor.o(($event) => $options.swichMenu(index), index)
  153. };
  154. }),
  155. c: $data.scrollTop,
  156. d: $data.itemId,
  157. e: common_vendor.f($data.tabbar, (item, index, i0) => {
  158. return {
  159. a: common_vendor.t(item.name),
  160. b: common_vendor.f(item.foods, (item1, index1, i1) => {
  161. return {
  162. a: item1.icon,
  163. b: common_vendor.t(item1.name),
  164. c: index1,
  165. d: common_vendor.o(($event) => $options.clickMenu(item1), index1)
  166. };
  167. }),
  168. c: "item" + index,
  169. d: index
  170. };
  171. }),
  172. f: $data.scrollRightTop,
  173. g: common_vendor.o((...args) => $options.rightScroll && $options.rightScroll(...args))
  174. };
  175. }
  176. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-3e59c162"], ["__file", "C:/Users/Administrator/Desktop/srcaaa/pages/classify.vue"]]);
  177. wx.createPage(MiniProgramPage);