You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.1 KiB

3 months ago
  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="'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. let app = getApp();
  33. export default {
  34. name: 'tabNav',
  35. props: {
  36. dataConfig: {
  37. type: Object,
  38. default: () => {}
  39. },
  40. //是否固定
  41. isFixed: {
  42. type: Boolean,
  43. default: false
  44. }
  45. },
  46. data() {
  47. return {
  48. tabClick: 0, //导航栏被点击
  49. isLeft: 0, //导航栏下划线位置
  50. isWidth: 0, //每个导航栏占位
  51. mainWidth: 0,
  52. tabLeft: 0,
  53. isTop: 0,
  54. navHeight: 35,
  55. tabItem: null ,//tab选中的对象
  56. themeColor:this.$options.filters.filterTheme(app.globalData.theme)
  57. };
  58. },
  59. computed: {
  60. //外部盒子
  61. boxStyle() {
  62. return {
  63. borderRadius: this.dataConfig.bgStyle.val * 2 + 'rpx',
  64. background: `linear-gradient(${this.dataConfig.bgColor.color[0].item}, ${this.dataConfig.bgColor.color[1].item})`,
  65. margin: 0 + ' ' + 2*this.dataConfig.lrConfig.val + 'rpx' + ' ' + 0 ,
  66. // padding: 2*this.dataConfig.upConfig.val + 'rpx' + ' ' + '20rpx' + ' ' + 2*this.dataConfig.downConfig.val + 'rpx' ,
  67. }
  68. },
  69. //标签文字颜色
  70. fontColor() {
  71. return this.dataConfig.fontColor.color[0].item
  72. },
  73. //选中颜色
  74. checkColor() {
  75. return this.dataConfig.themeStyleConfig.tabVal?this.dataConfig.checkColor.color[0].item:this.themeColor
  76. },
  77. tabList() {
  78. //type=0微页面,1分类,2首页
  79. let tabList = this.dataConfig.listConfig.list;
  80. tabList.unshift({
  81. title: '首页',
  82. type: 2,
  83. val: 0
  84. })
  85. return tabList
  86. },
  87. },
  88. created() {
  89. let that = this
  90. // 获取设备宽度
  91. uni.getSystemInfo({
  92. success(e) {
  93. //that.mainWidth = e.windowWidth
  94. that.isWidth = (e.windowWidth) / 5
  95. }
  96. })
  97. setTimeout((e) => {
  98. let statusHeight = uni.getSystemInfoSync().statusBarHeight;
  99. const query = uni.createSelectorQuery().in(this);
  100. query.select('.navTabBox').boundingClientRect(data => {
  101. that.navHeight = (data.height + statusHeight) * 2;
  102. }).exec();
  103. }, 300)
  104. that.$nextTick(function() {
  105. uni.getSystemInfo({
  106. success: function(res) {
  107. that.windowHeight = res.windowHeight;
  108. }
  109. });
  110. })
  111. // #ifdef MP || APP-PLUS
  112. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  113. // #endif
  114. // #ifdef H5
  115. this.isTop = 0
  116. // #endif
  117. },
  118. watch: {
  119. tabClick: {
  120. handler(newValue, oldValue) {
  121. if (this.tabItem) this.$emit('changeTab', newValue, this.tabItem);
  122. },
  123. immediate: true
  124. }
  125. },
  126. methods: {
  127. // 导航栏点击
  128. longClick(index, item) {
  129. this.tabItem = item;
  130. this.tabClick = index; //设置导航点击了哪一个
  131. this.$nextTick(() => {
  132. let id = 'id' + index;
  133. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  134. //this.$emit('changeTab', index,item);
  135. })
  136. },
  137. }
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. .navTabBox {
  142. width: 100%;
  143. height: 70rpx;
  144. color: rgba(255, 255, 255, 1);
  145. position: fixed;
  146. z-index: 99;
  147. padding: 0 20rpx;
  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. margin-right: 30rpx;
  172. &.click {
  173. font-weight: bold;
  174. font-size: 30rpx;
  175. position: relative;
  176. &::after {
  177. content: '';
  178. width: 40rpx;
  179. height: 4rpx;
  180. background: var(--color);
  181. // background-color: #E93323;
  182. position: absolute;
  183. bottom: 0;
  184. left: 50%;
  185. transform: translateX(-50%);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. </style>