u-tabs-swiper.vue 14 KB

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