index1.vue 4.4 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" :class="[current==index ? 'u-tab-item-active' : '']"
  12. :data-current="index" @tap.stop="swichMenu(index)">
  13. <text class="u-line-1">{{item.name}}</text>
  14. </view>
  15. </scroll-view>
  16. <block v-for="(item,index) in tabbar" :key="index">
  17. <scroll-view scroll-y class="right-box" v-if="current==index">
  18. <view class="page-view">
  19. <view class="class-item">
  20. <view class="item-title">
  21. <text>{{item.name}}</text>
  22. </view>
  23. <view class="item-container">
  24. <view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1">
  25. <image class="item-menu-image" :src="item1.icon" mode=""></image>
  26. <view class="item-menu-name">{{item1.name}}</view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </scroll-view>
  32. </block>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import classifyData from "@/common/classify.data.js";
  38. export default {
  39. data() {
  40. return {
  41. tabbar: classifyData,
  42. scrollTop: 0, //tab标题的滚动条位置
  43. current: 0, // 预设当前项的值
  44. menuHeight: 0, // 左边菜单的高度
  45. menuItemHeight: 0, // 左边菜单item的高度
  46. }
  47. },
  48. computed: {
  49. },
  50. methods: {
  51. getImg() {
  52. return Math.floor(Math.random() * 35);
  53. },
  54. // 点击左边的栏目切换
  55. async swichMenu(index) {
  56. if(index == this.current) return ;
  57. this.current = index;
  58. // 如果为0,意味着尚未初始化
  59. if(this.menuHeight == 0 || this.menuItemHeight == 0) {
  60. await this.getElRect('menu-scroll-view', 'menuHeight');
  61. await this.getElRect('u-tab-item', 'menuItemHeight');
  62. }
  63. // 将菜单菜单活动item垂直居中
  64. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  65. },
  66. // 获取一个目标元素的高度
  67. getElRect(elClass, dataVal) {
  68. new Promise((resolve, reject) => {
  69. const query = uni.createSelectorQuery().in(this);
  70. query.select('.' + elClass).fields({size: true},res => {
  71. // 如果节点尚未生成,res值为null,循环调用执行
  72. if(!res) {
  73. setTimeout(() => {
  74. this.getElRect(elClass);
  75. }, 10);
  76. return ;
  77. }
  78. this[dataVal] = res.height;
  79. }).exec();
  80. })
  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-type-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>