tabNav.vue 5.1 KB

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