tablist.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="content">
  3. <view class="nav">
  4. <!-- 选项卡水平方向滑动,scroll-with-animation是滑动到下一个选项时,有一个延时效果 -->
  5. <scroll-view
  6. class="tab-scroll"
  7. scroll-x="true"
  8. scroll-with-animation
  9. :scroll-left="scrollLeft"
  10. >
  11. <view class="tab-scroll_box" :style="{'justify-content': category.length <= 3 ? 'space-around' : 'flex-start'}">
  12. <!-- 选项卡类别列表 -->
  13. <view
  14. class="tab-scroll_item"
  15. v-for="(item, index) in category"
  16. :key="index"
  17. :class="{ active: isActive == index }"
  18. @click="chenked(index, item)"
  19. >
  20. <view class="tile-box">
  21. <text class="name-box">{{ item.name }}</text>
  22. <text class="count">
  23. {{ item.count || 0 }}
  24. </text>
  25. </view>
  26. </view>
  27. </view>
  28. </scroll-view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. props: {
  35. category: {
  36. type: Array,
  37. default: () => []
  38. },
  39. isShowNumber: {
  40. type: Boolean,
  41. default: true
  42. },
  43. curruntActive: {
  44. type: [Number, String],
  45. default: 0
  46. }
  47. },
  48. watch: {
  49. // swiper与上面选项卡联动
  50. currentindex(newval) {
  51. this.isActive = newval;
  52. this.scrollLeft = 0;
  53. // 滑动swiper后,每个选项距离其父元素最左侧的距离
  54. for (let i = 0; i < newval - 1; i++) {
  55. this.scrollLeft += this.category[i].width;
  56. }
  57. }
  58. },
  59. data() {
  60. return {
  61. isActive: 0,
  62. index: 0,
  63. currentindex: 0,
  64. contentScrollW: 0, // 导航区宽度
  65. scrollLeft: 0, // 横向滚动条位置
  66. fullHeight: ""
  67. };
  68. },
  69. mounted() {
  70. var that = this;
  71. this.isActive = this.curruntActive;
  72. //获取手机屏幕的高度,让其等于swiper的高度,从而使屏幕充满
  73. uni.getSystemInfo({
  74. success: function (res) {
  75. that.fullHeight = "height:" + res.windowHeight + "px";
  76. }
  77. });
  78. // 获取标题区域宽度,和每个子元素节点的宽度
  79. this.getScrollW();
  80. },
  81. methods: {
  82. // 获取标题区域宽度,和每个子元素节点的宽度以及元素距离左边栏的距离
  83. getScrollW() {
  84. const query = uni.createSelectorQuery().in(this);
  85. query
  86. .select(".tab-scroll")
  87. .boundingClientRect((data) => {
  88. this.contentScrollW = data.width;
  89. })
  90. .exec();
  91. query
  92. .selectAll(".tab-scroll_item")
  93. .boundingClientRect((data) => {
  94. let dataLen = data.length;
  95. for (let i = 0; i < dataLen; i++) {
  96. this.category[i].left = data[i].left;
  97. this.category[i].width = data[i].width;
  98. }
  99. })
  100. .exec();
  101. },
  102. // 当前点击子元素靠左留一个选项展示,子元素宽度不相同也可实现
  103. chenked(index, item) {
  104. this.isActive = index;
  105. this.scrollLeft = 0;
  106. for (let i = 0; i < index - 1; i++) {
  107. this.scrollLeft += this.category[i].width;
  108. }
  109. this.$emit("tabs", { index, item });
  110. },
  111. // swiper滑动时,获取其索引,也就是第几个
  112. change(e) {
  113. const { current } = e.detail;
  114. this.currentindex = current;
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="scss">
  120. .content {
  121. display: flex;
  122. flex-direction: column;
  123. width: 100%;
  124. flex: 1;
  125. .nav {
  126. background-color: #fff;
  127. // position: fixed;
  128. z-index: 99;
  129. width: 100%;
  130. align-items: center;
  131. // min-height: 40rpx;
  132. .tab-scroll {
  133. flex: 1;
  134. overflow: hidden;
  135. box-sizing: border-box;
  136. padding-left: 30rpx;
  137. padding-right: 30rpx;
  138. .tab-scroll_box {
  139. display: flex;
  140. align-items: center;
  141. flex-wrap: nowrap;
  142. box-sizing: border-box;
  143. // height: 100rpx;
  144. .tab-scroll_item {
  145. // line-height: 60rpx;
  146. margin-right: 35rpx;
  147. flex-shrink: 0;
  148. display: flex;
  149. justify-content: center;
  150. font-size: 30rpx;
  151. padding-top: 10rpx;
  152. color: #333;
  153. .tile-box {
  154. display: flex;
  155. align-items: center;
  156. flex-direction: column;
  157. .name-box{
  158. }
  159. .count {
  160. padding: 13rpx 0;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. .swiper-content {
  168. padding-top: 120rpx;
  169. // flex: 1;
  170. .swiperitem-content {
  171. background-color: #ffffff;
  172. .nav_item {
  173. background-color: #ffffff;
  174. padding: 20rpx 40rpx 0rpx 40rpx;
  175. }
  176. }
  177. }
  178. }
  179. .active {
  180. position: relative;
  181. color: #1f7cff !important;
  182. font-weight: 600;
  183. }
  184. .active::after {
  185. content: "";
  186. position: absolute;
  187. width: 50rpx;
  188. height: 8rpx;
  189. background-color: #1f7cff;
  190. left: 0px;
  191. right: 0px;
  192. bottom: 0px;
  193. margin: auto;
  194. border-radius: 8rpx;
  195. margin-top: 10rpx;
  196. transition: all 0.3s;
  197. }
  198. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  199. /deep/.uni-scroll-view::-webkit-scrollbar {
  200. display: none;
  201. }
  202. </style>