时空网前端
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.

397 lines
12 KiB

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