微信小程序 自定义 Tab custom-tab-bar 点击刷新,回到首页

摘要:
前言:微信项目需要有二维码功能,显示时应突出显示。因此,它只能是定制的。让我们先看看我们想做什么,看看看到阴影时是否有点恐慌。不要害怕。这很简单。修改appJson文件,将tabBar更改为自定义选项卡,并保持列表不变。最多5个“tabBars”:{“selectedColor”:“#FB7F32”,“borderStyle”:“white”,“custom”:true,//自定义选项卡

前言:

微信项目中需要做个二维码的功能,显示的时候要突出出来,So, 只能自定义了,先看一下我们要做成这个样子,看到这个阴影是不是有点恐慌,不要怕,很简单

微信小程序 自定义 Tab custom-tab-bar 点击刷新,回到首页第1张

一、修改 app.json 文件,将 tabBar修改为自定义tab,list 保持不变,最多5个

  "tabBar": {
    "selectedColor": "#FB7F32",
    "borderStyle": "white",
    "custom": true, // 自定义tab
    "usingComponents": {},
   "list": [ { "pagePath": "pages/Home/index", "iconPath": "image/home.png", "selectedIconPath": "image/homeActive.png", "text": "组件" }, { "pagePath": "pages/Message/index", "iconPath": "image/message.png", "selectedIconPath": "image/messageActive.png", "text": "消息中心" }, { "pagePath": "pages/Home/index", "iconPath": "image/qrCode.png", "selectedIconPath": "image/qrCode.png", "text": "" }, { "pagePath": "pages/Home/index", "iconPath": "image/order.png", "selectedIconPath": "image/orderActive.png", "text": "订单" }, { "pagePath": "pages/Home/index", "iconPath": "image/mine.png", "selectedIconPath": "image/mineActive.png", "text": "我的" } ]

  

二、在项目的根目录新建 custom-tab-bar,这个文件就是微信已经帮我们定义好了,只需要新增文件,就可以微信自动读取

微信小程序 自定义 Tab custom-tab-bar 点击刷新,回到首页第2张

三、custom-tab-bar/index.wxml 写入,官网中提供,使用 cover-view 标签来操作,目前我这边是使用view,因为  cover-view 在向上滑动的时候,会带着tab 一起拖上去,不太好看,

 

<view   style="background: url('{{background}}') no-repeat; background-size: 100% auto">
  <view wx:for="{{list}}" wx:key="index"   data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab" >
    <view   >
      <view   wx:if="{{item.isSpecial}}">
        <image   src="http://t.zoukankan.com/image/qrCode.png" ></image>
      </view>
      <image   src="http://t.zoukankan.com/{{selected === index ? item.selectedIconPath : item.iconPath}}" wx:else></image>
    </view>
    <view   class="cover-text">{{item.text}}</view>
  </view>
</view>

  

四、custom-tab-bar/index.js 写入

const app = getApp()

Component({
  data: {
    selected: 0, // 目前tab所在的index 
    color: "#999", // 未选中颜色
    selectedColor: "#D0021B", // 选中颜色
  // tab 自定义配置需与index.json 保持一致 list: [{ pagePath: "/pages/Home/index", iconPath: "../image/home.png", selectedIconPath: "../image/homeActive.png", text: "首页", isSpecial: false }, { pagePath: "/pages/Message/index", iconPath: "../image/message.png", selectedIconPath: "../image/messageActive.png", text: "消息中心", isSpecial: false }, { pagePath: "/pages/pay/index", iconPath: "image/icon_API.png", selectedIconPath: "image/icon_API_HL.png", text: "", isSpecial: true }, { pagePath: "/index/index2", iconPath: "../image/order.png", selectedIconPath: "../image/orderActive.png", text: "历史订单", isSpecial: false }, { pagePath: "/index/index2", iconPath: "../image/mine.png", selectedIconPath: "../image/mineActive.png", text: "我的", isSpecial: false }], }, methods: {
  // 切换 tab 事件 switchTab(e) { let that = this const idx = e.currentTarget.dataset.index const path = e.currentTarget.dataset.path // 跳转页面 wx.switchTab({ url: path, }) that.setData({ selected: idx }) } } })

  

五、custom-tab-bar/index.wxss 写入

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 90rpx;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 99;
  position: relative;
  padding-top: 17rpx;
}
 
.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
 
.tab-bar-item cover-image {
   27px;
  height: 27px;
}
 
.tab-bar-item .cover-text {
  font-size: 10px;
}
.txt{
  color: #aaaaaa;
}
.fontWeight{
  font-weight: bold;
}
.bg_rec{
  background: #ffd324;
   80rpx;
  min-height: auto;
  height: 20rpx;
  margin-top: -28rpx;
  vertical-align: text-bottom;
  border-radius: 0;
  z-index: -10;
}
.center_img{
   100rpx;
  height: 100rpx;
  transform: translate(-50%);
  left: 50%;
  bottom:0;
}
.center-has-noimg{
   100%;
  height: 100%;
}
.center-has-image{
   35rpx;
  height: 35rpx;  
}
.center_part_code{
  padding: 10rpx;
  box-shadow: 0 0 0 #000;
  /*  100rpx;
  height: 100rpx; */
  position: absolute;
  top: -30px;
  z-index: 10;
   106rpx;
  height: 106rpx;
  transform: translate(-50%);
  left: 50%;
}

  

六、查看效果

发现:view 一直定在下部,滑动的时候不太好看,我这边在 每个switchTab页面的json 中配置了,禁止滚动,在页面内给 父级设置 overflow: scroll,( switchtab页面为tabBar 中list 配置的页面)

七、总结问题

1. 进入首页,点击其他tab,页面会刷新,tabindex 会再次回到首页

  解决方案:在每个 switchtab页面 中写入以下 ,( switchtab页面为tabBar 中list 配置的页面)

  onShow:function (params) {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0  // 数字是当前页面在tabbar的索引,如我的查询页索引是2,因此这边为2,同理首页就为0,消息中心页面为1
      })
    }
  },

  

免责声明:文章转载自《微信小程序 自定义 Tab custom-tab-bar 点击刷新,回到首页》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇实时监听输入框值变化:oninput &amp;amp; onpropertychange分解uber依赖注入库dig-使用篇下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于微信小程序map地图的使用

结构部分:  <map bindregionchange='bindregionchange' show-location longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' scale='18' > <cover-image bindtap='...

微信小程序设置全局请求URL 封装wx.request请求

app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * method: 请求方式 * url: 请求地址 * data: 要传递的参数 * callback: 请求...

使用android.view.TouchDelegate扩大View的触摸点击区域

Android4.0设计规定的有效可触摸的UI元素标准是48dp,转化为一个物理尺寸约为9毫米。7~10毫米,这是一个用户手指能准确并且舒适触摸的区域。 如下图所示,你的UI元素可能小于48dp,图标仅有32dp,按钮仅有40dp,但是他们的实际可操作焦点区域最好都应达到48dp的大小。 为使小的UI区域获得良好的触摸交互,根据View的特性,目前碰到了...

微信小程序使用阿里巴巴矢量图

一、symbol 1.搭建环境(在项目根目录打开cmd窗口)  npm init -y 这时根目录会多出一个package.json文件 2.mini-program-iconfont-cli(PS:一种适合小程序的多色解决方案) npm install mini-program-iconfont-cli --save-dev  提示有个高危漏洞,不管...

关于View转化成bitmap保存成图片

产品今天说项目分享时要分享出一张  封面图片 + 几行文字 + 二维码图片 的图片。 思索了一下 封面图片和二维码图片让后台给接口得到地址, 主要还是找个方式得到一个包含这些内容的图片。于是就想能不能将View转化成bitmap对象 然后就走了一遍各个前辈的路 整理了下原理和思路。        根据产品的需求  我要实现的步骤  把所有需要的集合在一个V...

Android游戏开发教程之六:自定义View详解

  在Android游戏开发中,有时Android控件不能满足我们的要求,就有必要使用Android自定义View。自定义View实现起来也不难,就是先继承View类,然后重写构造函数、onDraw、onMeasure等函数。        View需处理的三个问题        对于常规的游戏,我们在View中需要处理以下几种问题: 1.控制事件;...