tabNav.vue 4.9 KB

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