tabNav.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view>
  3. <!-- #ifdef MP || APP-PLUS -->
  4. <view style="visibility: hidden;" :style="{ height: navHeight + 'px' }" v-if="isFixed"></view>
  5. <!-- #endif -->
  6. <view class="navTabBox"
  7. :style="'background: linear-gradient(90deg, '+ bgColor[0].item +' 50%, '+ bgColor[1].item +' 100%);margin-top:'+mbConfig*2+'rpx;color:'+txtColor+';top:'+isTop"
  8. :class="{isFixed:isFixed}">
  9. <view class="longTab">
  10. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
  11. :scroll-left="tabLeft" show-scrollbar="true">
  12. <view :url="'/pages/goods/goods_list/index?cid='+item.id+'&title='+item.cate_name" class="longItem"
  13. :style='"width:"+isWidth+"px"' :data-index="index" :class="index===tabClick?'click':''"
  14. v-for="(item,index) in tabTitle" :key="index" :id="'id'+index" @click="longClick(item,index)">
  15. {{$t(item.cate_name)}}
  16. </view>
  17. <!-- <view class="underlineBox" :style='"transform:translateX("+isLeft+"px);width:"+isWidth+"px"'>
  18. <view class="underline"></view>
  19. </view> -->
  20. </scroll-view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import {
  27. getCategoryList
  28. } from '@/api/store.js';
  29. export default {
  30. name: 'tabNav',
  31. props: {
  32. dataConfig: {
  33. type: Object,
  34. default: () => {}
  35. },
  36. isFixed: {
  37. type: Boolean | String | Number,
  38. default: false
  39. }
  40. },
  41. data() {
  42. return {
  43. tabTitle: [],
  44. tabLeft: 0,
  45. isWidth: 0, //每个导航栏占位
  46. tabClick: 0, //导航栏被点击
  47. isLeft: 0, //导航栏下划线位置
  48. bgColor: this.dataConfig.bgColor.color,
  49. mbConfig: this.dataConfig.mbConfig.val,
  50. txtColor: this.dataConfig.txtColor.color[0].item,
  51. fixedTop: 0,
  52. isTop: 0,
  53. navHeight: 0
  54. };
  55. },
  56. created() {
  57. let that = this;
  58. that.getAllCategory();
  59. // 获取设备宽度
  60. uni.getSystemInfo({
  61. success(e) {
  62. that.isWidth = e.windowWidth / 5
  63. }
  64. })
  65. },
  66. methods: {
  67. // 导航栏点击
  68. longClick(item, index) {
  69. if (this.tabTitle.length > 5) {
  70. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  71. }
  72. this.tabClick = index //设置导航点击了哪一个
  73. this.isLeft = index * this.isWidth //设置下划线位置
  74. this.$emit('bindSortId', item.id)
  75. },
  76. // 获取导航
  77. getAllCategory: function() {
  78. let that = this;
  79. getCategoryList().then(res => {
  80. // res.data.unshift({
  81. // "id": -99,
  82. // 'cate_name': '首页'
  83. // })
  84. that.tabTitle = res.data;
  85. setTimeout((e) => {
  86. const query = uni.createSelectorQuery().in(this);
  87. query.select('.navTabBox').boundingClientRect(data => {
  88. that.domOffsetTop = data.top
  89. that.navHeight = data.height
  90. that.$emit('bindHeight', data)
  91. }).exec();
  92. }, 200)
  93. // #ifdef MP || APP-PLUS
  94. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  95. // #endif
  96. // #ifdef H5
  97. this.isTop = 0
  98. // #endif
  99. })
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss">
  105. .navTabBox {
  106. width: 100%;
  107. background: linear-gradient(90deg, $bg-star 50%, $bg-end 100%);
  108. color: rgba(255, 255, 255, 1);
  109. // padding-bottom: 20rpx;
  110. &.isFixed {
  111. z-index: 45;
  112. position: fixed;
  113. left: 0;
  114. width: 100%;
  115. /* #ifdef H5 */
  116. padding-top: 20rpx;
  117. top: 0;
  118. /* #endif */
  119. }
  120. .longTab {
  121. width: 100%;
  122. .longItem {
  123. height: 50rpx;
  124. display: inline-block;
  125. line-height: 50rpx;
  126. text-align: center;
  127. font-size: 30rpx;
  128. overflow: hidden;
  129. text-overflow: ellipsis;
  130. white-space: nowrap;
  131. &.click {
  132. font-weight: bold;
  133. }
  134. }
  135. .underlineBox {
  136. height: 3px;
  137. width: 20%;
  138. display: flex;
  139. align-content: center;
  140. justify-content: center;
  141. transition: .5s;
  142. .underline {
  143. width: 33rpx;
  144. height: 4rpx;
  145. background-color: white;
  146. }
  147. }
  148. }
  149. }
  150. .child-box {
  151. width: 100%;
  152. position: relative;
  153. background-color: #fff;
  154. box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.02);
  155. .wrapper {
  156. display: flex;
  157. align-items: center;
  158. padding: 20rpx 0;
  159. background: #fff;
  160. }
  161. .child-item {
  162. flex-shrink: 0;
  163. width: 140rpx;
  164. display: flex;
  165. flex-direction: column;
  166. align-items: center;
  167. justify-content: center;
  168. margin-left: 10rpx;
  169. image {
  170. width: 90rpx;
  171. height: 90rpx;
  172. border-radius: 50%;
  173. }
  174. .txt {
  175. font-size: 24rpx;
  176. color: #282828;
  177. text-align: center;
  178. margin-top: 10rpx;
  179. }
  180. &.on {
  181. image {
  182. border: 1px solid $theme-color-opacity;
  183. }
  184. .txt {
  185. color: $theme-color;
  186. }
  187. }
  188. }
  189. }
  190. </style>