u-tabs.vue 10 KB

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