u-tabs-swiper.vue 14 KB

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