cateNav.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <!-- 商品分类 -->
  3. <view>
  4. <!-- #ifdef MP || APP-PLUS -->
  5. <!-- <view style="visibility: hidden;" :style="{ height: navHeight + 'rpx' }"></view> -->
  6. <!-- #endif -->
  7. <view class="navTabBox" :class="{isFixed:isFixed}" :style="[boxStyle]">
  8. <view class="longTab">
  9. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation
  10. :scroll-left="tabLeft" show-scrollbar="true">
  11. <view class="longItem"
  12. :style="'width:'+isWidth+'px;color:' + (index == tabClick ? checkColor : fontColor)+';--color:'+checkColor"
  13. :data-index="index" :class="index===tabClick?'click':''" v-for="(item,index) in tabList"
  14. :key="index" :id="'id'+index" @click="longClick(index,item)">{{ item.title }}
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </view>
  19. <view style="height: 70rpx"></view>
  20. </view>
  21. </template>
  22. <script>
  23. // +----------------------------------------------------------------------
  24. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  25. // +----------------------------------------------------------------------
  26. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  27. // +----------------------------------------------------------------------
  28. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  29. // +----------------------------------------------------------------------
  30. // | Author: CRMEB Team <admin@crmeb.com>
  31. // +----------------------------------------------------------------------
  32. import {
  33. getCategoryFirst,
  34. } from '@/api/api.js';
  35. let app = getApp();
  36. export default {
  37. name: 'tabNav',
  38. props: {
  39. dataConfig: {
  40. type: Object,
  41. default: () => {}
  42. },
  43. //是否固定
  44. isFixed: {
  45. type: Boolean,
  46. default: false
  47. }
  48. },
  49. data() {
  50. return {
  51. tabClick: 0, //导航栏被点击
  52. isLeft: 0, //导航栏下划线位置
  53. isWidth: 0, //每个导航栏占位
  54. mainWidth: 0,
  55. tabLeft: 0,
  56. isTop: 0,
  57. navHeight: 35,
  58. tabItem: null ,//tab选中的对象
  59. themeColor:this.$options.filters.filterTheme(app.globalData.theme)
  60. };
  61. },
  62. computed: {
  63. //外部盒子
  64. boxStyle() {
  65. return {
  66. borderRadius: this.dataConfig.bgStyle.val * 2 + 'rpx',
  67. background: `linear-gradient(${this.dataConfig.bgColor.color[0].item}, ${this.dataConfig.bgColor.color[1].item})`,
  68. }
  69. },
  70. //标签文字颜色
  71. fontColor() {
  72. return this.dataConfig.fontColor.color[0].item
  73. },
  74. //选中颜色
  75. checkColor() {
  76. return this.dataConfig.themeStyleConfig.tabVal?this.dataConfig.checkColor.color[0].item:this.themeColor
  77. },
  78. tabList() {
  79. //type=0微页面,1分类,2首页
  80. let tabList = this.dataConfig.listConfig.list;
  81. tabList.unshift({
  82. title: '首页',
  83. type: 2,
  84. val: 0
  85. })
  86. return tabList
  87. },
  88. },
  89. created() {
  90. let that = this
  91. // 获取设备宽度
  92. uni.getSystemInfo({
  93. success(e) {
  94. //that.mainWidth = e.windowWidth
  95. that.isWidth = (e.windowWidth) / 5
  96. }
  97. })
  98. setTimeout((e) => {
  99. let statusHeight = uni.getSystemInfoSync().statusBarHeight;
  100. const query = uni.createSelectorQuery().in(this);
  101. query.select('.navTabBox').boundingClientRect(data => {
  102. that.navHeight = (data.height + statusHeight) * 2;
  103. }).exec();
  104. }, 300)
  105. that.$nextTick(function() {
  106. uni.getSystemInfo({
  107. success: function(res) {
  108. that.windowHeight = res.windowHeight;
  109. }
  110. });
  111. })
  112. // #ifdef MP || APP-PLUS
  113. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  114. // #endif
  115. // #ifdef H5
  116. this.isTop = 0
  117. // #endif
  118. },
  119. watch: {
  120. tabClick: {
  121. handler(newValue, oldValue) {
  122. if (this.tabItem) this.$emit('changeTab', newValue, this.tabItem);
  123. },
  124. immediate: true
  125. }
  126. },
  127. methods: {
  128. // 导航栏点击
  129. longClick(index, item) {
  130. this.tabItem = item;
  131. this.tabClick = index; //设置导航点击了哪一个
  132. this.$nextTick(() => {
  133. let id = 'id' + index;
  134. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  135. //this.$emit('changeTab', index,item);
  136. })
  137. },
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .navTabBox {
  143. width: 100%;
  144. height: 70rpx;
  145. color: rgba(255, 255, 255, 1);
  146. position: fixed;
  147. z-index: 99;
  148. &.isFixed {
  149. z-index: 10;
  150. position: fixed;
  151. left: 0;
  152. width: 100%;
  153. /* #ifdef H5 */
  154. top: 0;
  155. /* #endif */
  156. }
  157. .click {
  158. color: white;
  159. }
  160. .longTab {
  161. .longItem {
  162. height: 70rpx;
  163. display: inline-block;
  164. line-height: 70rpx;
  165. text-align: center;
  166. font-size: 28rpx;
  167. color: #333333;
  168. white-space: nowrap;
  169. overflow: hidden;
  170. text-overflow: ellipsis;
  171. &.click {
  172. font-weight: bold;
  173. font-size: 30rpx;
  174. position: relative;
  175. &::after {
  176. content: '';
  177. width: 40rpx;
  178. height: 4rpx;
  179. background: var(--color);
  180. // background-color: #E93323;
  181. position: absolute;
  182. bottom: 0;
  183. left: 50%;
  184. transform: translateX(-50%);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. </style>