首页 > 微信 > 详细

微信小程序自定义tabbar的实现

时间:2019-10-11 19:53:35      阅读:158      评论:0      收藏:0      [点我收藏+]

微信小程序自定义tabbar的实现

目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果;当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置。

方案:自己动手写一个和微信小程序提供的tabbar相同的效果,而且还可以满足灵活配置的功能.有参照有赞小程序

效果:

  1. 效果A:

         技术分享图片

 

          2.效果B:

       

         技术分享图片         

 

 

 实现:

             项目结构如下:

              技术分享图片

       

            II 效果A实现:

 

                1》wxml的代码:

               
Page({
  data: {
    blockid:0,
    bgcolor:‘#ffffff‘,
    color:"#cccccc",
    selectedColor:‘#333333‘,
    showborder:false,
    bordercolor:"",
    tabbar:[
      {
        pagePath: "page/home0/index",
        selectedIconPath: ‘/resources/tabbar/homeh.png‘,
        iconPath: ‘/resources/tabbar/home.png‘,
        text: ‘首页A‘,
        isdot: false,
        number: 0
      }, {
        pagePath: "page/home1/index",
        selectedIconPath: ‘/resources/tabbar/kindh.png‘,
        iconPath: ‘/resources/tabbar/kind.png‘,
        text: ‘首页B‘,
        isdot: true,
        number: 0
      }, {
        pagePath: "page/home2/index",
        selectedIconPath: ‘/resources/tabbar/myh.png‘,
        iconPath: ‘/resources/tabbar/my.png‘,
        text: ‘首页C‘,
        isdot: false,
        number: 5
      }, {
        pagePath: "page/home3/index",
        selectedIconPath: ‘/resources/tabbar/shopcarth.png‘,
        iconPath: ‘/resources/tabbar/shopcart.png‘,
        text: ‘首页D‘,
        isdot: false,
        number: 0
      }
    ]
  },
  // event.detail 的值为当前选中项的索引
  tabbarChange(e) {
   
    var index = parseInt(e.detail);
    this.setData({
      blockid:index
    })
  }
})

          

          2》json文件如下:

        
{
  "usingComponents": {
    "tabbar": "../../components/tabbar/index"
  }
}

 

          3》js文件如下:
          
Page({
  data: {
    blockid:0,
    bgcolor:‘#ffffff‘,
    color:"#cccccc",
    selectedColor:‘#333333‘,
    showborder:false,
    bordercolor:"",
    tabbar:[
      {
        pagePath: "page/home0/index",
        selectedIconPath: ‘/resources/tabbar/homeh.png‘,
        iconPath: ‘/resources/tabbar/home.png‘,
        text: ‘首页A‘,
        isdot: false,
        number: 0
      }, {
        pagePath: "page/home1/index",
        selectedIconPath: ‘/resources/tabbar/kindh.png‘,
        iconPath: ‘/resources/tabbar/kind.png‘,
        text: ‘首页B‘,
        isdot: true,
        number: 0
      }, {
        pagePath: "page/home2/index",
        selectedIconPath: ‘/resources/tabbar/myh.png‘,
        iconPath: ‘/resources/tabbar/my.png‘,
        text: ‘首页C‘,
        isdot: false,
        number: 5
      }, {
        pagePath: "page/home3/index",
        selectedIconPath: ‘/resources/tabbar/shopcarth.png‘,
        iconPath: ‘/resources/tabbar/shopcart.png‘,
        text: ‘首页D‘,
        isdot: false,
        number: 0
      }
    ]
  },
  // event.detail 的值为当前选中项的索引
  tabbarChange(e) {
   
    var index = parseInt(e.detail);
    this.setData({
      blockid:index
    })
  }
})

 

 

        III效果B实现:

               1》wxml的代码:

<block wx:if="{{blockid==0}}">
  这里是首页A
</block>
<block wx:if="{{blockid==1}}">
  这里是首页B
</block>
<block wx:if="{{blockid==2}}">
  这里是首页C
</block>
<block wx:if="{{blockid==3}}">
  这里是首页D
</block>

<!--底部tabbar【一般正常用法,index页面特殊用法】-->
<tabbar tabbarData="{{tabbar}}" active="{{0}}" bgcolor="{{bgcolor}}" color="{{color}}" selectedColor="{{selectedColor}}"  showborder="{{showborder}}"  bind:tapChange="tabbarChange" />

         2》json的代码

   

{
  "usingComponents": {
    "tabbar": "../../components/tabbar/index"
  }
}

      3》js的代码

 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    blockid: 0,
    bgcolor: ‘green‘,
    color: "red",
    selectedColor: ‘blue‘,
    showborder: false,
    bordercolor: "",
    tabbar: [{
      pagePath: "page/home0/index",
      selectedIconPath: ‘/resources/tabbar/homeh.png‘,
      iconPath: ‘/resources/tabbar/home.png‘,
      text: ‘首页A‘,
    }, {
      pagePath: "page/home1/index",
      selectedIconPath: ‘/resources/tabbar/kindh.png‘,
      iconPath: ‘/resources/tabbar/kind.png‘,
      text: ‘首页B‘,
    }, {
      pagePath: "page/home2/index",
      selectedIconPath: ‘/resources/tabbar/myh.png‘,
      iconPath: ‘/resources/tabbar/my.png‘,
      text: ‘首页C‘,
    }, {
      pagePath: "page/home3/index",
      selectedIconPath: ‘/resources/tabbar/shopcarth.png‘,
      iconPath: ‘/resources/tabbar/shopcart.png‘,
      text: ‘首页D‘,
    }]
  },
  // event.detail 的值为当前选中项的索引
  tabbarChange(e) {

    var index = parseInt(e.detail);
    this.setData({
      blockid: index
    })
  },
})

 

 

 

 

github地址如下:https://github.com/weiyunhelong/WeChatTabbar,欢迎下载并使用!

 

微信小程序自定义tabbar的实现

原文:https://www.cnblogs.com/tudaogaoyang/p/11656054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!