TabBarView.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * App端 手绘 tabbar 可以实现凸起按钮
  3. * version:1.0.2
  4. */
  5. export default class TabBarView{
  6. constructor(){
  7. //tabbar 配置
  8. this.configs = {
  9. background:'#fff', //背景
  10. height: 50 , //高度
  11. fontSize: 15, //字体大小
  12. iconWidth: 24, //图片大小
  13. borderStyle:'#F5F5F5', //bordercolor
  14. color:'#000000', //字体颜色
  15. selectedColor:'#038AFF',
  16. midIconWidth:60 //中间凸起按钮大小
  17. }
  18. //白名单 不需要配置
  19. this.whiteList = [];
  20. // //tabbar 列表 midButton:true 可实现凸起 想凸几个凸几个
  21. this.tabBarList =[{
  22. icon: "./static/images/common/huoyuan@2x(1).png",
  23. selectIcon: "./static/images/common/huoyuan@2x.png",
  24. name: "货源",
  25. path: "/pages/goodSource/index"
  26. },
  27. {
  28. "path": "/pages/riders/index",
  29. "icon": "static/images/common/zhaoche.png",
  30. "selectIcon": "static/images/common/zhaoche_check.png",
  31. "name": "车友"
  32. },
  33. {
  34. "path": "/pages/order/index",
  35. "icon": "/static/images/common/dingdan@2x(1).png",
  36. "selectIcon": "/static/images/common/dingdan@2x.png",
  37. "name": "订单"
  38. },
  39. {
  40. "path": "/pages/news/index",
  41. "icon": "/static/images/common/mesicon@2x.png",
  42. "selectIcon": "/static/images/common/xiaoxi@2x.png",
  43. "name": "消息"
  44. },
  45. {
  46. "path": "/pages/mine/index",
  47. "icon": "/static/images/common/iconmy@2x.png",
  48. "selectIcon": "/static/images/common/my@2x.png",
  49. "name": "我的"
  50. }
  51. ]
  52. //tabbarItem宽度
  53. this.tabbarItemWidth = Math.floor( 100 / this.tabBarList.length );
  54. }
  55. //开局初始化tabbar
  56. init(){
  57. this.whiteList = [];
  58. //创建tabbar盒子
  59. const tabBarBox = new plus.nativeObj.View('tabBarBox', {
  60. position:'dock',
  61. dock:'bottom',
  62. bottom:'0px',
  63. left:'0px',
  64. width:'100%',
  65. height: this.configs.height + 'px',
  66. backgroundColor:this.configs.background
  67. },[
  68. {
  69. tag:'rect',
  70. id:'taBarBoxRect',
  71. rectStyles:{
  72. color:this.configs.borderStyle
  73. },
  74. position:{
  75. top:'0px',
  76. left:'0px',
  77. width:"100%",
  78. height:'1px'
  79. }
  80. }
  81. ]);
  82. //显示tabbar 盒子
  83. tabBarBox.show();
  84. //创建tabbar item
  85. for(let i = 0; i < this.tabBarList.length; i++){
  86. const tabItem = this.tabBarList[i];
  87. this.whiteList.push(tabItem.path);
  88. const tabbarItem = new plus.nativeObj.View('tabBarItem' + i,this.setTabBarStyle(tabItem.midButton,i),this.drawTabBar(false,tabItem));
  89. //添加tabbarItem 事件 根据自己需求 默认调转页面
  90. tabbarItem.addEventListener("click", function(e) {
  91. uni.switchTab({
  92. url:tabItem.path
  93. })
  94. }, false);
  95. }
  96. }
  97. //监听tabbar
  98. $watch(){
  99. setTimeout(()=>{
  100. const pages = getCurrentPages();
  101. const page = pages[pages.length - 1];
  102. const path = '/'+ page.route;
  103. const tabBarBox = plus.nativeObj.View.getViewById('tabBarBox');
  104. for(let i = 0; i < this.tabBarList.length; i++){
  105. const tabItem = this.tabBarList[i];
  106. const tabBarItem = plus.nativeObj.View.getViewById('tabBarItem' + i);
  107. tabBarItem.draw(this.drawTabBar(tabItem.path === path,tabItem))
  108. if(this.whiteList.indexOf(path) != -1){
  109. tabBarItem.show();
  110. }else{
  111. tabBarItem.hide();
  112. }
  113. }
  114. if(this.whiteList.indexOf(path) != -1){
  115. tabBarBox.show();
  116. }else{
  117. tabBarBox.hide();
  118. }
  119. },0)
  120. }
  121. //设置 tabbar 风格
  122. setTabBarStyle(midButton,i){
  123. return {
  124. position:'dock',
  125. bottom:'0px',
  126. left:this.tabbarItemWidth * i + '%',
  127. dock:'bottom',
  128. width: this.tabbarItemWidth + '%' ,
  129. height: midButton ? this.configs.height + (this.configs.midIconWidth / 2) + 'px' : this.configs.height - 1 + 'px'
  130. }
  131. }
  132. //画 tabbar 样式
  133. drawTabBar(select,tabItem){
  134. return [
  135. {
  136. tag:'img',
  137. id:'tabItemIcon',
  138. src:select ? tabItem.selectIcon : tabItem.icon,
  139. position:{
  140. top:tabItem.midButton ? '0px' : '4px',
  141. left:'auto',
  142. width:tabItem.midButton ? this.configs.midIconWidth : this.configs.iconWidth,
  143. height:tabItem.midButton ? this.configs.midIconWidth : this.configs.iconWidth,
  144. }
  145. },
  146. {
  147. tag:'font',
  148. id:'tabItemFont',
  149. text: tabItem.name,
  150. textStyles:{
  151. size:'10px',
  152. color:select ? this.configs.selectedColor : this.configs.color,
  153. },
  154. position:{
  155. bottom:'5px',
  156. left:'auto',
  157. width:'100%',
  158. height:'12px'
  159. }
  160. }
  161. ]
  162. }
  163. }