index2.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="">
  3. <navBar title="分类" :back="false" color="white" background="green" />
  4. <u-sticky bgColor="#fff">
  5. <u-tabs :list="list1" :scrollable="false"></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: 'wkiwi',
  51. height: 0,
  52. categoryActive: 0,
  53. scrollTop: 0,
  54. scrollLeftTop: 0,
  55. // scrollHeight: 0,
  56. classifyData: classifyData,
  57. arr: [0, 584, 1168, 1752, 2336, 2805, 3274, 3858, 4442, 4911, 5380, 5734, 6203, 6672,
  58. 7017
  59. ], //初始值,后边计算会根据手机适配覆盖
  60. leftItemHeight: 51, //49行会计算出新值进行覆盖
  61. navLeftHeight: 0, //左边scroll-view 内层nav的总高度
  62. diff: 0, //左边scroll-view 内层nav的总高度与视口之差
  63. tabBarHeight: 0 //如果此页面为Tab页面,自己改变高度值,,一般tab高度为51
  64. };
  65. },
  66. created() {
  67. //如果你的分类数据为后台异步获取请 将下方代码放置你的数据回调中
  68. // this.$nextTick(()=>{
  69. // this.getHeightList();
  70. // })
  71. },
  72. onLoad: function() {
  73. this.height = uni.getSystemInfoSync().windowHeight - this.tabBarHeight;
  74. },
  75. onReady() {
  76. this.getHeightList();
  77. },
  78. methods: {
  79. getHeightList() {
  80. let _this = this;
  81. let selectorQuery = uni.createSelectorQuery();
  82. selectorQuery.selectAll('.nav-left-item').boundingClientRect(function(rects) {
  83. _this.leftItemHeight = rects[0].height;
  84. _this.navLeftHeight = _this.leftItemHeight * classifyData.length;
  85. _this.diff = _this.navLeftHeight - _this.height;
  86. });
  87. selectorQuery
  88. .selectAll('.box')
  89. .boundingClientRect(function(rects) {
  90. let arr = [0];
  91. let top = 0;
  92. rects.forEach(function(rect) {
  93. // rect.id // 节点的ID
  94. // rect.dataset // 节点的dataset
  95. // rect.left // 节点的左边界坐标
  96. // rect.right // 节点的右边界坐标
  97. // rect.top // 节点的上边界坐标
  98. // rect.bottom // 节点的下边界坐标
  99. // rect.width // 节点的宽度
  100. // rect.height // 节点的高度
  101. top += rect.height;
  102. arr.push(top);
  103. });
  104. console.log(arr);
  105. _this.arr = arr;
  106. })
  107. .exec();
  108. },
  109. scroll(e) {
  110. let _this = this;
  111. if (this.timeoutId) {
  112. clearTimeout(this.timeoutId);
  113. }
  114. this.timeoutId = setTimeout(function() {
  115. //节流
  116. _this.scrollHeight = e.detail.scrollTop + 1 + _this.height / 2;
  117. //+1不要删除,解决最后一项某种情况下翻到底部,左边按钮并不会切换至最后一个
  118. //若想使切换参考线为屏幕顶部请删除 _this.height/2
  119. for (let i = 0; i < _this.arr.length; i++) {
  120. let height1 = _this.arr[i];
  121. let height2 = _this.arr[i + 1];
  122. if (!height2 || (_this.scrollHeight >= height1 && _this.scrollHeight < height2)) {
  123. _this.categoryActive = i;
  124. _this.diff > 0 && (_this.scrollLeftTop = Math.round((_this.categoryActive * _this
  125. .diff) / (classifyData.length - 1)));
  126. return false;
  127. }
  128. }
  129. _this.categoryActive = 0;
  130. _this.timeoutId = undefined;
  131. }, 10);
  132. },
  133. categoryClickMain(index) {
  134. this.categoryActive = index;
  135. this.scrollTop == this.arr[index] ? (this.scrollTop = this.scrollTop + 1) : (this.scrollTop = this.arr[
  136. index]); //防止两次相等造成点击不触发滚动时间
  137. },
  138. cart: function(text) {
  139. uni.showToast({
  140. title: text,
  141. icon: 'none'
  142. });
  143. }
  144. },
  145. components: {}
  146. };
  147. </script>
  148. <style lang="scss" scoped>
  149. @import "./index.scss";
  150. </style>