You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

488 lines
14 KiB

3 years ago
  1. <template>
  2. <view class="u-tabs" :style="{
  3. zIndex: zIndex,
  4. background: bgColor
  5. }">
  6. <scroll-view scroll-x class="u-scroll-view" :scroll-left="scrollLeft" scroll-with-animation :style="{ zIndex: zIndex + 1 }">
  7. <view class="u-tabs-scroll-box" :class="{'u-tabs-scorll-flex': !isScroll}">
  8. <view class="u-tabs-item" :style="[tabItemStyle(index)]"
  9. v-for="(item, index) in getTabs" :key="index" :class="[preId + index]" @tap="emit(index)">
  10. <u-badge :count="item[count] || item['count'] || 0" :offset="offset" size="mini"></u-badge>
  11. {{ item[name] || item['name']}}
  12. </view>
  13. <view v-if="showBar" class="u-scroll-bar" :style="[tabBarStyle]"></view>
  14. </view>
  15. </scroll-view>
  16. </view>
  17. </template>
  18. <script>
  19. import colorGradient from '../../libs/function/colorGradient';
  20. let color = colorGradient;
  21. const { windowWidth } = uni.getSystemInfoSync();
  22. const preId = 'UEl_';
  23. /**
  24. * tabsSwiper 全屏选项卡
  25. * @description 该组件内部实现主要依托于uniapp的scroll-view和swiper组件主要特色是切换过程中tabsSwiper文字的颜色可以渐变底部滑块可以 跟随式滑动活动tab滚动居中等应用场景可以用于需要左右切换页面比如商城的订单中心(待收货-待付款-待评价-已退货)等应用场景
  26. * @tutorial https://www.uviewui.com/components/tabsSwiper.html
  27. * @property {Boolean} is-scroll tabs是否可以左右拖动默认true
  28. * @property {Array} list 标签数组元素为对象[{name: '推荐'}]
  29. * @property {String Number} current 指定哪个tab为激活状态默认0
  30. * @property {String Number} height 导航栏的高度单位rpx默认80
  31. * @property {String Number} font-size tab文字大小单位rpx默认30
  32. * @property {String Number} swiper-width tabs组件外部swiper的宽度默认为屏幕宽度单位rpx默认750
  33. * @property {String} active-color 滑块和激活tab文字的颜色默认#2979ff
  34. * @property {String} inactive-color tabs文字颜色默认#303133
  35. * @property {String Number} bar-width 滑块宽度单位rpx默认40
  36. * @property {String Number} bar-height 滑块高度单位rpx默认6
  37. * @property {Object} bar-style 底部滑块的样式对象形式
  38. * @property {Object} active-item-style 活动tabs item的样式对象形式
  39. * @property {Boolean} show-bar 是否显示底部的滑块默认true
  40. * @property {String Number} gutter 单个tab标签的左右内边距之和单位rpx默认40
  41. * @property {String} bg-color tabs导航栏的背景颜色默认#ffffff
  42. * @property {String} name 组件内部读取的list参数中的属性名见官网说明默认name
  43. * @property {String} count 组件内部读取的list参数中的属性名badge徽标数同name属性的使用见官网说明默认count
  44. * @property {Array} offset 设置badge徽标数的位置偏移格式为 [x, y]也即设置的为top和right的值单位rpx默认[5, 20]
  45. * @property {Boolean} bold 激活选项的字体是否加粗默认true
  46. * @event {Function} change 点击标签时触发
  47. * @example <u-tabs-swiper ref="tabs" :list="list" :is-scroll="false"></u-tabs-swiper>
  48. */
  49. export default {
  50. name: "u-tabs-swiper",
  51. props: {
  52. // 导航菜单是否需要滚动,如只有2或者3个的时候,就不需要滚动了,此时使用flex平分tab的宽度
  53. isScroll: {
  54. type: Boolean,
  55. default: true
  56. },
  57. //需循环的标签列表
  58. list: {
  59. type: Array,
  60. default () {
  61. return [];
  62. }
  63. },
  64. // 当前活动tab的索引
  65. current: {
  66. type: [Number, String],
  67. default: 0
  68. },
  69. // 导航栏的高度和行高,单位rpx
  70. height: {
  71. type: [Number, String],
  72. default: 80
  73. },
  74. // 字体大小,单位rpx
  75. fontSize: {
  76. type: [Number, String],
  77. default: 30
  78. },
  79. // 过渡动画时长, 单位s
  80. // duration: {
  81. // type: [Number, String],
  82. // default: 0.5
  83. // },
  84. swiperWidth: {
  85. //line3生效, 外部swiper的宽度, 单位rpx
  86. type: [String, Number],
  87. default: 750
  88. },
  89. // 选中项的主题颜色
  90. activeColor: {
  91. type: String,
  92. default: '#2979ff'
  93. },
  94. // 未选中项的颜色
  95. inactiveColor: {
  96. type: String,
  97. default: '#303133'
  98. },
  99. // 菜单底部移动的bar的宽度,单位rpx
  100. barWidth: {
  101. type: [Number, String],
  102. default: 40
  103. },
  104. // 移动bar的高度
  105. barHeight: {
  106. type: [Number, String],
  107. default: 6
  108. },
  109. // 单个tab的左或右内边距(各占一半),单位rpx
  110. gutter: {
  111. type: [Number, String],
  112. default: 40
  113. },
  114. // 如果是绝对定位,添加z-index值
  115. zIndex: {
  116. type: [Number, String],
  117. default: 1
  118. },
  119. // 导航栏的背景颜色
  120. bgColor: {
  121. type: String,
  122. default: '#ffffff'
  123. },
  124. //滚动至中心目标类型
  125. autoCenterMode: {
  126. type: String,
  127. default: 'window'
  128. },
  129. // 读取传入的数组对象的属性(tab名称)
  130. name: {
  131. type: String,
  132. default: 'name'
  133. },
  134. // 读取传入的数组对象的属性(徽标数)
  135. count: {
  136. type: String,
  137. default: 'count'
  138. },
  139. // 徽标数位置偏移
  140. offset: {
  141. type: Array,
  142. default: () => {
  143. return [5, 20]
  144. }
  145. },
  146. // 活动tab字体是否加粗
  147. bold: {
  148. type: Boolean,
  149. default: true
  150. },
  151. // 当前活动tab item的样式
  152. activeItemStyle: {
  153. type: Object,
  154. default() {
  155. return {}
  156. }
  157. },
  158. // 是否显示底部的滑块
  159. showBar: {
  160. type: Boolean,
  161. default: true
  162. },
  163. // 底部滑块的自定义样式
  164. barStyle: {
  165. type: Object,
  166. default() {
  167. return {}
  168. }
  169. }
  170. },
  171. data() {
  172. return {
  173. scrollLeft: 0, // 滚动scroll-view的左边滚动距离
  174. tabQueryInfo: [], // 存放对tab菜单查询后的节点信息
  175. windowWidth: 0, // 屏幕宽度,单位为px
  176. //scrollBarLeft: 0, // 移动bar需要通过translateX()移动的距离
  177. animationFinishCurrent: this.current,
  178. componentsWidth: 0,
  179. line3AddDx: 0,
  180. line3Dx: 0,
  181. preId,
  182. sW: 0,
  183. tabsInfo: [],
  184. colorGradientArr: [],
  185. colorStep: 100 // 两个颜色之间的渐变等分
  186. };
  187. },
  188. computed: {
  189. // 获取当前活跃的current值
  190. getCurrent() {
  191. const current = Number(this.current);
  192. // 判断是否超出边界
  193. if (current > this.getTabs.length - 1) {
  194. return this.getTabs.length - 1;
  195. }
  196. if (current < 0) return 0;
  197. return current;
  198. },
  199. getTabs() {
  200. return this.list;
  201. },
  202. // 滑块需要移动的距离
  203. scrollBarLeft() {
  204. return Number(this.line3Dx) + Number(this.line3AddDx);
  205. },
  206. // 滑块的宽度转为px单位
  207. barWidthPx() {
  208. return uni.upx2px(this.barWidth);
  209. },
  210. // tab的样式
  211. tabItemStyle() {
  212. return (index) => {
  213. let style = {
  214. height: this.height + 'rpx',
  215. lineHeight: this.height + 'rpx',
  216. padding: `0 ${this.gutter / 2}rpx`,
  217. color: this.tabsInfo.length > 0 ? (this.tabsInfo[index] ? this.tabsInfo[index].color : this.activeColor) : this.inactiveColor,
  218. fontSize: this.fontSize + 'rpx',
  219. zIndex: this.zIndex + 2,
  220. fontWeight: (index == this.getCurrent && this.bold) ? 'bold' : 'normal'
  221. };
  222. if(index == this.getCurrent) {
  223. // 给选中的tab item添加外部自定义的样式
  224. style = Object.assign(style, this.activeItemStyle);
  225. }
  226. return style;
  227. }
  228. },
  229. // 底部滑块的样式
  230. tabBarStyle() {
  231. let style = {
  232. width: this.barWidthPx + 'px',
  233. height: this.barHeight + 'rpx',
  234. borderRadius: '100px',
  235. backgroundColor: this.activeColor,
  236. left: this.scrollBarLeft + 'px'
  237. };
  238. return Object.assign(style, this.barStyle);
  239. }
  240. },
  241. watch: {
  242. current(n, o) {
  243. this.change(n);
  244. this.setFinishCurrent(n);
  245. },
  246. list() {
  247. this.$nextTick(() => {
  248. this.init();
  249. })
  250. }
  251. },
  252. mounted() {
  253. this.init();
  254. },
  255. methods: {
  256. async init() {
  257. this.countPx();
  258. await this.getTabsInfo();
  259. this.countLine3Dx();
  260. this.getQuery(() => {
  261. this.setScrollViewToCenter();
  262. });
  263. // 颜色渐变过程数组
  264. this.colorGradientArr = color.colorGradient(this.inactiveColor, this.activeColor, this.colorStep);
  265. },
  266. // 获取各个tab的节点信息
  267. getTabsInfo() {
  268. return new Promise((resolve, reject) => {
  269. let view = uni.createSelectorQuery().in(this);
  270. for (let i = 0; i < this.list.length; i++) {
  271. view.select('.' + preId + i).boundingClientRect();
  272. }
  273. view.exec(res => {
  274. const arr = [];
  275. for (let i = 0; i < res.length; i++) {
  276. // 给每个tab添加其文字颜色属性
  277. res[i].color = this.inactiveColor;
  278. // 当前tab直接赋予activeColor
  279. if (i == this.getCurrent) res[i].color = this.activeColor;
  280. arr.push(res[i]);
  281. }
  282. this.tabsInfo = arr;
  283. resolve();
  284. });
  285. })
  286. },
  287. // 当swiper滑动结束,计算滑块最终要停留的位置
  288. countLine3Dx() {
  289. const tab = this.tabsInfo[this.animationFinishCurrent];
  290. // 让滑块中心点和当前tab中心重合
  291. if (tab) this.line3Dx = tab.left + tab.width / 2 - this.barWidthPx / 2 - this.tabsInfo[0].left;
  292. },
  293. countPx() {
  294. // swiper宽度由rpx转为px单位,因为dx等,都是px单位
  295. this.sW = uni.upx2px(Number(this.swiperWidth));
  296. },
  297. emit(index) {
  298. this.$emit('change', index);
  299. },
  300. change() {
  301. this.setScrollViewToCenter();
  302. },
  303. getQuery(cb) {
  304. try {
  305. let view = uni.createSelectorQuery().in(this).select('.u-tabs');
  306. view.fields({
  307. size: true
  308. },
  309. data => {
  310. if (data) {
  311. this.componentsWidth = data.width;
  312. if (cb && typeof cb === 'function') cb(data);
  313. } else {
  314. this.getQuery(cb);
  315. }
  316. }
  317. ).exec();
  318. } catch (e) {
  319. this.componentsWidth = windowWidth;
  320. }
  321. },
  322. // 把活动tab移动到屏幕中心点
  323. setScrollViewToCenter() {
  324. let tab;
  325. tab = this.tabsInfo[this.animationFinishCurrent];
  326. if (tab) {
  327. let tabCenter = tab.left + tab.width / 2;
  328. let fatherWidth;
  329. // 活动tab移动到中心时,以屏幕还是tab组件为宽度为基准
  330. if (this.autoCenterMode === 'window') {
  331. fatherWidth = windowWidth;
  332. } else {
  333. fatherWidth = this.componentsWidth;
  334. }
  335. this.scrollLeft = tabCenter - fatherWidth / 2;
  336. }
  337. },
  338. setDx(dx) {
  339. let nextTabIndex = dx > 0 ? this.animationFinishCurrent + 1 : this.animationFinishCurrent - 1;
  340. // 判断索引是否超出边界
  341. nextTabIndex = nextTabIndex <= 0 ? 0 : nextTabIndex;
  342. nextTabIndex = nextTabIndex >= this.list.length ? this.list.length - 1 : nextTabIndex;
  343. const tab = this.tabsInfo[nextTabIndex];
  344. // 当前tab中心点x轴坐标
  345. let nowTab = this.tabsInfo[this.animationFinishCurrent];
  346. let nowTabX = nowTab.left + nowTab.width / 2;
  347. // 下一个tab
  348. let nextTab = this.tabsInfo[nextTabIndex];
  349. let nextTabX = nextTab.left + nextTab.width / 2;
  350. // 两个tab之间的距离,因为下一个tab可能在当前tab的左边或者右边,取绝对值即可
  351. let distanceX = Math.abs(nextTabX - nowTabX);
  352. this.line3AddDx = (dx / this.sW) * distanceX;
  353. this.setTabColor(this.animationFinishCurrent, nextTabIndex, dx);
  354. },
  355. // 设置tab的颜色
  356. setTabColor(nowTabIndex, nextTabIndex, dx) {
  357. let colorIndex = Math.abs(Math.ceil((dx / this.sW) * 100));
  358. let colorLength = this.colorGradientArr.length;
  359. // 处理超出索引边界的情况
  360. colorIndex = colorIndex >= colorLength ? colorLength - 1 : colorIndex <= 0 ? 0 : colorIndex;
  361. // 设置下一个tab的颜色
  362. this.tabsInfo[nextTabIndex].color = this.colorGradientArr[colorIndex];
  363. // 设置当前tab的颜色
  364. this.tabsInfo[nowTabIndex].color = this.colorGradientArr[colorLength - 1 - colorIndex];
  365. },
  366. // swiper结束滑动
  367. setFinishCurrent(current) {
  368. // 如果滑动的索引不一致,修改tab颜色变化,因为可能会有直接点击tab的情况
  369. this.tabsInfo.map((val, index) => {
  370. if (current == index) val.color = this.activeColor;
  371. else val.color = this.inactiveColor;
  372. return val;
  373. });
  374. this.line3AddDx = 0;
  375. this.animationFinishCurrent = current;
  376. this.countLine3Dx();
  377. }
  378. }
  379. };
  380. </script>
  381. <style scoped lang="scss">
  382. @import "../../libs/css/style.components.scss";
  383. view,
  384. scroll-view {
  385. box-sizing: border-box;
  386. }
  387. .u-tabs {
  388. width: 100%;
  389. transition-property: background-color, color;
  390. }
  391. /* #ifndef APP-NVUE */
  392. ::-webkit-scrollbar,
  393. ::-webkit-scrollbar,
  394. ::-webkit-scrollbar {
  395. display: none;
  396. width: 0 !important;
  397. height: 0 !important;
  398. -webkit-appearance: none;
  399. background: transparent;
  400. }
  401. /* #endif */
  402. /* #ifdef H5 */
  403. // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
  404. scroll-view ::v-deep ::-webkit-scrollbar {
  405. display: none;
  406. width: 0 !important;
  407. height: 0 !important;
  408. -webkit-appearance: none;
  409. background: transparent;
  410. }
  411. /* #endif */
  412. .u-scroll-view {
  413. width: 100%;
  414. white-space: nowrap;
  415. position: relative;
  416. }
  417. .u-tabs-scroll-box {
  418. position: relative;
  419. }
  420. .u-tabs-scorll-flex {
  421. @include vue-flex;
  422. justify-content: space-between;
  423. }
  424. .u-tabs-scorll-flex .u-tabs-item {
  425. flex: 1;
  426. }
  427. .u-tabs-item {
  428. position: relative;
  429. display: inline-block;
  430. text-align: center;
  431. transition-property: background-color, color, font-weight;
  432. }
  433. .content {
  434. overflow: hidden;
  435. white-space: nowrap;
  436. text-overflow: ellipsis;
  437. }
  438. .boxStyle {
  439. pointer-events: none;
  440. position: absolute;
  441. transition-property: all;
  442. }
  443. .boxStyle2 {
  444. pointer-events: none;
  445. position: absolute;
  446. bottom: 0;
  447. transition-property: all;
  448. transform: translateY(-100%);
  449. }
  450. .itemBackgroundBox {
  451. pointer-events: none;
  452. position: absolute;
  453. top: 0;
  454. transition-property: left, background-color;
  455. @include vue-flex;
  456. flex-direction: row;
  457. justify-content: center;
  458. align-items: center;
  459. }
  460. .itemBackground {
  461. height: 100%;
  462. width: 100%;
  463. transition-property: all;
  464. }
  465. .u-scroll-bar {
  466. position: absolute;
  467. bottom: 4rpx;
  468. }
  469. </style>