index1.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="u-wrap">
  3. <view class="u-search-box">
  4. <view class="u-search-inner">
  5. <u-icon name="search" color="#909399" :size="28"></u-icon>
  6. <text class="u-search-text">搜索uView</text>
  7. </view>
  8. </view>
  9. <view class="u-menu-wrap">
  10. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
  11. <view v-for="(item, index) in tabbar" :key="index" class="u-tab-item"
  12. :class="[current == index ? 'u-tab-item-active' : '']" :data-current="index"
  13. @tap.stop="swichMenu(index)">
  14. <text class="u-line-1">{{ item.name }}</text>
  15. </view>
  16. </scroll-view>
  17. <block v-for="(item, index) in tabbar" :key="index">
  18. <scroll-view scroll-y class="right-box" v-if="current == index">
  19. <view class="page-view">
  20. <view class="class-item">
  21. <view class="item-title">
  22. <text>{{ item.name }}</text>
  23. </view>
  24. <view class="item-container">
  25. <view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1"
  26. @click="clickMenu(item1)">
  27. <image class="item-menu-image" :src="item1.icon" mode=""></image>
  28. <view class="item-menu-name">{{ item1.name }}</view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </block>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import classifyData from "@/pages_template/common/classify.data.js";
  40. export default {
  41. data() {
  42. return {
  43. tabbar: classifyData,
  44. scrollTop: 0, //tab标题的滚动条位置
  45. current: 0, // 预设当前项的值
  46. menuHeight: 0, // 左边菜单的高度
  47. menuItemHeight: 0, // 左边菜单item的高度
  48. }
  49. },
  50. methods: {
  51. // 点击左边的栏目切换
  52. async swichMenu(index) {
  53. if (index == this.current) return;
  54. this.current = index;
  55. // 如果为0,意味着尚未初始化
  56. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  57. await this.getElRect('menu-scroll-view', 'menuHeight');
  58. await this.getElRect('u-tab-item', 'menuItemHeight');
  59. }
  60. // 将菜单菜单活动item垂直居中
  61. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  62. },
  63. // 获取一个目标元素的高度
  64. getElRect(elClass, dataVal) {
  65. new Promise((resolve, reject) => {
  66. const query = uni.createSelectorQuery().in(this);
  67. query.select('.' + elClass).fields({ size: true }, res => {
  68. // 如果节点尚未生成,res值为null,循环调用执行
  69. if (!res) {
  70. setTimeout(() => {
  71. this.getElRect(elClass);
  72. }, 10);
  73. return;
  74. }
  75. this[dataVal] = res.height;
  76. }).exec();
  77. })
  78. },
  79. clickMenu(menu) {
  80. console.log(menu);
  81. }
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .u-wrap {
  87. height: calc(100vh);
  88. /* #ifdef H5 */
  89. height: calc(100vh - var(--window-top));
  90. /* #endif */
  91. display: flex;
  92. flex-direction: column;
  93. }
  94. .u-search-box {
  95. padding: 18rpx 30rpx;
  96. }
  97. .u-menu-wrap {
  98. flex: 1;
  99. display: flex;
  100. overflow: hidden;
  101. }
  102. .u-search-inner {
  103. background-color: rgb(234, 234, 234);
  104. border-radius: 100rpx;
  105. display: flex;
  106. align-items: center;
  107. padding: 10rpx 16rpx;
  108. }
  109. .u-search-text {
  110. font-size: 26rpx;
  111. color: $u-tips-color;
  112. margin-left: 10rpx;
  113. }
  114. .u-tab-view {
  115. width: 200rpx;
  116. height: 100%;
  117. }
  118. .u-tab-item {
  119. height: 110rpx;
  120. background: #f6f6f6;
  121. box-sizing: border-box;
  122. display: flex;
  123. align-items: center;
  124. justify-content: center;
  125. font-size: 26rpx;
  126. color: #444;
  127. font-weight: 400;
  128. line-height: 1;
  129. }
  130. .u-tab-item-active {
  131. position: relative;
  132. color: #000;
  133. font-size: 30rpx;
  134. font-weight: 600;
  135. background: #fff;
  136. }
  137. .u-tab-item-active::before {
  138. content: "";
  139. position: absolute;
  140. border-left: 4px solid $u-primary;
  141. height: 32rpx;
  142. left: 0;
  143. top: 39rpx;
  144. }
  145. .u-tab-view {
  146. height: 100%;
  147. }
  148. .right-box {
  149. background-color: rgb(250, 250, 250);
  150. }
  151. .page-view {
  152. padding: 16rpx;
  153. }
  154. .class-item {
  155. margin-bottom: 30rpx;
  156. background-color: #fff;
  157. padding: 16rpx;
  158. border-radius: 8rpx;
  159. }
  160. .item-title {
  161. font-size: 26rpx;
  162. color: $u-main-color;
  163. font-weight: bold;
  164. }
  165. .item-menu-name {
  166. font-weight: normal;
  167. font-size: 24rpx;
  168. color: $u-main-color;
  169. }
  170. .item-container {
  171. display: flex;
  172. flex-wrap: wrap;
  173. }
  174. .thumb-box {
  175. width: 33.333333%;
  176. display: flex;
  177. align-items: center;
  178. justify-content: center;
  179. flex-direction: column;
  180. margin-top: 20rpx;
  181. }
  182. .item-menu-image {
  183. width: 120rpx;
  184. height: 120rpx;
  185. }
  186. </style>