tabNav.vue 7.2 KB

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