tabNav.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <!-- 商品分类 -->
  3. <view>
  4. <!-- #ifdef MP || APP-PLUS -->
  5. <view :style="{ height: navHeight + 'px' }" v-if="!fromType">{{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;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"></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. txtColor: this.dataConfig.txtColor.color[0].item,
  56. fixedTop: 0,
  57. isTop: 0,
  58. navHeight: 0
  59. };
  60. },
  61. created() {
  62. let that = this;
  63. that.getAllCategory();
  64. // 获取设备宽度
  65. uni.getSystemInfo({
  66. success(e) {
  67. that.isWidth = e.windowWidth / 5
  68. }
  69. })
  70. },
  71. methods: {
  72. // 导航栏点击
  73. longClick(item, index) {
  74. if (this.tabTitle.length > 5) {
  75. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  76. }
  77. this.tabClick = index //设置导航点击了哪一个
  78. this.isLeft = index * this.isWidth //设置下划线位置
  79. this.$emit('bindSortId', item.id)
  80. },
  81. // 获取导航
  82. getAllCategory: function() {
  83. let that = this;
  84. getCategoryList().then(res => {
  85. res.data.unshift({
  86. "id": -99,
  87. 'cate_name': '首页'
  88. })
  89. that.tabTitle = res.data;
  90. that.$nextTick(function(){
  91. setTimeout((e) => {
  92. if(res.data.length){
  93. const query = uni.createSelectorQuery().in(this);
  94. query.select('.navTabBox').boundingClientRect(data => {
  95. that.domOffsetTop = data.top
  96. that.navHeight = data.height
  97. that.$emit('bindHeight', data)
  98. }).exec();
  99. }
  100. }, 200)
  101. })
  102. // #ifdef MP || APP-PLUS
  103. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  104. // #endif
  105. // #ifdef H5
  106. this.isTop = 0
  107. // #endif
  108. })
  109. }
  110. }
  111. }
  112. </script>
  113. <style lang="scss">
  114. .navTabBox {
  115. width: 100%;
  116. background: linear-gradient(90deg, $bg-star 50%, $bg-end 100%);
  117. color: rgba(255, 255, 255, 1);
  118. padding-bottom: 20rpx;
  119. &.isFixed {
  120. z-index: 45;
  121. position: fixed;
  122. left: 0;
  123. width: 100%;
  124. /* #ifdef H5 */
  125. padding-top: 20rpx;
  126. top: 0;
  127. /* #endif */
  128. }
  129. .longTab {
  130. width: 100%;
  131. .longItem {
  132. height: 50rpx;
  133. display: inline-block;
  134. line-height: 50rpx;
  135. text-align: center;
  136. font-size: 30rpx;
  137. overflow: hidden;
  138. text-overflow: ellipsis;
  139. white-space: nowrap;
  140. &.click {
  141. font-weight: bold;
  142. }
  143. }
  144. .underlineBox {
  145. height: 3px;
  146. width: 20%;
  147. display: flex;
  148. align-content: center;
  149. justify-content: center;
  150. transition: .5s;
  151. .underline {
  152. width: 33rpx;
  153. height: 4rpx;
  154. background-color: white;
  155. }
  156. }
  157. }
  158. }
  159. .child-box {
  160. width: 100%;
  161. position: relative;
  162. background-color: #fff;
  163. box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.02);
  164. .wrapper {
  165. display: flex;
  166. align-items: center;
  167. padding: 20rpx 0;
  168. background: #fff;
  169. }
  170. .child-item {
  171. flex-shrink: 0;
  172. width: 140rpx;
  173. display: flex;
  174. flex-direction: column;
  175. align-items: center;
  176. justify-content: center;
  177. margin-left: 10rpx;
  178. image {
  179. width: 90rpx;
  180. height: 90rpx;
  181. border-radius: 50%;
  182. }
  183. .txt {
  184. font-size: 24rpx;
  185. color: #282828;
  186. text-align: center;
  187. margin-top: 10rpx;
  188. }
  189. &.on {
  190. image {
  191. border: 1px solid $theme-color-opacity;
  192. }
  193. .txt {
  194. color: $theme-color;
  195. }
  196. }
  197. }
  198. }
  199. </style>