index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="">
  3. <navBar title="分类" :back="false" color="white" background="green" />
  4. <u-sticky bgColor="#fff">
  5. <u-tabs :list="list1"></u-tabs>
  6. </u-sticky>
  7. <view class="page-body" :style="'height:' + height + 'px'">
  8. <scroll-view class="nav-left" scroll-y :style="'height:' + height + 'px'" :scroll-top="scrollLeftTop"
  9. scroll-with-animation>
  10. <view class="nav-left-item" @click="categoryClickMain(index)" :key="index"
  11. :class="index == categoryActive ? 'active' : ''" v-for="(item, index) in classifyData">
  12. {{ item.name }}
  13. </view>
  14. </scroll-view>
  15. <scroll-view class="nav-right" scroll-y :scroll-top="scrollTop" @scroll="scroll"
  16. :style="'height:' + height + 'px'" scroll-with-animation>
  17. <view v-for="(foods, index) in classifyData" :key="index" class="box">
  18. <view style="margin-bottom:10upx;font-weight: 600;">{{foods.name}}</view>
  19. <view :id="i == 0 ? 'first' : ''" class="nav-right-item" v-for="(item, i) in foods.foods" :key="i"
  20. @click="cart(item.name)">
  21. <image src="https://img.tnblog.net/bigclassimg/1net.png" />
  22. <view class="right_item_name">{{ item.name }}</view>
  23. <!-- <view class="item_bottom">
  24. <view class="item_img">
  25. <image src="https://img.tnblog.net/bigclassimg/1net.png" />
  26. </view>
  27. <view class="item_des">
  28. <view class="right_item_name">{{ item.name }}</view>
  29. <image src="https://img.tnblog.net/bigclassimg/1net.png" />
  30. </view>
  31. </view> -->
  32. </view>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import classifyData from '@/common/classify.data.js';
  40. export default {
  41. data() {
  42. return {
  43. list1: [{
  44. name: '关注',
  45. }, {
  46. name: '推荐',
  47. }, {
  48. name: '电影'
  49. }, {
  50. name: '科技'
  51. }, {
  52. name: '音乐'
  53. }, {
  54. name: '美食'
  55. }, {
  56. name: '文化'
  57. }, {
  58. name: '财经'
  59. }, {
  60. name: '手工'
  61. }],
  62. name: 'wkiwi',
  63. height: 0,
  64. categoryActive: 0,
  65. scrollTop: 0,
  66. scrollLeftTop: 0,
  67. // scrollHeight: 0,
  68. classifyData: classifyData,
  69. arr: [0, 584, 1168, 1752, 2336, 2805, 3274, 3858, 4442, 4911, 5380, 5734, 6203, 6672,
  70. 7017
  71. ], //初始值,后边计算会根据手机适配覆盖
  72. leftItemHeight: 51, //49行会计算出新值进行覆盖
  73. navLeftHeight: 0, //左边scroll-view 内层nav的总高度
  74. diff: 0, //左边scroll-view 内层nav的总高度与视口之差
  75. tabBarHeight: 0 //如果此页面为Tab页面,自己改变高度值,,一般tab高度为51
  76. };
  77. },
  78. created() {
  79. //如果你的分类数据为后台异步获取请 将下方代码放置你的数据回调中
  80. // this.$nextTick(()=>{
  81. // this.getHeightList();
  82. // })
  83. },
  84. onLoad: function() {
  85. this.height = uni.getSystemInfoSync().windowHeight - this.tabBarHeight;
  86. },
  87. onReady() {
  88. this.getHeightList();
  89. },
  90. methods: {
  91. getHeightList() {
  92. let _this = this;
  93. let selectorQuery = uni.createSelectorQuery();
  94. selectorQuery.selectAll('.nav-left-item').boundingClientRect(function(rects) {
  95. _this.leftItemHeight = rects[0].height;
  96. _this.navLeftHeight = _this.leftItemHeight * classifyData.length;
  97. _this.diff = _this.navLeftHeight - _this.height;
  98. });
  99. selectorQuery
  100. .selectAll('.box')
  101. .boundingClientRect(function(rects) {
  102. let arr = [0];
  103. let top = 0;
  104. rects.forEach(function(rect) {
  105. // rect.id // 节点的ID
  106. // rect.dataset // 节点的dataset
  107. // rect.left // 节点的左边界坐标
  108. // rect.right // 节点的右边界坐标
  109. // rect.top // 节点的上边界坐标
  110. // rect.bottom // 节点的下边界坐标
  111. // rect.width // 节点的宽度
  112. // rect.height // 节点的高度
  113. top += rect.height;
  114. arr.push(top);
  115. });
  116. console.log(arr);
  117. _this.arr = arr;
  118. })
  119. .exec();
  120. },
  121. scroll(e) {
  122. let _this = this;
  123. if (this.timeoutId) {
  124. clearTimeout(this.timeoutId);
  125. }
  126. this.timeoutId = setTimeout(function() {
  127. //节流
  128. _this.scrollHeight = e.detail.scrollTop + 1 + _this.height / 2;
  129. //+1不要删除,解决最后一项某种情况下翻到底部,左边按钮并不会切换至最后一个
  130. //若想使切换参考线为屏幕顶部请删除 _this.height/2
  131. for (let i = 0; i < _this.arr.length; i++) {
  132. let height1 = _this.arr[i];
  133. let height2 = _this.arr[i + 1];
  134. if (!height2 || (_this.scrollHeight >= height1 && _this.scrollHeight < height2)) {
  135. _this.categoryActive = i;
  136. _this.diff > 0 && (_this.scrollLeftTop = Math.round((_this.categoryActive * _this
  137. .diff) / (classifyData.length - 1)));
  138. return false;
  139. }
  140. }
  141. _this.categoryActive = 0;
  142. _this.timeoutId = undefined;
  143. }, 10);
  144. },
  145. categoryClickMain(index) {
  146. this.categoryActive = index;
  147. this.scrollTop == this.arr[index] ? (this.scrollTop = this.scrollTop + 1) : (this.scrollTop = this.arr[
  148. index]); //防止两次相等造成点击不触发滚动时间
  149. },
  150. cart: function(text) {
  151. uni.showToast({
  152. title: text,
  153. icon: 'none'
  154. });
  155. }
  156. },
  157. components: {}
  158. };
  159. </script>
  160. <style lang="scss" scoped>
  161. @import "./index.scss";
  162. </style>