<tabbar>

[[toc]]

<tabbar>为页面滑动切换组件(别名:标签页)

  • 常用于tab切换页面。
  • 支持url页面(tabPages模式)。
  • 支持以 <tabbar-page>子组件编写的页面。
  • 支持顶部、底部高自定义选项卡。

子组件

仅支持<tabbar-page>组件作为标签页内容,详细请看 这里

预览效果

&lt;tabbar&gt; - 图1

示例代码

  1. <template>
  2. <div class="app">
  3. <tabbar
  4. ref="reflectName"
  5. class="tabbar"
  6. tabType="bottom"
  7. @pageSelected="pageSelected"
  8. @tabReselect="tabReselect">
  9. <!--页签①-->
  10. <tabbar-page ref="name_1"
  11. @refreshListener="refreshListener"
  12. tabName="name_1"
  13. title="首页"
  14. selectedIcon="md-home">
  15. <navbar class="page-navbar">
  16. <navbar-item type="back"></navbar-item>
  17. <navbar-item type="title">
  18. <text class="page-navbar-title">首页</text>
  19. </navbar-item>
  20. <navbar-item type="right" @click="viewCode('component/tabbar')">
  21. <icon content="md-code-working" class="iconr"></icon>
  22. </navbar-item>
  23. </navbar>
  24. <div class="page-content">
  25. <image src="https://eeui.app/assets/images/cartoon/m2.png" class="page-content-image"></image>
  26. <text class="content-text">页签里面可以放任何子组件,感谢你对eeui的支持</text>
  27. </div>
  28. </tabbar-page>
  29. <!--页签②-->
  30. <tabbar-page ref="name_2" @refreshListener="refreshListener"
  31. tabName="name_2"
  32. title="好友"
  33. message="3"
  34. selectedIcon="https://eeui.app/assets/images/cartoon/m8.png"
  35. unSelectedIcon="https://eeui.app/assets/images/cartoon/m7.png">
  36. <navbar class="page-navbar">
  37. <navbar-item type="title">
  38. <text class="page-navbar-title">好友</text>
  39. </navbar-item>
  40. </navbar>
  41. <div class="page-content">
  42. <text class="content-text">page 2,tab显示器图标支持网络图片</text>
  43. </div>
  44. </tabbar-page>
  45. <!--页签③-->
  46. <tabbar-page ref="name_3"
  47. @refreshListener="refreshListener"
  48. tabName="name_3"
  49. title="圈子"
  50. message="99"
  51. selectedIcon="md-aperture">
  52. <navbar class="page-navbar">
  53. <navbar-item type="title">
  54. <text class="page-navbar-title">圈子</text>
  55. </navbar-item>
  56. </navbar>
  57. <div class="page-content">
  58. <text class="content-text">page 3</text>
  59. </div>
  60. </tabbar-page>
  61. <!--页签④-->
  62. <tabbar-page ref="name_4"
  63. @refreshListener="refreshListener"
  64. tabName="name_4"
  65. title="设置"
  66. dot="true"
  67. selectedIcon="md-cog">
  68. <navbar class="page-navbar">
  69. <navbar-item type="title">
  70. <text class="page-navbar-title">设置</text>
  71. </navbar-item>
  72. </navbar>
  73. <div class="page-content">
  74. <text class="content-text">page 4</text>
  75. </div>
  76. </tabbar-page>
  77. </tabbar>
  78. </div>
  79. </template>
  80. <style>
  81. .app {
  82. flex: 1
  83. }
  84. .iconr {
  85. width: 100px;
  86. height: 100px;
  87. color: #ffffff;
  88. }
  89. .tabbar {
  90. width: 750px;
  91. flex: 1;
  92. }
  93. .page-content {
  94. width: 750px;
  95. padding-top: 200px;
  96. align-items: center;
  97. }
  98. .content-text {
  99. font-size: 24px;
  100. }
  101. .page-navbar {
  102. width: 750px;
  103. height: 90px;
  104. }
  105. .page-navbar-title {
  106. color: #ffffff;
  107. font-size: 28px;
  108. }
  109. .page-content-image {
  110. width: 480px;
  111. height: 480px;
  112. margin-top: 30px;
  113. margin-bottom: 30px;
  114. }
  115. </style>
  116. <script>
  117. const eeui = app.requireModule('eeui');
  118. export default {
  119. methods: {
  120. viewCode(str) {
  121. this.openViewCode(str);
  122. },
  123. pageSelected(params) {
  124. eeui.toast({
  125. message: "切换到第" + (params.position + 1) + "个标签页",
  126. gravity: "middle"
  127. });
  128. },
  129. tabReselect(params) {
  130. eeui.toast({
  131. message: "第" + (params.position + 1) + "个标签页被再次点击",
  132. gravity: "middle"
  133. });
  134. eeui.toast();
  135. },
  136. refreshListener(params) {
  137. setTimeout(() => {
  138. eeui.toast({
  139. message: "刷新成功:" + params.tabName,
  140. gravity: "middle"
  141. });
  142. this.$refs[params.tabName].refreshEnd();
  143. }, 1000);
  144. }
  145. }
  146. };
  147. </script>

配置参数

属性名 类型 描述 默认值
tabType String 设置tab位置,
顶部:top
顶部滑动:slidingTop
底部:bottom
bottom
tabHeight Number 设置tab高度 100
tabBackgroundColor String 设置tab的背景颜色 #3EB4FF,底部位置:#ffffff
tabWidth Number 设置tab固定大小 -
tabPageAnimated Boolean 开关页面切换动画 true
tabSlideSwitch Boolean 手势滑动切换页面 true
indicatorColor String 设置tab显示器颜色 #ffffff
indicatorHeight Number 设置tab显示器高度 4
indicatorWidth Number 设置tab显示器固定宽度 20
indicatorCornerRadius Number 设置tab显示器圆角弧度 2
underlineGravity Number 设置tab下划线位置,
上方:1
下方:0
0
underlineColor String 设置tab下划线颜色 #ffffff
underlineHeight Number 设置tab下划线高度 0
dividerColor String 设置tab分割线颜色 #ffffff
dividerWidth Number 设置tab分割线宽度 0
dividerPadding Number 设置tab分割线的上下内边距 12
textBold Number 设置tab字体加粗,
不加粗:0
选择加粗:1
全部加粗:2
0
textSize Number 设置tab字体大小 26
textSelectColor String 设置tab字体选中颜色 #ffffff,底部位置:#2C97DE
textUnselectColor String 设置tab字体未选中颜色 #eeeeee,底部位置:#333333
iconVisible Boolean 设置tab图标是否可见 false,底部位置:true
iconGravity Number 设置tab图标显示位置,
上方:1
下方:0
1
iconWidth Number 设置tab图标宽度 40
iconHeight Number 设置tab图标高度 40
iconMargin Number 设置tab图标与文字间距 10
preload Boolean 是否预加载所有子页面:
true: 预加载所有子页面;
false: 第一次切换到时加载子页面。
false

例如:

  1. <tabbar
  2. ref="reflectName"
  3. tabType="bottom"></tabbar>

配置参数 tabPages

说明:tabPages模式支持url定义页面;数据格式:对象数组。

属性名 类型 描述 默认值
tabName String tab页签名称 -
title String tab名称 New Page
url String 页面地址
①支持本地地址,如:xxx.js
②支持远程地址,如:http://abc.com/xxx.js
可使用 root:// 表示 src 目录,比如首页: root://pages/index.js
-
unSelectedIcon String tab未选图标 home
selectedIcon String tab已选图标 home
params Object tab页面传递参数,通过app.config.params获取 -
cache Number tab页面缓存时间,设置0不缓存(单位:毫秒) 0
message Number tab未读信息数 0
dot Boolean 是否显示tab未读红点 false
loading Boolean 是否显示等待效果:truefalse true
loadingBackground Boolean 是否显示等待效果过渡背景:truefalse false
statusBarColor String 状态栏颜色值,默认:隐藏
主要用于主页面沉浸式时设置每个页面不同的状态栏效果;
tabType:bottom推荐使用。
-

例如:

  1. <tabbar
  2. ref="reflectName"
  3. :tabPages="[ {
  4. title: '首页',
  5. url: 'http://dotwe.org/raw/dist/b5fd96d8d790f0100bdfc20b93eedf09.bundle.wx',
  6. }, {
  7. title: '好友',
  8. url: 'xxxx.js',
  9. message: 9
  10. } ]"></tabbar>

事件回调 callback

  1. /**
  2. * 组件加载完成
  3. */
  4. @ready = function() { ... }
  5. /**
  6. * 标签页切换时调用,滑动被停止之前一直调用
  7. * 返回参数:data = {position: 1, positionOffset: 1, positionOffsetPixels: 100}
  8. */
  9. @pageScrolled = function(data) { ... }
  10. /**
  11. * 标签页切换完成调用
  12. * 返回参数:data = {position: 1}
  13. */
  14. @pageSelected = function(data) { ... }
  15. /**
  16. * 标签页状态改变的时候调用
  17. * 返回参数:data = {state: 1}
  18. */
  19. @pageScrollStateChanged = function(data) { ... }
  20. /**
  21. * 标签被点击选择
  22. * 返回参数:data = {position: 1}
  23. */
  24. @tabSelect = function(data) { ... }
  25. /**
  26. * 标签被再次点击选择
  27. * 返回参数:data = {position: 1}
  28. */
  29. @tabReselect = function(data) { ... }
  30. /**
  31. * 标签页创建完毕
  32. * 返回参数:data = {tabName: 'tabName', url: '...'}
  33. */
  34. @viewCreated = function(data) { ... }
  35. /**
  36. * 标签页视图滚动事件(滚动完成后调用)
  37. * 返回参数:data = {tabName: 'tabName', x: 0, y: 100}
  38. */
  39. @scrolled = function(data) { ... }
  40. /**
  41. * 标签页视图滚动状态发生变化
  42. * 返回参数:data = {tabName: 'tabName', x: 0, y: 100, newState: 0}
  43. */
  44. @scrollStateChanged = function(data) { ... }

调用方法 methods

  1. /**
  2. * 根据页签名称获取页签位置
  3. * 参数一:页签名称
  4. * 返回参数:页签位置
  5. */
  6. this.$refs.reflectName.getTabPosition(tabName);
  7. /**
  8. * 根据页签位置获取页签名称
  9. * 参数一:页签位置
  10. * 返回参数:页签名称
  11. */
  12. this.$refs.reflectName.getTabName(2);
  13. /**
  14. * 显示tab未读信息数量
  15. * 参数一:页签名称
  16. * 参数二:未读数量
  17. */
  18. this.$refs.reflectName.showMsg(tabName, 5);
  19. /**
  20. * 显示tab未读红点
  21. * 参数一:页签名称
  22. */
  23. this.$refs.reflectName.showDot(tabName);
  24. /**
  25. * 隐藏tab未读信息数及未读红点
  26. * 参数一:页签名称
  27. */
  28. this.$refs.reflectName.hideMsg(tabName);
  29. /**
  30. * 删除指定tab页
  31. * 参数一:页签名称
  32. */
  33. this.$refs.reflectName.removePageAt(tabName);
  34. /**
  35. * 切换tab页
  36. * 参数一:页签名称
  37. */
  38. this.$refs.reflectName.setCurrentItem(tabName);
  39. /**
  40. * tab页跳转url(仅:tabPages模式支持)
  41. * 参数一:页签名称
  42. * 参数二:跳转的App Js文件地址
  43. */
  44. this.$refs.reflectName.goUrl(tabName, 'http://....');
  45. /**
  46. * tab页刷新(仅:tabPages模式支持)
  47. * 参数一:页签名称
  48. */
  49. this.$refs.reflectName.reload(tabName);
  50. /**
  51. * 设置tab位置
  52. * 参数一:top|bottom
  53. */
  54. this.$refs.reflectName.setTabType('top');
  55. /**
  56. * 设置tab高度
  57. * 参数一:高度
  58. */
  59. this.$refs.reflectName.setTabHeight(100);
  60. /**
  61. * 设置tab背景颜色
  62. * 参数一:颜色代码
  63. */
  64. this.$refs.reflectName.setTabBackgroundColor('#ffffff');
  65. /**
  66. * 设置tab字体大小
  67. * 参数一:字体大小
  68. */
  69. this.$refs.reflectName.setTabTextsize(22);
  70. /**
  71. * 设置tab字体加粗
  72. * 参数一:0|1|2
  73. */
  74. this.$refs.reflectName.setTabTextBold(0);
  75. /**
  76. * 设置tab未选字体颜色
  77. * 参数一:颜色代码
  78. */
  79. this.$refs.reflectName.setTabTextUnselectColor('#ffffff');
  80. /**
  81. * 设置tab已选字体颜色
  82. * 参数一:颜色代码
  83. */
  84. this.$refs.reflectName.setTabTextSelectColor('#00ffff');
  85. /**
  86. * 设置tab图标可见
  87. * 参数一:true|false
  88. */
  89. this.$refs.reflectName.setTabIconVisible(true);
  90. /**
  91. * 设置tab图标宽度
  92. * 参数一:宽度
  93. */
  94. this.$refs.reflectName.setTabIconWidth(50);
  95. /**
  96. * 设置tab图标高度
  97. * 参数一:高度
  98. */
  99. this.$refs.reflectName.setTabIconHeight(50);
  100. /**
  101. * 开关页面切换动画
  102. * 参数一:true|false
  103. */
  104. this.$refs.reflectName.setTabPageAnimated(false);
  105. /**
  106. * 手势滑动切换页面
  107. * 参数一:true|false
  108. */
  109. this.$refs.reflectName.setTabSlideSwitch(false);

tabbar-page

支持放置任何内容作为标签页内容。

tabbar-page 示例代码

  1. <tabbar-page
  2. ref="reflectName"
  3. tabName="name_3"
  4. title="圈子"
  5. message="99"
  6. selectedIcon="md-aperture">
  7. <div>
  8. <text>支持任何子组件</text>
  9. </div>
  10. </tabbar-page>

tabbar-page 配置参数

属性名 类型 描述 默认值
tabName String tab页签名称 -
title String tab名称 New Page
unSelectedIcon String tab未选图标 home
selectedIcon String tab已选图标 home
message Number tab未读信息数 0
dot Boolean 是否显示tab未读红点 false

例如:

  1. <tabbar-page
  2. ref="reflectName"
  3. tabName="name_3"
  4. title="圈子"
  5. message="99"
  6. selectedIcon="md-aperture">
  7. .....
  8. </tabbar-page>

tabbar-page 事件回调

  1. /**
  2. * 标签页下拉刷新事件
  3. * 注:刷新处理完毕后请调用方法“refreshEnd()”标记刷新结束
  4. * 返回参数:data = {tabName: 'tabName', title: 'title'}
  5. */
  6. @refreshListener = function(data) { ... }

tabbar-page 调用方法

  1. /**
  2. * 设置下拉刷新状态
  3. */
  4. this.$refs.reflectName.setRefresh();
  5. /**
  6. * 标记下拉刷新结束
  7. */
  8. this.$refs.reflectName.refreshEnd();

[拓展]完全自定义tabbar

在线示例

点击查看预览

示例代码

  1. <template>
  2. <div class="app">
  3. <div class="nav">
  4. <template v-for="(item, index) in tabPages">
  5. <div class="nav-item">
  6. <text :class="[index === position ? 'nav-item-text-active' : 'nav-item-text']" @click="switchPage(index)">{{item.title}}</text>
  7. <div v-if="index === position" class="nav-item-active"></div>
  8. </div>
  9. <text v-if="index < tabPages.length - 1" class="nav-line"></text>
  10. </template>
  11. </div>
  12. <tabbar
  13. ref="reflectName"
  14. class="tabbar"
  15. tabType="top"
  16. tabHeight="0"
  17. :tabPages="tabPages"
  18. @pageSelected="pageSelected"></tabbar>
  19. </div>
  20. </template>
  21. <style>
  22. .app {
  23. flex: 1
  24. }
  25. .nav {
  26. width: 750px;
  27. height: 100px;
  28. background-color: #3EB4FF;
  29. flex-direction: row;
  30. justify-content: center;
  31. align-items: center;
  32. }
  33. .nav-item {
  34. justify-content: center;
  35. align-items: center;
  36. }
  37. .nav-item-text,
  38. .nav-item-text-active {
  39. position: relative;
  40. width: 120px;
  41. height: 100px;
  42. line-height: 100px;
  43. text-align: center;
  44. font-size: 28px;
  45. color: #ffffff;
  46. font-weight: 300;
  47. }
  48. .nav-item-text-active {
  49. color: #E1C040;
  50. font-weight: 600;
  51. }
  52. .nav-item-active {
  53. position: absolute;
  54. left: 40px;
  55. bottom: 0;
  56. width: 40px;
  57. border-radius: 6px;
  58. height: 6px;
  59. background-color: #E1C040;
  60. }
  61. .nav-line {
  62. height: 24px;
  63. width: 1px;
  64. background-color: #ffffff;
  65. }
  66. .tabbar {
  67. flex: 1;
  68. width: 750px;
  69. }
  70. </style>
  71. <script>
  72. export default {
  73. data() {
  74. return {
  75. position: 0,
  76. tabPages: [
  77. {
  78. tabName: 'home',
  79. title: '首页',
  80. url: 'http://dotwe.org/raw/dist/b5fd96d8d790f0100bdfc20b93eedf09.bundle.wx',
  81. },
  82. {
  83. tabName: 'friend',
  84. title: '好友',
  85. url: 'http://dotwe.org/raw/dist/ba938c9aaebe41e5f60b98f90bd0bf61.bundle.wx',
  86. },
  87. {
  88. tabName: 'group',
  89. title: '圈子',
  90. url: 'http://dotwe.org/raw/dist/fb6f016b0116969b6b503e1d3a35285f.bundle.wx',
  91. },
  92. {
  93. tabName: 'setting',
  94. title: '设置',
  95. url: 'http://dotwe.org/raw/dist/ad0045a7cff0b3a680d9de6dd4806e81.bundle.wx',
  96. },
  97. ],
  98. }
  99. },
  100. methods: {
  101. pageSelected(data) {
  102. this.position = data.position;
  103. },
  104. switchPage(index) {
  105. this.position = index;
  106. this.$refs.reflectName.setCurrentItem(this.tabPages[index].tabName);
  107. }
  108. }
  109. };
  110. </script>