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.
 
 
 

476 regels
13 KiB

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