tabNav.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <!-- 商品分类 -->
  3. <view>
  4. <!-- #ifdef MP || APP-PLUS -->
  5. <!-- <view :style="{height: (40+dataConfig.topConfig.val*2+dataConfig.bottomConfig.val*2) + 'rpx'}" v-if="!fromType"></view> -->
  6. <!-- #endif -->
  7. <view :class="{
  8. tabNav1: dataConfig.styleConfig.tabVal == 0,
  9. tabNav2: dataConfig.styleConfig.tabVal == 1,
  10. tabNav3: dataConfig.styleConfig.tabVal == 2,
  11. isFixed: isFixed
  12. }">
  13. <view :style="[tabNavStyle]">
  14. <view :style="[tabNavBgColor]">
  15. <scroll-view scroll-x="true" :scroll-left="tabLeft" scroll-with-animation>
  16. <view class="list acea-row row-middle">
  17. <view v-for="(item, index) in tabListConfig" :key="index" :class="{ on: index == tabClick }" :style="[
  18. index == tabClick ? textStyle : {}
  19. ]" class="item" @click="longClick(item, index)">
  20. {{ item.text.val }}
  21. <text v-if="dataConfig.styleConfig.tabVal != 2" class="line" :style="[lineStyle]"></text>
  22. </view>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </view>
  27. </view>
  28. <view v-if="isFixed" style="height: 80rpx"></view>
  29. </view>
  30. </template>
  31. <script>
  32. import {
  33. getCategoryList
  34. } from '@/api/store.js';
  35. import {
  36. getCategoryVersion
  37. } from '@/api/api.js';
  38. export default {
  39. name: 'tabNav',
  40. props: {
  41. dataConfig: {
  42. type: Object,
  43. default: () => {}
  44. },
  45. isFixed: {
  46. type: Boolean | String | Number,
  47. default: false
  48. },
  49. fromType: {
  50. type: Number,
  51. default: 0
  52. }
  53. },
  54. data() {
  55. return {
  56. tabTitle: [],
  57. tabLeft: 0,
  58. isWidth: 0, //每个导航栏占位
  59. tabClick: 0, //导航栏被点击
  60. isLeft: 0, //导航栏下划线位置
  61. fixedTop: 0,
  62. isTop: 0,
  63. navHeight: 45,
  64. tabList: 45,
  65. };
  66. },
  67. computed: {
  68. lineStyle() {
  69. let styleObject = {};
  70. if (this.dataConfig.toneConfig.tabVal) {
  71. switch (this.dataConfig.styleConfig.tabVal) {
  72. case 0:
  73. styleObject['background'] = `linear-gradient(90deg, ${this.dataConfig.decorateColor.color[0].item} 0%, ${this.dataConfig.decorateColor.color[1].item} 100%)`;
  74. break;
  75. case 1:
  76. styleObject['border-bottom-color'] = this.dataConfig.decorateColor2.color[0].item;
  77. break;
  78. }
  79. }
  80. return styleObject;
  81. },
  82. textStyle() {
  83. let styleObject = {};
  84. if (this.dataConfig.toneConfig.tabVal) {
  85. switch (this.dataConfig.styleConfig.tabVal) {
  86. case 0:
  87. styleObject['color'] = this.dataConfig.textColor.color[0].item;
  88. break;
  89. case 1:
  90. styleObject['color'] = this.dataConfig.textColor2.color[0].item;
  91. break;
  92. case 2:
  93. styleObject['background'] = `linear-gradient(90deg, ${this.dataConfig.decorateColor.color[0].item} 0%, ${this.dataConfig.decorateColor.color[1].item} 100%)`;
  94. styleObject['color'] = this.dataConfig.textColor3.color[0].item;
  95. break;
  96. }
  97. }
  98. return styleObject;
  99. },
  100. tabNavBgColor() {
  101. let borderRadius = `${this.dataConfig.fillet.val * 2}rpx`;
  102. if (this.dataConfig.fillet.type) {
  103. borderRadius =
  104. `${this.dataConfig.fillet.valList[0].val * 2}rpx ${this.dataConfig.fillet.valList[1].val * 2}rpx ${this.dataConfig.fillet.valList[2].val * 2}rpx ${this.dataConfig.fillet.valList[3].val * 2}rpx`;
  105. }
  106. return {
  107. 'border-radius': borderRadius,
  108. 'background': `linear-gradient(90deg, ${this.dataConfig.moduleColor.color[0].item} 0%, ${this.dataConfig.moduleColor.color[1].item} 100%)`,
  109. };
  110. },
  111. tabNavStyle() {
  112. return {
  113. 'padding': `${this.dataConfig.topConfig.val * 2}rpx ${this.dataConfig.prConfig.val * 2}rpx ${this.dataConfig.bottomConfig.val * 2}rpx`,
  114. 'margin-top': `${this.dataConfig.mbConfig.val * 2}rpx`,
  115. };
  116. },
  117. tabListConfig() {
  118. let tabList = this.dataConfig.tabListConfig.list;
  119. tabList.unshift({
  120. classPage: {
  121. id: 0
  122. },
  123. dataType: {
  124. tabVal: 0
  125. },
  126. microPage: {
  127. id: 0
  128. },
  129. text: {
  130. val: '首页'
  131. },
  132. });
  133. return tabList
  134. },
  135. },
  136. created() {
  137. let that = this;
  138. that.getAllCategory();
  139. // 获取设备宽度
  140. uni.getSystemInfo({
  141. success(e) {
  142. that.isWidth = e.windowWidth / 5
  143. }
  144. })
  145. },
  146. methods: {
  147. // 导航栏点击
  148. longClick(item, index) {
  149. if (this.tabTitle.length > 5) {
  150. this.tabLeft = (index - 2) * this.isWidth //设置下划线位置
  151. }
  152. this.tabClick = index //设置导航点击了哪一个
  153. this.isLeft = index * this.isWidth //设置下划线位置
  154. let data = {
  155. type: item.dataType.tabVal, //0 商品分类 1 微页面
  156. microPage: item.microPage.id,
  157. classPage: item.classPage.id,
  158. };
  159. this.$emit('bindSortId', data);
  160. },
  161. setCategory(data) {
  162. data.unshift({
  163. "id": -99,
  164. 'cate_name': '首页'
  165. })
  166. this.tabTitle = data;
  167. // #ifdef MP || APP-PLUS
  168. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  169. // #endif
  170. // #ifdef H5
  171. this.isTop = 0
  172. // #endif
  173. },
  174. getCategory() {
  175. getCategoryList().then(res => {
  176. uni.setStorageSync('category', JSON.stringify(res.data));
  177. this.setCategory(res.data);
  178. })
  179. },
  180. // 获取导航
  181. getAllCategory: function() {
  182. let that = this;
  183. let category = uni.getStorageSync('category');
  184. if (category) {
  185. getCategoryVersion().then(res => {
  186. let categoryVersion = uni.getStorageSync('categoryVersion');
  187. if (res.data.version === categoryVersion) {
  188. this.setCategory(JSON.parse(category));
  189. } else {
  190. uni.setStorageSync('categoryVersion', res.data.version);
  191. this.getCategory();
  192. }
  193. });
  194. } else {
  195. this.getCategory();
  196. }
  197. }
  198. }
  199. }
  200. </script>
  201. <style lang="scss">
  202. .tabNav1 {
  203. &.isFixed {
  204. z-index: 45;
  205. position: fixed;
  206. left: 0;
  207. width: 100%;
  208. /* #ifdef H5 */
  209. top: 0;
  210. /* #endif */
  211. }
  212. .list {
  213. flex-wrap: nowrap;
  214. height: 80rpx;
  215. margin-left: 24rpx;
  216. }
  217. .item {
  218. position: relative;
  219. z-index: 2;
  220. flex-shrink: 0;
  221. height: 80rpx;
  222. margin: 0 55rpx 0 0;
  223. font-size: 28rpx;
  224. line-height: 80rpx;
  225. color: #333333;
  226. &:last-child {
  227. margin: 0 24rpx 0 0;
  228. }
  229. }
  230. .on {
  231. font-weight: bold;
  232. font-size: 32rpx;
  233. .line {
  234. position: absolute;
  235. bottom: 22rpx;
  236. left: 50%;
  237. z-index: -1;
  238. width: 100%;
  239. height: 8rpx;
  240. border-radius: 4rpx;
  241. background: linear-gradient(90deg, var(--view-theme) 0%, var(--view-gradient) 100%);
  242. transform: translateX(-50%);
  243. }
  244. }
  245. }
  246. .tabNav2 {
  247. .list {
  248. flex-wrap: nowrap;
  249. height: 80rpx;
  250. margin-left: 24rpx;
  251. }
  252. .item {
  253. position: relative;
  254. z-index: 2;
  255. flex-shrink: 0;
  256. height: 80rpx;
  257. margin: 0 55rpx 0 0;
  258. font-size: 28rpx;
  259. line-height: 80rpx;
  260. color: #333333;
  261. &:last-child {
  262. margin: 0 24rpx 0 0;
  263. }
  264. }
  265. .on {
  266. font-weight: bold;
  267. font-size: 32rpx;
  268. color: var(--view-theme);
  269. .line {
  270. position: absolute;
  271. bottom: 8rpx;
  272. left: 50%;
  273. z-index: -1;
  274. width: 64rpx;
  275. height: 64rpx;
  276. border: 4rpx solid var(--view-theme);
  277. border-top-color: transparent;
  278. border-right-color: transparent;
  279. border-left-color: transparent;
  280. border-radius: 50%;
  281. transform: translateX(-50%);
  282. }
  283. }
  284. }
  285. .tabNav3 {
  286. .list {
  287. flex-wrap: nowrap;
  288. height: 80rpx;
  289. margin-left: 15rpx;
  290. }
  291. .item {
  292. position: relative;
  293. flex-shrink: 0;
  294. height: 80rpx;
  295. padding: 0 20rpx;
  296. margin: 0 15rpx 0 0;
  297. font-size: 28rpx;
  298. line-height: 80rpx;
  299. color: #333333;
  300. }
  301. .on {
  302. height: 48rpx;
  303. border-radius: 24rpx;
  304. background: linear-gradient(90deg, var(--view-theme) 0%, var(--view-gradient) 100%);
  305. text-align: center;
  306. font-size: 26rpx;
  307. line-height: 48rpx;
  308. color: #FFFFFF;
  309. }
  310. }
  311. </style>