草庐IT

微信小程序自定义tabbar【中间凸起样式】

京茶吉鹿 2023-04-17 原文

微信小程序自定义tabBar样式,选中后中间凸起

效果预览

微信开发文档:自定义tabBar

一、配置信息

  • 在 app.json 中的 tabBar 中指定 custom 字段为 true【允许使用自定义 tabBar】

  • 在所有 tab 页 json 中申明usingComponents 项,或者在 app.json 中全局开启

  • 在 list 中指定自己需要 tab

  • 示例

    "tabBar": {
        "custom": true,
        "color": "#515151",
        "selectedColor": "#DAA520",
        "backgroundColor": "#000000",
        "list": [
            {
                "pagePath": "pages/index/index",
                "text": "首页"
            },
            {
                "pagePath": "pages/hospital/hospital",
                "text": "医院"
            },
            {
                "pagePath": "pages/publish/publish",
                "text": "item3"
            },
            {
                "pagePath": "pages/popularization/popularization",
                "text": "科普"
            },
            {
                "pagePath": "pages/me/me",
                "text": "我的"
            }
        ]
    },
    "usingComponents": {},
    

二、添加 tabBar 代码文件

在代码根目录下添加custom-tab-bar文件夹,并在该文件夹下新建 page,文件结构如下

|-- cusotm-tab-bar
​    |-- index.js
​​    |-- index.json
​   ​ |-- index.wxml
​   ​ |-- index.wxss

三、编写 tabBar 代码

  1. wxml 代码

    <!--custom-tab-bar/index.wxml-->
    <view class="tab-bar">
      <view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.bulge?'bulge':''}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
        <view  wx:if="item.bulge" class="tab-bar-bulge"></view>
        <image class="image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
        <!-- <view  wx:if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}" class="tab-bar-view">{{item.text}}</view> -->
        <view  class="tab-bar-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
      </view>
    </view>
    
  2. js 代码

    // custom-tab-bar/index.js
    Component({
      data: {
        color: "#515151",
        selectedColor: "#DAA520",
        backgroundColor: "#ffffff",
        list: [
          {
            pagePath: "/pages/index/index",
            text: "首页",
            iconPath: "/images/tabbar/index.png",
            selectedIconPath: "/images/tabbar/index-selected.png"
          },
          {
            pagePath: "/pages/hospital/hospital",
            text: "医院",
            iconPath: "/images/tabbar/hospital.png",
            selectedIconPath: "/images/tabbar/hospital-selected.png"
          },
          {
            pagePath: "/pages/publish/publish",
            bulge:true,
            text: "发布",
            iconPath: "/images/tabbar/dog.png",
            selectedIconPath: "/images/tabbar/dog-selected.png"
          },
          {
            pagePath: "/pages/popularization/popularization",
            text: "科普",
            iconPath: "/images/tabbar/popularization.png",
            selectedIconPath: "/images/tabbar/popularization-selected.png"
          },
          {
            pagePath: "/pages/me/me",
            text: "我的",
            iconPath: "/images/tabbar/me.png",
            selectedIconPath: "/images/tabbar/me-selected.png"
          },
        ]
      },
      attached() {
      },
      methods: {
        switchTab(e) {
          const data = e.currentTarget.dataset
          const url = data.path
          wx.switchTab({url}) 
        }
      }
    })
    
  3. wxss 代码

    .tab-bar {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      height: 50px;
      background: #FFF;
      display: flex;
      line-height: 1.2;
      padding-bottom: env(safe-area-inset-bottom);
      border-top: 1px solid #e6e6e6;
    }
    
    .tab-bar-item {
      flex: 1;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }
    
    .tab-bar-item .image {
      width: 26px;
      height: 26px;
    }
    .bulge {
      background-color: #FFF;
    }
    .bulge .tab-bar-bulge{
      position: absolute;
      z-index: -1;
      width: 64px;
      height: 64px;
      top: -24px;
      border-radius: 50%;
      border-top: 1px solid #e6e6e6;
      background-color: #FFF;
    }
    .bulge .image{
      position: absolute; 
      width: 50px;
      height: 50px;
      top: -16px;
    }
    .bulge .tab-bar-view{
      position: relative;
      bottom: -16px;
      margin-top: 4px;
    }
    
    .tab-bar-item .tab-bar-view {
      font-size: 12px;
      margin-top: 4px;
    }
    
    
  4. json 代码

    {
      "component": true
    }
    

四、配置 tab 页

在每一个 tab 页的onShow函数中写入下面的代码,其中 selected 的值为每个 tab 的索引

onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            // 首页为 0
            selected: 0
        })
    }
},

获取项目源代码

👇微信公众号【京茶吉鹿】内回复“微信小程序组件”👇

有关微信小程序自定义tabbar【中间凸起样式】的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  7. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  8. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  9. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐