liandong.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="page-body" :style="'height:'+height+'px'">
  3. <scroll-view class="nav-left" scroll-y :style="'height:'+height+'px'" :scroll-top="scrollLeftTop" scroll-with-animation>
  4. <view class="nav-left-item" @click="categoryClickMain(index)" :key="index" :class="index==categoryActive?'active':''"
  5. v-for="(item,index) in classifyData">
  6. {{item.name}}
  7. </view>
  8. </scroll-view>
  9. <scroll-view class="nav-right" scroll-y :scroll-top="scrollTop" @scroll="scroll" :style="'height:'+height+'px'" scroll-with-animation>
  10. <view v-for="(foods,index) in classifyData" :key="index" class="box">
  11. <view :id="i==0?'first':''" class="nav-right-item" v-for="(item,i) in foods.foods" :key="i" @click="cart(item.name)">
  12. <image :src="item.icon" />
  13. <view>{{item.name}}</view>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </template>
  19. <script>
  20. import classifyData from '@/components/data.js';
  21. export default {
  22. data() {
  23. return {
  24. name: "wkiwi",
  25. height: 0,
  26. categoryActive: 0,
  27. scrollTop: 0,
  28. scrollLeftTop: 0,
  29. // scrollHeight: 0,
  30. classifyData:classifyData,
  31. arr:[0,584,1168,1752,2336,2805,3274,3858,4442,4911,5380,5734,6203,6672,7017],//初始值,后边计算会根据手机适配覆盖
  32. leftItemHeight: 51,//49行会计算出新值进行覆盖
  33. navLeftHeight:0,//左边scroll-view 内层nav的总高度
  34. diff: 0,//左边scroll-view 内层nav的总高度与视口之差
  35. tabBarHeight:0,//如果此页面为Tab页面,自己改变高度值,,一般tab高度为51
  36. }
  37. },
  38. created(){
  39. this.height = uni.getSystemInfoSync().windowHeight - this.tabBarHeight;
  40. console.log(1111)
  41. //如果你的分类数据为后台异步获取请 将下方代码放置你的数据回调中
  42. // this.$nextTick(()=>{
  43. // this.getHeightList();
  44. // })
  45. },
  46. onLoad() {
  47. console.log(1111)
  48. },
  49. onReady() {
  50. this.getHeightList();
  51. },
  52. methods: {
  53. getHeightList(){
  54. let _this = this;
  55. let selectorQuery=uni.createSelectorQuery();
  56. selectorQuery.selectAll('.nav-left-item').boundingClientRect(function(rects) {
  57. _this.leftItemHeight = rects[0].height;
  58. _this.navLeftHeight = _this.leftItemHeight * classifyData.length;
  59. _this.diff = _this.navLeftHeight - _this.height;
  60. });
  61. selectorQuery.selectAll('.box').boundingClientRect(function(rects) {
  62. let arr = [0];
  63. let top = 0;
  64. rects.forEach(function(rect){
  65. top += rect.height;
  66. arr.push(top)
  67. })
  68. console.log(arr)
  69. _this.arr = arr
  70. }).exec()
  71. },
  72. scroll(e) {
  73. let _this = this
  74. if(this.timeoutId){
  75. clearTimeout(this.timeoutId);
  76. }
  77. this.timeoutId = setTimeout(function(){
  78. _this.scrollHeight = e.detail.scrollTop + 1 + _this.height/2;
  79. for (let i = 0; i < _this.arr.length;i++) {
  80. let height1 = _this.arr[i];
  81. let height2 = _this.arr[i+1];
  82. if (!height2 || (_this.scrollHeight >= height1 && _this.scrollHeight < height2)) {
  83. _this.categoryActive = i;
  84. (_this.diff>0) && (_this.scrollLeftTop = Math.round((_this.categoryActive * _this.diff)/(classifyData.length-1)));
  85. return false;
  86. }
  87. }
  88. _this.categoryActive = 0;
  89. _this.timeoutId = undefined;
  90. }, 10)
  91. },
  92. categoryClickMain(index) {
  93. this.categoryActive = index;
  94. this.scrollTop == this.arr[index] ? this.scrollTop = this.scrollTop+1 : this.scrollTop = this.arr[index]//防止两次相等造成点击不触发滚动时间
  95. },
  96. cart: function (text) {
  97. uni.showToast({
  98. title: text,
  99. icon: "none",
  100. })
  101. }
  102. },
  103. components: {
  104. }
  105. }
  106. </script>
  107. <style>
  108. .page-body {
  109. display: flex;
  110. background: #fff;
  111. overflow: hidden;
  112. }
  113. .nav {
  114. display: flex;
  115. width: 100%;
  116. }
  117. .nav-left {
  118. width: 25%;
  119. background: #fafafa;
  120. }
  121. .nav-left-item {
  122. height: 100upx;
  123. border-right: solid 1px #f1f1f1;
  124. border-bottom: solid 1px #f1f1f1;
  125. font-size: 30upx;
  126. display: flex;
  127. align-items: center;
  128. justify-content: center;
  129. }
  130. .nav-left-item:last-child{
  131. border-bottom: none;
  132. }
  133. .nav-right {
  134. width: 75%;
  135. }
  136. .box {
  137. display: block;
  138. overflow: hidden;
  139. border-bottom: 20upx solid #f3f3f3;
  140. }
  141. .box:last-child {
  142. border: none;
  143. min-height:100vh;
  144. }
  145. .nav-right-item {
  146. width: 28%;
  147. height: 220upx;
  148. float: left;
  149. text-align: center;
  150. padding: 11upx;
  151. font-size: 28upx;
  152. background: #fff;
  153. }
  154. .nav-right-item image {
  155. width: 150upx;
  156. height: 150upx;
  157. }
  158. .active {
  159. color: #FF80AB;
  160. background: #fff;
  161. border-right: 0;
  162. }
  163. ::-webkit-scrollbar {/*取消小程序的默认导航条样式*/
  164. width: 0;
  165. height: 0;
  166. color: transparent;
  167. }
  168. </style>