tabNav.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view>
  3. <view v-if="isShow && tabTitle.length">
  4. <!-- #ifdef MP -->
  5. <view style="visibility: hidden;" :style="{ height: navHeight + 'px' }" v-if="isFixed"></view>
  6. <!-- #endif -->
  7. <view class="navTabBox" :style="'color:'+txtColor+';'" :class="{isFixed:isFixed}">
  8. <view class="longTab">
  9. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex; align-items: center; height: 50rpx;" scroll-with-animation
  10. :scroll-left="tabLeft" show-scrollbar="true">
  11. <view :url="'/pages/goods/goods_list/index?cid='+item.id+'&title='+item.cate_name" class="longItem"
  12. :style='"width:"+"max-content"' :data-index="index" :class="index===tabClick?'click':''"
  13. v-for="(item,index) in tabTitle" :key="index" :id="'id'+index"
  14. @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. <view v-if="!isShow && tabTitle.length && isIframe">
  25. <!-- #ifdef MP -->
  26. <view style="visibility: hidden;" :style="{ height: navHeight + 'px' }" v-if="isFixed"></view>
  27. <!-- #endif -->
  28. <view class="navTabBox" :style="'color:'+txtColor+';top:'+isTop" :class="{isFixed:isFixed}">
  29. <view class="longTab">
  30. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex; align-items: center;" scroll-with-animation
  31. :scroll-left="tabLeft" show-scrollbar="true">
  32. <view :url="'/pages/goods/goods_list/index?cid='+item.id+'&title='+item.cate_name" class="longItem"
  33. :style='"width:"+isWidth+"px"' :data-index="index" :class="index===tabClick?'click':''"
  34. v-for="(item,index) in tabTitle" :key="index" :id="'id'+index"
  35. @click="longClick(item,index)">
  36. {{$t(item.cate_name)}}
  37. </view>
  38. <view class="underlineBox" :style='"transform:translateX("+isLeft+"px);width:"+isWidth+"px"'>
  39. <view class="underline"></view>
  40. </view>
  41. </scroll-view>
  42. </view>
  43. </view>
  44. </view>
  45. <view class='index-wrapper' v-else-if="isIframe && !tabTitle.length">
  46. <view class='scroll-product'>
  47. <view class="empty-img">{{$t(`暂无数据,请先添加分类`)}}</view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. let app = getApp()
  54. import {
  55. getCategoryList
  56. } from '@/api/store.js';
  57. export default {
  58. name: 'tabNav',
  59. props: {
  60. dataConfig: {
  61. type: Object,
  62. default: () => {}
  63. },
  64. isFixed: {
  65. type: Boolean | String | Number,
  66. default: false
  67. }
  68. },
  69. data() {
  70. return {
  71. tabTitle: [],
  72. isIframe: app.globalData.isIframe,
  73. tabLeft: 0,
  74. isWidth: 0, //每个导航栏占位
  75. tabClick: 0, //导航栏被点击
  76. isLeft: 0, //导航栏下划线位置
  77. bgColor: '',
  78. // #ifdef H5
  79. mbConfig: 0,
  80. // #endif
  81. // #ifdef MP
  82. mbConfig: 45,
  83. // #endif
  84. txtColor: '#333333',
  85. fixedTop: 0,
  86. isTop: 0,
  87. navHeight: 0,
  88. isShow: true
  89. };
  90. },
  91. watch: {
  92. dataConfig: {
  93. immediate: true,
  94. handler(nVal, oVal) {
  95. if (nVal) {
  96. this.isShow = nVal.isShow.val;
  97. }
  98. }
  99. }
  100. },
  101. created() {
  102. let that = this;
  103. that.getAllCategory();
  104. // 获取设备宽度
  105. uni.getSystemInfo({
  106. success(e) {
  107. that.isWidth = 100
  108. }
  109. })
  110. },
  111. methods: {
  112. // 导航栏点击
  113. longClick(item, index) {
  114. if (this.tabTitle.length > 4) {
  115. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  116. }
  117. this.tabClick = index //设置导航点击了哪一个
  118. this.isLeft = index * this.isWidth //设置下划线位置
  119. this.$emit('bindSortId', item.id)
  120. // setTimeout(e => {
  121. // uni.pageScrollTo({
  122. // scrollTop: this.domOffsetTop - 130,
  123. // duration: 300
  124. // })
  125. // }, 1000)
  126. },
  127. // 获取导航
  128. getAllCategory: function() {
  129. let that = this;
  130. getCategoryList().then(res => {
  131. // res.data.unshift({
  132. // "id": -99,
  133. // 'cate_name': '精选'
  134. // })
  135. this.$emit('bindSortId', res.data[0].id)
  136. that.tabTitle = res.data;
  137. })
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. .navTabBox {
  144. // width: 100%;
  145. // background: linear-gradient(90deg, $bg-star 50%, $bg-end 100%);
  146. color: #E93323;
  147. padding: 0 30rpx;
  148. // &.isFixed {
  149. // z-index: 10000;
  150. // position: fixed;
  151. // // left: 0;
  152. // top: 0;
  153. // // left: 0;
  154. // background-color: #fff;
  155. // // // background: linear-gradient(90deg, var(--view-main-start) 0%, var(--view-main-over) 100%);
  156. // // color: #333 !important;
  157. // // width: 100%;
  158. // // /* #ifdef H5 */
  159. // // padding-top: 20rpx;
  160. // // top: 0;
  161. // // /* #endif */
  162. // }
  163. .longTab {
  164. width: 100%;
  165. .longItem {
  166. height: 50rpx;
  167. display: inline-block;
  168. line-height: 50rpx;
  169. text-align: center;
  170. font-size: 34rpx;
  171. overflow: hidden;
  172. text-overflow: ellipsis;
  173. white-space: nowrap;
  174. margin-right: 40rpx;
  175. &.click {
  176. font-size: 36rpx;
  177. font-weight: bold;
  178. color: var(--view-theme);
  179. }
  180. }
  181. .underlineBox {
  182. height: 3px;
  183. width: 20%;
  184. display: flex;
  185. align-content: center;
  186. justify-content: center;
  187. transition: .5s;
  188. .underline {
  189. width: 33rpx;
  190. height: 4rpx;
  191. background-color: #E93323;
  192. }
  193. }
  194. }
  195. }
  196. .empty-img {
  197. width: 690rpx;
  198. height: 200rpx;
  199. border-radius: 14rpx;
  200. margin: 26rpx auto 0 auto;
  201. background-color: #ccc;
  202. text-align: center;
  203. line-height: 200rpx;
  204. .iconfont {
  205. font-size: 50rpx;
  206. }
  207. }
  208. .sticky-box {
  209. /* #ifdef APP-PLUS || MP */
  210. display: flex;
  211. position: -webkit-sticky;
  212. /* #endif */
  213. position: sticky;
  214. z-index: 998;
  215. flex-direction: row;
  216. margin: 0px;
  217. background: #f5f5f5;
  218. padding: 10rpx 0;
  219. /* #ifdef MP || APP-PLUS*/
  220. //top: 110rpx;
  221. /* #endif */
  222. // overflow-x: scroll;
  223. /deep/ .uni-scroll-view-content{
  224. display: flex;
  225. align-items: center;
  226. width: max-content;
  227. }
  228. }
  229. </style>