tabNav.vue 9.4 KB

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