当前位置:   article > 正文

uni-app自定义底部导航栏_uniapp自定义底部tabbar

uniapp自定义底部tabbar

 整体代码

  1. <template>
  2. <view class="tabbar" :style="{paddingBottom:PaddingBottom}">
  3. <template v-for="(item,index) in list">
  4. <view class="pageList" @click="toNewPage(item.path)">
  5. <image :src="item.trueIcon" class="icon" v-if="pageIndex == index"></image>
  6. <image :src="item.icon" class="icon" v-else></image>
  7. <view class="pageName" :class="pageIndex == index ? 'nowPage':''">
  8. {{item.text}}
  9. </view>
  10. </view>
  11. </template>
  12. </view>
  13. </template>
  14. <script>
  15. export default{
  16. props:{
  17. pageIndex:{
  18. type:Number,
  19. default:0
  20. }
  21. },
  22. data(){
  23. return{
  24. PaddingBottom:'0',
  25. list:[
  26. {
  27. text:'记账',
  28. icon:'/static/icon/bookkeeping.png',
  29. trueIcon:'/static/icon/truebookkeeping.png',
  30. path:'/pages/bookkeeping/bookkeeping'
  31. },
  32. {
  33. text:'财富',
  34. icon:'/static/icon/wealth.png',
  35. trueIcon:'/static/icon/truewealth.png',
  36. path:'/pages/wealth/wealth'
  37. },
  38. ]
  39. }
  40. },
  41. methods:{
  42. /**
  43. *@description 跳转到新页面
  44. *@param {String} path 要跳转的路径
  45. */
  46. toNewPage(path){
  47. uni.switchTab({
  48. url: path,
  49. fail(res) {
  50. // console.log(res)
  51. }
  52. })
  53. }
  54. },
  55. mounted(){
  56. this.PaddingBottom = this.$publicFn.setSafeDistance().bottom
  57. }
  58. }
  59. </script>
  60. <style lang="scss">
  61. @import "../../static/scss/publicScss.scss";
  62. @include mediaModel(){
  63. .tabbar{
  64. display: none;
  65. }
  66. }
  67. @include mediaModel('phone'){
  68. .tabbar{
  69. @include displayFlex($justify:space-around);
  70. position: fixed;
  71. height: 100rpx;
  72. bottom: 0;
  73. background-color: $domColor;
  74. width: 100%;
  75. box-shadow: 0 -10px 10px #ececec;
  76. z-index: 999;
  77. .pageList{
  78. // @include displayFlex();
  79. margin: 0 40rpx;
  80. .icon{
  81. width: 40rpx;
  82. height: 40rpx;
  83. }
  84. .pageName{
  85. font:{
  86. size: 14rpx;
  87. }
  88. text-align: center;
  89. }
  90. .nowPage{
  91. color: $tabbarText;
  92. }
  93. }
  94. }
  95. }
  96. </style>

补充

获取页面显示的安全距离

封装的公共函数--用于获取手机端的安全可视距离(顶部top和底部bottom)

  1. /**
  2. * @description 获取页面的安全显示区域
  3. * @returns { object } 返回的安全top,bottom
  4. */
  5. setSafeDistance(){
  6. let app = uni.getSystemInfoSync()
  7. let top = app.statusBarHeight+'px'
  8. let bottom = app.safeAreaInsets.bottom+'px'
  9. let safe = {
  10. top:top,
  11. bottom:bottom
  12. }
  13. return safe;
  14. }

进行页面点击跳转

 官网文档:uni.navigateTo(OBJECT) | uni-app官网

注意:

1、path的跳转路径前要加 " / "

2、pages.json文件夹要对tabBar进行配置要不不进行跳转
  1. "tabBar": {
  2. "height": "1px",
  3. "custom": true,
  4. "list": [
  5. {
  6. "pagePath": "pages/bookkeeping/bookkeeping",
  7. "text": ""
  8. },
  9. {
  10. "pagePath": "pages/wealth/wealth",
  11. "text": ""
  12. }
  13. ]
  14. }

scss简化媒体查询

  1. //媒体查询响应式开发
  2. @mixin mediaModel($model:'pc'){
  3. @if $model == "pc"{
  4. @media (min-width:481px){
  5. @content;
  6. }
  7. }@else{
  8. @media (max-width:480px){
  9. @content;
  10. }
  11. }
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/531648
推荐阅读
相关标签
  

闽ICP备14008679号