u-tabs-one.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view class="u-tabs" :style="{
  3. background: bgColor
  4. }">
  5. <!-- $u.getRect()对组件根节点无效,因为写了.in(this),故这里获取内层接点尺寸 -->
  6. <view>
  7. <scroll-view scroll-x class="u-scroll-view" :scroll-left="scrollLeft" scroll-with-animation>
  8. <view class="u-scroll-box" :id="id" :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: '#2979ff'
  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: 4
  102. },
  103. // 单个tab的左或有内边距(左右相同)
  104. gutter: {
  105. type: [String, Number],
  106. default: 15
  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. currentIndexBl: {
  160. type: Boolean,
  161. default: false
  162. },
  163. },
  164. data() {
  165. return {
  166. scrollLeft: 0, // 滚动scroll-view的左边滚动距离
  167. tabQueryInfo: [], // 存放对tab菜单查询后的节点信息
  168. componentWidth: 0, // 屏幕宽度,单位为px
  169. scrollBarLeft: 0, // 移动bar需要通过translateX()移动的距离
  170. parentLeft: 0, // 父元素(tabs组件)到屏幕左边的距离
  171. id: this.$u.guid(), // id值
  172. currentIndex: this.current,
  173. barFirstTimeMove: true, // 滑块第一次移动时(页面刚生成时),无需动画,否则给人怪异的感觉
  174. };
  175. },
  176. watch: {
  177. // 监听tab的变化,重新计算tab菜单的布局信息,因为实际使用中菜单可能是通过
  178. // 后台获取的(如新闻app顶部的菜单),获取返回需要一定时间,所以list变化时,重新获取布局信息
  179. list(n, o) {
  180. // list变动时,重制内部索引,否则可能导致超出数组边界的情况
  181. if(n.length !== o.length) this.currentIndex = 0;
  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 tabInfo = this.tabQueryInfo[this.currentIndex];
  202. var tabWidth =this.barWidth + 'rpx';
  203. if (tabInfo&&(this.currentIndex!=0||this.currentIndexBl)) {
  204. tabWidth = (tabInfo.width*2-30)+"rpx";
  205. }
  206. var scrollBarLeft=this.scrollBarLeft
  207. if(this.currentIndexBl){
  208. scrollBarLeft=scrollBarLeft-15
  209. }
  210. let style = {
  211. width: tabWidth,
  212. transform: `translate(${scrollBarLeft}px, -100%)`,
  213. // 滑块在页面渲染后第一次滑动时,无需动画效果
  214. 'transition-duration': `${this.barFirstTimeMove ? 0 : this.duration }s`,
  215. 'background-color': this.activeColor,
  216. height: this.barHeight + 'rpx',
  217. opacity: this.barFirstTimeMove ? 0 : 1,
  218. // 设置一个很大的值,它会自动取能用的最大值,不用高度的一半,是因为高度可能是单数,会有小数出现
  219. 'border-radius': `${this.barHeight / 2}px`
  220. };
  221. Object.assign(style, this.barStyle);
  222. return style;
  223. },
  224. // tab的样式
  225. tabItemStyle() {
  226. return (index) => {
  227. let style = {
  228. height: this.height + 'rpx',
  229. 'line-height': this.height + 'rpx',
  230. 'font-size': this.fontSize + 'rpx',
  231. 'transition-duration': `${this.duration}s`,
  232. padding: this.isScroll ? `0 ${this.gutter}rpx` : '',
  233. padding: `0 ${this.gutter}rpx`,
  234. //flex: this.isScroll ? 'auto' : '1',
  235. width: this.$u.addUnit(this.itemWidth)
  236. };
  237. // 字体加粗
  238. if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
  239. if (index == this.currentIndex) {
  240. style.color = this.activeColor;
  241. // 给选中的tab item添加外部自定义的样式
  242. style = Object.assign(style, this.activeItemStyle);
  243. } else {
  244. style.color = this.inactiveColor;
  245. }
  246. return style;
  247. }
  248. }
  249. },
  250. methods: {
  251. // 设置一个init方法,方便多处调用
  252. async init() {
  253. // 获取tabs组件的尺寸信息
  254. let tabRect = await this.$uGetRect('#' + this.id);
  255. // tabs组件距离屏幕左边的宽度
  256. this.parentLeft = tabRect.left;
  257. // tabs组件的宽度
  258. this.componentWidth = tabRect.width;
  259. this.getTabRect();
  260. },
  261. // 点击某一个tab菜单
  262. clickTab(index) {
  263. // 点击当前活动tab,不触发事件
  264. if(index == this.currentIndex) return ;
  265. // 发送事件给父组件
  266. this.$emit('change', index);
  267. },
  268. // 查询tab的布局信息
  269. getTabRect() {
  270. // 创建节点查询
  271. let query = uni.createSelectorQuery().in(this);
  272. // 历遍所有tab,这里是执行了查询,最终使用exec()会一次性返回查询的数组结果
  273. for (let i = 0; i < this.list.length; i++) {
  274. // 只要size和rect两个参数
  275. query.select(`#u-tab-item-${i}`).fields({
  276. size: true,
  277. rect: true
  278. });
  279. }
  280. // 执行查询,一次性获取多个结果
  281. query.exec(
  282. function(res) {
  283. this.tabQueryInfo = res;
  284. // 初始化滚动条和移动bar的位置
  285. this.scrollByIndex();
  286. }.bind(this)
  287. );
  288. },
  289. // 滚动scroll-view,让活动的tab处于屏幕的中间位置
  290. scrollByIndex() {
  291. // 当前活动tab的布局信息,有tab菜单的width和left(为元素左边界到父元素左边界的距离)等信息
  292. let tabInfo = this.tabQueryInfo[this.currentIndex];
  293. if (!tabInfo) return;
  294. // 活动tab的宽度
  295. let tabWidth = tabInfo.width;
  296. // 活动item的左边到tabs组件左边的距离,用item的left减去tabs的left
  297. let offsetLeft = tabInfo.left - this.parentLeft;
  298. // 将活动的tabs-item移动到屏幕正中间,实际上是对scroll-view的移动
  299. let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2;
  300. this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
  301. // 当前活动item的中点点到左边的距离减去滑块宽度的一半,即可得到滑块所需的移动距离
  302. let left = tabInfo.left + tabInfo.width / 2 - this.parentLeft;
  303. console.log(tabWidth,uni.upx2px(this.barWidth),tabInfo.left)
  304. // 计算当前活跃item到组件左边的距离
  305. //this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2;
  306. var barWidth =this.barWidth ;
  307. if (tabInfo&&(this.currentIndex!=0||this.currentIndexBl)) {
  308. barWidth = (tabInfo.width*2-30);
  309. }
  310. this.scrollBarLeft =(tabWidth/2- uni.upx2px(barWidth)/2)+ tabInfo.left ;
  311. // 第一次移动滑块的时候,barFirstTimeMove为true,放到延时中将其设置false
  312. // 延时是因为scrollBarLeft作用于computed计算时,需要一个过程需,否则导致出错
  313. if(this.barFirstTimeMove == true) {
  314. setTimeout(() => {
  315. this.barFirstTimeMove = false;
  316. }, 100)
  317. }
  318. }
  319. },
  320. mounted() {
  321. this.init();
  322. }
  323. };
  324. </script>
  325. <style lang="scss" scoped>
  326. @import "../../libs/css/style.components.scss";
  327. view,
  328. scroll-view {
  329. box-sizing: border-box;
  330. }
  331. /* #ifndef APP-NVUE */
  332. ::-webkit-scrollbar,
  333. ::-webkit-scrollbar,
  334. ::-webkit-scrollbar {
  335. display: none;
  336. width: 0 !important;
  337. height: 0 !important;
  338. -webkit-appearance: none;
  339. background: transparent;
  340. }
  341. /* #endif */
  342. .u-scroll-box {
  343. position: relative;
  344. /* #ifdef MP-TOUTIAO */
  345. white-space: nowrap;
  346. /* #endif */
  347. }
  348. /* #ifdef H5 */
  349. // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
  350. scroll-view ::v-deep ::-webkit-scrollbar {
  351. display: none;
  352. width: 0 !important;
  353. height: 0 !important;
  354. -webkit-appearance: none;
  355. background: transparent;
  356. }
  357. /* #endif */
  358. .u-scroll-view {
  359. width: 100%;
  360. white-space: nowrap;
  361. position: relative;
  362. }
  363. .u-tab-item {
  364. position: relative;
  365. /* #ifndef APP-NVUE */
  366. display: inline-block;
  367. /* #endif */
  368. text-align: center;
  369. transition-property: background-color, color;
  370. }
  371. .u-tab-bar {
  372. position: absolute;
  373. bottom: 0;
  374. }
  375. .u-tabs-scorll-flex {
  376. @include vue-flex;
  377. //justify-content: space-between;
  378. }
  379. </style>