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

393 lines
12 KiB

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) this.currentIndex = 0;
  181. // 用$nextTick等待视图更新完毕后再计算tab的局部信息,否则可能因为tab还没生成就获取,就会有问题
  182. this.$nextTick(() => {
  183. this.init();
  184. });
  185. },
  186. current: {
  187. immediate: true,
  188. handler(nVal, oVal) {
  189. // 视图更新后再执行移动操作
  190. this.$nextTick(() => {
  191. this.currentIndex = nVal;
  192. this.scrollByIndex();
  193. });
  194. }
  195. },
  196. },
  197. computed: {
  198. // 移动bar的样式
  199. tabBarStyle() {
  200. let style = {
  201. width: this.barWidth + 'rpx',
  202. transform: `translate(${this.scrollBarLeft}px, -100%)`,
  203. // 滑块在页面渲染后第一次滑动时,无需动画效果
  204. 'transition-duration': `${this.barFirstTimeMove ? 0 : this.duration }s`,
  205. 'background-color': this.activeColor,
  206. height: this.barHeight + 'rpx',
  207. // 设置一个很大的值,它会自动取能用的最大值,不用高度的一半,是因为高度可能是单数,会有小数出现
  208. 'border-radius': `${this.barHeight / 2}px`
  209. };
  210. Object.assign(style, this.barStyle);
  211. return style;
  212. },
  213. // tab的样式
  214. tabItemStyle() {
  215. return (index) => {
  216. let style = {
  217. height: this.height + 'rpx',
  218. 'line-height': this.height + 'rpx',
  219. 'font-size': this.fontSize + 'rpx',
  220. 'transition-duration': `${this.duration}s`,
  221. padding: this.isScroll ? `0 ${this.gutter}rpx` : '',
  222. flex: this.isScroll ? 'auto' : '1',
  223. width: this.$u.addUnit(this.itemWidth)
  224. };
  225. // 字体加粗
  226. if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
  227. if (index == this.currentIndex) {
  228. style.color = this.activeColor;
  229. // 给选中的tab item添加外部自定义的样式
  230. style = Object.assign(style, this.activeItemStyle);
  231. } else {
  232. style.color = this.inactiveColor;
  233. }
  234. return style;
  235. }
  236. }
  237. },
  238. methods: {
  239. // 设置一个init方法,方便多处调用
  240. async init() {
  241. // 获取tabs组件的尺寸信息
  242. let tabRect = await this.$uGetRect('#' + this.id);
  243. // tabs组件距离屏幕左边的宽度
  244. this.parentLeft = tabRect.left;
  245. // tabs组件的宽度
  246. this.componentWidth = tabRect.width;
  247. this.getTabRect();
  248. },
  249. // 点击某一个tab菜单
  250. clickTab(index) {
  251. this.currentIndex = index;
  252. this.Checked = 'Checked';
  253. // // 点击当前活动tab,不触发事件
  254. // if(index == this.currentIndex){
  255. // this.Checked = 'Checked';
  256. // return ;
  257. // }else{
  258. // this.Checked = '';
  259. // };
  260. // 发送事件给父组件
  261. this.$emit('change', index);
  262. },
  263. // 查询tab的布局信息
  264. getTabRect() {
  265. // 创建节点查询
  266. let query = uni.createSelectorQuery().in(this);
  267. // 历遍所有tab,这里是执行了查询,最终使用exec()会一次性返回查询的数组结果
  268. for (let i = 0; i < this.list.length; i++) {
  269. // 只要size和rect两个参数
  270. query.select(`#u-tab-item-${i}`).fields({
  271. size: true,
  272. rect: true
  273. });
  274. }
  275. // 执行查询,一次性获取多个结果
  276. query.exec(
  277. function(res) {
  278. this.tabQueryInfo = res;
  279. // 初始化滚动条和移动bar的位置
  280. this.scrollByIndex();
  281. }.bind(this)
  282. );
  283. },
  284. // 滚动scroll-view,让活动的tab处于屏幕的中间位置
  285. scrollByIndex() {
  286. // 当前活动tab的布局信息,有tab菜单的width和left(为元素左边界到父元素左边界的距离)等信息
  287. let tabInfo = this.tabQueryInfo[this.currentIndex];
  288. if (!tabInfo) return;
  289. // 活动tab的宽度
  290. let tabWidth = tabInfo.width;
  291. // 活动item的左边到tabs组件左边的距离,用item的left减去tabs的left
  292. let offsetLeft = tabInfo.left - this.parentLeft;
  293. // 将活动的tabs-item移动到屏幕正中间,实际上是对scroll-view的移动
  294. let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2;
  295. this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
  296. // 当前活动item的中点点到左边的距离减去滑块宽度的一半,即可得到滑块所需的移动距离
  297. let left = tabInfo.left + tabInfo.width / 2 - this.parentLeft;
  298. // 计算当前活跃item到组件左边的距离
  299. this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2;
  300. // 第一次移动滑块的时候,barFirstTimeMove为true,放到延时中将其设置false
  301. // 延时是因为scrollBarLeft作用于computed计算时,需要一个过程需,否则导致出错
  302. if(this.barFirstTimeMove == true) {
  303. setTimeout(() => {
  304. this.barFirstTimeMove = false;
  305. }, 100)
  306. }
  307. }
  308. },
  309. mounted() {
  310. this.init();
  311. }
  312. };
  313. </script>
  314. <style lang="scss" scoped>
  315. @import "../../libs/css/style.components.scss";
  316. view,
  317. scroll-view {
  318. box-sizing: border-box;
  319. }
  320. /* #ifndef APP-NVUE */
  321. ::-webkit-scrollbar,
  322. ::-webkit-scrollbar,
  323. ::-webkit-scrollbar {
  324. display: none;
  325. width: 0 !important;
  326. height: 0 !important;
  327. -webkit-appearance: none;
  328. background: transparent;
  329. }
  330. /* #endif */
  331. .u-scroll-box {
  332. position: relative;
  333. /* #ifdef MP-TOUTIAO */
  334. white-space: nowrap;
  335. /* #endif */
  336. }
  337. /* #ifdef H5 */
  338. // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
  339. scroll-view ::v-deep ::-webkit-scrollbar {
  340. display: none;
  341. width: 0 !important;
  342. height: 0 !important;
  343. -webkit-appearance: none;
  344. background: transparent;
  345. }
  346. /* #endif */
  347. .u-scroll-view {
  348. width: 100%;
  349. white-space: nowrap;
  350. position: relative;
  351. }
  352. .u-tab-item {
  353. position: relative;
  354. /* #ifndef APP-NVUE */
  355. display: inline-block;
  356. /* #endif */
  357. text-align: center;
  358. transition-property: background-color, color;
  359. }
  360. .u-tab-bar {
  361. position: absolute;
  362. bottom: 0;
  363. }
  364. .u-tabs-scorll-flex {
  365. @include vue-flex;
  366. justify-content: space-between;
  367. }
  368. .anniu{
  369. // padding:20rpx 58rpx;
  370. background-color: #F5F5F5;
  371. color: #555555;
  372. display: block;
  373. border-radius: 10rpx;
  374. margin-left:10rpx ;
  375. width: 172rpx;
  376. height: 80rpx;
  377. line-height: 80rpx;
  378. }
  379. .Checked{
  380. background-color: #FE9903;
  381. color: #FFFFFF;
  382. }
  383. </style>