金诚优选前端代码
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.

381 lines
12 KiB

  1. <template>
  2. <view class="u-tabs" :style="{
  3. background: bgColor
  4. }">
  5. <!-- $u.getRect()对组件根节点无效因为写了.in(this)故这里获取内层接点尺寸 -->
  6. <view :id="id">
  7. <scroll-view scroll-x class="u-scroll-view" :scroll-left="scrollLeft" scroll-with-animation>
  8. <view class="u-scroll-box" :class="{'u-tabs-scorll-flex': !isScroll}">
  9. <view class="u-tab-item u-line-1" :id="'u-tab-item-' + index" v-for="(item, index) in list" :key="index" @tap="clickTab(index)"
  10. :style="[tabItemStyle(index)]">
  11. <u-badge :count="item[count] || item['count'] || 0" :offset="offset" size="mini"></u-badge>
  12. {{ item[name] || item['name']}}
  13. </view>
  14. <view v-if="showBar" class="u-tab-bar" :style="[tabBarStyle]"></view>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * tabs 标签
  23. * @description 该组件是一个tabs标签组件在标签多的时候可以配置为左右滑动标签少的时候可以禁止滑动 该组件的一个特点是配置为滚动模式时激活的tab会自动移动到组件的中间位置
  24. * @tutorial https://www.uviewui.com/components/tabs.html
  25. * @property {Boolean} is-scroll tabs是否可以左右拖动默认true
  26. * @property {Array} list 标签数组元素为对象[{name: '推荐'}]
  27. * @property {String Number} current 指定哪个tab为激活状态默认0
  28. * @property {String Number} height 导航栏的高度单位rpx默认80
  29. * @property {String Number} font-size tab文字大小单位rpx默认30
  30. * @property {String Number} duration 滑块移动一次所需的时间单位秒默认0.5
  31. * @property {String} active-color 滑块和激活tab文字的颜色默认#2979ff
  32. * @property {String} inactive-color tabs文字颜色默认#303133
  33. * @property {String Number} bar-width 滑块宽度单位rpx默认40
  34. * @property {Object} active-item-style 活动tabs item的样式对象形式
  35. * @property {Object} bar-style 底部滑块的样式对象形式
  36. * @property {Boolean} show-bar 是否显示底部的滑块默认true
  37. * @property {String Number} bar-height 滑块高度单位rpx默认6
  38. * @property {String Number} item-width 标签的宽度默认auto
  39. * @property {String Number} gutter 单个tab标签的左右内边距之和单位rpx默认40
  40. * @property {String} bg-color tabs导航栏的背景颜色默认#ffffff
  41. * @property {String} name 组件内部读取的list参数中的属性名tab名称见官网说明默认name
  42. * @property {String} count 组件内部读取的list参数中的属性名badge徽标数同name属性的使用见官网说明默认count
  43. * @property {Array} offset 设置badge徽标数的位置偏移格式为 [x, y]也即设置的为top和right的值单位rpx默认[5, 20]
  44. * @property {Boolean} bold 激活选项的字体是否加粗默认true
  45. * @event {Function} change 点击标签时触发
  46. * @example <u-tabs ref="tabs" :list="list" :is-scroll="false"></u-tabs>
  47. */
  48. export default {
  49. name: "u-tabs",
  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. // 导航栏的高度和行高
  69. height: {
  70. type: [String, Number],
  71. default: 80
  72. },
  73. // 字体大小
  74. fontSize: {
  75. type: [String, Number],
  76. default: 30
  77. },
  78. // 过渡动画时长, 单位ms
  79. duration: {
  80. type: [String, Number],
  81. default: 0.5
  82. },
  83. // 选中项的主题颜色
  84. activeColor: {
  85. type: String,
  86. default: '#1998FE'
  87. },
  88. // 未选中项的颜色
  89. inactiveColor: {
  90. type: String,
  91. default: '#303133'
  92. },
  93. // 菜单底部移动的bar的宽度,单位rpx
  94. barWidth: {
  95. type: [String, Number],
  96. default: 40
  97. },
  98. // 移动bar的高度
  99. barHeight: {
  100. type: [String, Number],
  101. default: 6
  102. },
  103. // 单个tab的左或有内边距(左右相同)
  104. gutter: {
  105. type: [String, Number],
  106. default: 30
  107. },
  108. // 导航栏的背景颜色
  109. bgColor: {
  110. type: String,
  111. default: '#ffffff'
  112. },
  113. // 读取传入的数组对象的属性(tab名称)
  114. name: {
  115. type: String,
  116. default: 'name'
  117. },
  118. // 读取传入的数组对象的属性(徽标数)
  119. count: {
  120. type: String,
  121. default: 'count'
  122. },
  123. // 徽标数位置偏移
  124. offset: {
  125. type: Array,
  126. default: () => {
  127. return [5, 20]
  128. }
  129. },
  130. // 活动tab字体是否加粗
  131. bold: {
  132. type: Boolean,
  133. default: true
  134. },
  135. // 当前活动tab item的样式
  136. activeItemStyle: {
  137. type: Object,
  138. default() {
  139. return {}
  140. }
  141. },
  142. // 是否显示底部的滑块
  143. showBar: {
  144. type: Boolean,
  145. default: true
  146. },
  147. // 底部滑块的自定义样式
  148. barStyle: {
  149. type: Object,
  150. default() {
  151. return {}
  152. }
  153. },
  154. // 标签的宽度
  155. itemWidth: {
  156. type: [Number, String],
  157. default: 'auto'
  158. }
  159. },
  160. data() {
  161. return {
  162. scrollLeft: 0, // 滚动scroll-view的左边滚动距离
  163. tabQueryInfo: [], // 存放对tab菜单查询后的节点信息
  164. componentWidth: 0, // 屏幕宽度,单位为px
  165. scrollBarLeft: 0, // 移动bar需要通过translateX()移动的距离
  166. parentLeft: 0, // 父元素(tabs组件)到屏幕左边的距离
  167. id: this.$u.guid(), // id值
  168. currentIndex: this.current,
  169. barFirstTimeMove: true, // 滑块第一次移动时(页面刚生成时),无需动画,否则给人怪异的感觉
  170. };
  171. },
  172. watch: {
  173. // 监听tab的变化,重新计算tab菜单的布局信息,因为实际使用中菜单可能是通过
  174. // 后台获取的(如新闻app顶部的菜单),获取返回需要一定时间,所以list变化时,重新获取布局信息
  175. list(n, o) {
  176. // list变动时,重制内部索引,否则可能导致超出数组边界的情况
  177. if(n.length !== o.length) {
  178. this.currentIndex = 0;
  179. }else{
  180. return;
  181. }
  182. // 用$nextTick等待视图更新完毕后再计算tab的局部信息,否则可能因为tab还没生成就获取,就会有问题
  183. this.$nextTick(() => {
  184. this.init();
  185. });
  186. },
  187. current: {
  188. immediate: true,
  189. handler(nVal, oVal) {
  190. // 视图更新后再执行移动操作
  191. this.$nextTick(() => {
  192. this.currentIndex = nVal;
  193. this.scrollByIndex();
  194. });
  195. }
  196. },
  197. },
  198. computed: {
  199. // 移动bar的样式
  200. tabBarStyle() {
  201. let scrollBarLeft = this.scrollBarLeft - 9;
  202. let style = {
  203. width: this.barWidth + 'rpx',
  204. transform: `translate(${scrollBarLeft}px, -100%)`,
  205. // 滑块在页面渲染后第一次滑动时,无需动画效果
  206. 'transition-duration': `${this.barFirstTimeMove ? 0 : this.duration }s`,
  207. 'background-color': this.activeColor,
  208. height: this.barHeight + 'rpx',
  209. opacity: this.barFirstTimeMove ? 0 : 1,
  210. // 设置一个很大的值,它会自动取能用的最大值,不用高度的一半,是因为高度可能是单数,会有小数出现
  211. 'border-radius': `${this.barHeight / 2}px`
  212. };
  213. Object.assign(style, this.barStyle);
  214. return style;
  215. },
  216. // tab的样式
  217. tabItemStyle() {
  218. return (index) => {
  219. let style = {
  220. height: this.height + 'rpx',
  221. 'line-height': this.height + 'rpx',
  222. 'font-size': this.fontSize + 'rpx',
  223. 'transition-duration': `${this.duration}s`,
  224. padding: this.isScroll ? `0 ${this.gutter}rpx` : '',
  225. flex: this.isScroll ? 'auto' : '1',
  226. width: this.$u.addUnit(this.itemWidth)
  227. };
  228. // 字体加粗
  229. if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
  230. if (index == this.currentIndex) {
  231. style.color = this.activeColor;
  232. style.fontSize = '36rpx';
  233. // 给选中的tab item添加外部自定义的样式
  234. style = Object.assign(style, this.activeItemStyle);
  235. } else {
  236. style.color = this.inactiveColor;
  237. }
  238. return style;
  239. }
  240. }
  241. },
  242. methods: {
  243. // 设置一个init方法,方便多处调用
  244. async init() {
  245. // 获取tabs组件的尺寸信息
  246. let tabRect = await this.$uGetRect('#' + this.id);
  247. // tabs组件距离屏幕左边的宽度
  248. this.parentLeft = tabRect.left;
  249. // tabs组件的宽度
  250. this.componentWidth = tabRect.width;
  251. this.getTabRect();
  252. },
  253. // 点击某一个tab菜单
  254. clickTab(index) {
  255. // 点击当前活动tab,不触发事件
  256. if(index == this.currentIndex) return ;
  257. // 发送事件给父组件
  258. this.$emit('change', index);
  259. },
  260. // 查询tab的布局信息
  261. getTabRect() {
  262. // 创建节点查询
  263. let query = uni.createSelectorQuery().in(this);
  264. // 历遍所有tab,这里是执行了查询,最终使用exec()会一次性返回查询的数组结果
  265. for (let i = 0; i < this.list.length; i++) {
  266. // 只要size和rect两个参数
  267. query.select(`#u-tab-item-${i}`).fields({
  268. size: true,
  269. rect: true
  270. });
  271. }
  272. // 执行查询,一次性获取多个结果
  273. query.exec(
  274. function(res) {
  275. this.tabQueryInfo = res;
  276. // 初始化滚动条和移动bar的位置
  277. this.scrollByIndex();
  278. }.bind(this)
  279. );
  280. },
  281. // 滚动scroll-view,让活动的tab处于屏幕的中间位置
  282. scrollByIndex() {
  283. // 当前活动tab的布局信息,有tab菜单的width和left(为元素左边界到父元素左边界的距离)等信息
  284. let tabInfo = this.tabQueryInfo[this.currentIndex];
  285. if (!tabInfo) return;
  286. // 活动tab的宽度
  287. let tabWidth = tabInfo.width;
  288. // 活动item的左边到tabs组件左边的距离,用item的left减去tabs的left
  289. let offsetLeft = tabInfo.left - this.parentLeft;
  290. // 将活动的tabs-item移动到屏幕正中间,实际上是对scroll-view的移动
  291. let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2;
  292. this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
  293. // 当前活动item的中点点到左边的距离减去滑块宽度的一半,即可得到滑块所需的移动距离
  294. let left = tabInfo.left + tabInfo.width / 2 - this.parentLeft;
  295. // 计算当前活跃item到组件左边的距离
  296. this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2;
  297. // 第一次移动滑块的时候,barFirstTimeMove为true,放到延时中将其设置false
  298. // 延时是因为scrollBarLeft作用于computed计算时,需要一个过程需,否则导致出错
  299. if(this.barFirstTimeMove == true) {
  300. setTimeout(() => {
  301. this.barFirstTimeMove = false;
  302. }, 100)
  303. }
  304. }
  305. },
  306. mounted() {
  307. this.init();
  308. }
  309. };
  310. </script>
  311. <style lang="scss" scoped>
  312. @import "../../libs/css/style.components.scss";
  313. view,
  314. scroll-view {
  315. box-sizing: border-box;
  316. }
  317. /* #ifndef APP-NVUE */
  318. ::-webkit-scrollbar,
  319. ::-webkit-scrollbar,
  320. ::-webkit-scrollbar {
  321. display: none;
  322. width: 0 !important;
  323. height: 0 !important;
  324. -webkit-appearance: none;
  325. background: transparent;
  326. }
  327. /* #endif */
  328. .u-scroll-box {
  329. position: relative;
  330. /* #ifdef MP-TOUTIAO */
  331. white-space: nowrap;
  332. /* #endif */
  333. }
  334. /* #ifdef H5 */
  335. // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
  336. scroll-view ::v-deep ::-webkit-scrollbar {
  337. display: none;
  338. width: 0 !important;
  339. height: 0 !important;
  340. -webkit-appearance: none;
  341. background: transparent;
  342. }
  343. /* #endif */
  344. .u-scroll-view {
  345. width: 100%;
  346. white-space: nowrap;
  347. position: relative;
  348. }
  349. .u-tab-item {
  350. position: relative;
  351. /* #ifndef APP-NVUE */
  352. display: inline-block;
  353. /* #endif */
  354. text-align: center;
  355. transition-property: background-color, color;
  356. z-index: 2;
  357. }
  358. .u-tab-bar {
  359. position: absolute;
  360. bottom: 10px;
  361. width: 68rpx !important;
  362. height: 10rpx !important;
  363. background: #CFE7FD !important;
  364. border-radius: 5rpx !important;
  365. z-index: 1;
  366. }
  367. .u-tabs-scorll-flex {
  368. @include vue-flex;
  369. justify-content: space-between;
  370. }
  371. </style>