关键词搜索

全站搜索
×
密码登录在这里
×
注册会员

已有账号? 请点击

忘记密码

已有账号? 请点击

使用其他方式登录

微信小程序自定义头部导航栏返回

发布2022-05-20 浏览980次

详情内容

在微信小程序开发时,可以自定义头部导航栏返回,先看下效果。

image.png

image.png

下面实现的方式和代码。

1、微信自定义头部:app.json里面配置: "navigationStyle": "custom"

组件index.js

// components/navbar/index.js
const app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: 'Wechat'
    },
    back: {
      type: Boolean,
      value: false
    },
    home: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight + 'px',
    navigationBarHeight: (app.globalData.statusBarHeight + 44) + 'px',
    statusBarHeight: app.globalData.statusBarHeight+'px'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    backHome: function () {
      wx.navigateTo({
        url: '/pages/home/index',
      })
    },
    back: function () {
      wx.navigateBack({
        delta: 1
      })
    }

  }
})

index.wxml

<!--components/navbar/index.wxml-->
<view class="navbar" style="{{'height: ' + navigationBarHeight}}">
  <view style="{{'height: ' + statusBarHeight}}"></view>
  <view class='title-container'>
    <view class='capsule' wx:if="{{ back || home }}">
      <view bindtap='back' class="iconfont icon-fanhui" wx:if="{{back}}">
           
      </view>
      <view bindtap='backHome' class="iconfont icon-zhuomianzhuye" wx:if="{{home}}">
      </view>
    </view>
    <view class='title'>{{title}}</view>
  </view>
</view>
<view style="height:{{navigationBarHeight}};background: white;"></view>

index.wxss

.navbar {
  width: 100%;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}
.title-container {
  height: 40px;
  display: flex;
  align-items: center;
  position: relative;
}
.capsule {
  margin-left: 10px;
  height: 30px;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid #dddddd;
  border-radius: 16px;
  display: flex;
  align-items: center;
}
.capsule > view {
  width: 45px;
  height: 60%;
  position: relative;text-align: center;
}

.icon-fanhui:before {
 
  content: '';display: inline-block;width: 26rpx;height: 26rpx;
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAR1JREFUOE+t1L1KBDEQB/D/nLtisVaCCIIsWOyaBCsrKysbKwsP7LS5zkoQrAQrq6tEECsbFX2cybyCja8grESuWMJtkt27tMn8mC9CWPKhIZ7WekpEv9baGz++N2iM+QRwBuDHWru5ENjCQER3zHw/GPSwW2Z+mNeupJI97JqZp129j4IedsXMj6FBBsE21jTNREReYlvRCXrYhYi8xjB3Pxf0sHMR+UjBOkGt9TMRTf4fEI2Z+Wsh0BizB8Bltd8XDfVwd4Ye9EGDU1ZK7YxGo3cAh6lodA+VUlsz9CgFjYIOqet6I8uyNwDHMTQJdEhVVet5nrvyT0JoMuiQsizXiqJwmZ4S0Tczbw/+bVqBK1rrJwCrInK5DDC443+qIGIVtG4bDwAAAABJRU5ErkJggg==) center center no-repeat;background-size: cover;
}
.icon-zhuomianzhuye:before {
  
  content: '';display: inline-block;width: 26rpx;height: 26rpx;
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAaZJREFUOE/tk7FLW1EUxr9zk61Tp67SKX3v3EcRimPrUkqnOri2kS4FMZtbQVuFQJ1LqIOQLi4i2LEO1cWhW3jnPkxByF/QpW6Be+RKXojJS2J07R3v/e7vfpzvu4RbrDiOn6kqZVn2e5qcpgmsta+991+JiFS15pz7MenORGAURVVjTAPAR1UtEdFnIqqlabo7DjoWyMzrALZVteqc2w8AZn4PoEFEW2mabhVBC4HM/EVVl4loRUROBi9aa9+oanB9KCKrw9AbwCRJHnjvvwF4XCqVqq1W60+RiyRJ5lX1u6qee++rWZZd5ro+sFKpzJXL5T0Af7vd7kq73f43afjW2ofe+yMiMgDeichF0F8DoyhaMMY0VfXYObc2LfnBc2Y+APAUwFsROSNmXgLQBFAXkfossFzLzDsAPgRoAG567zvGmE4QDIcw7QFmftHTPAHwKABfishPZv4F4NMdgRsishjH8at+KEXA4L7IoYj093sOr4H9UHqlHXHYe+R0GHpvYA7IHf8HhtrMFsp9ZljUktMh4PMxVRqpTd74EX1e9oFfMVZzBb8dEcYU8lNsAAAAAElFTkSuQmCC);background-size: cover;
}

.capsule > view:nth-child(2) {
  border-left: 1px solid #dddddd;  
}
.capsule image {
  width: 50%;
  height: 100%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.title {
  color: #333;
  position: absolute;
  top: 6px;
  left: 104px;
  right: 104px;
  height: 30px;
  line-height: 30px;
  font-size: 16px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

index.json

{
  "component": true,
  "usingComponents": {}
}

app.js

onLaunch() {
    

    //获取右上角菜单按钮的布局位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    //获取系统信息
    wx.getSystemInfo({
      success: res => {
        //console.log('xxxx', res)
        //状态栏的高度
        let statusBarHeight = res.statusBarHeight,
        //胶囊按钮与顶部的距离
        navTop = menuButtonObject.top,
        navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let globalData = this.globalData;
        globalData.statusBarHeight = statusBarHeight;
        globalData.navHeight = navHeight;//导航栏高度
        globalData.navTop = navTop;//胶囊按钮与顶部的距离
        globalData.jnheight = menuButtonObject.height;//胶囊的高度
        globalData.jnwidth = menuButtonObject.width;//胶囊的宽度
      },
      fail(err) {
        console.log(err);
      }
    });
},
globalData: {
    statusBarHeight:0,
    navHeight:0,
   
  }

在页面时需要使用的话,先引用组件

{
  "usingComponents": {
    "navbar": "/components/navbar/index"
  }
}
<!--pages/index.wxml-->
<navbar title="这是标题" back="true" home="true"/>


点击QQ咨询
开通会员
上传资源赚钱
返回顶部
×
  • 微信支付
  • 支付宝付款
扫码支付
微信扫码支付
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载