微信小程序> 小程序自定义导航栏返回主页

小程序自定义导航栏返回主页

浏览量:566 时间: 来源:生如夏花S

小程序自定义导航栏返回主页

    • 效果图
    • 在app.js中获取状态栏的高度statusBarHeight
    • 自定义组件navbar.wxml
    • 自定义组件navbar.wxss
    • 自定义组件navbar.json
    • 自定义组件navbar.js
    • 调用组件的页面.json
    • 调用组件的页面.wxml

效果图

小程序

在app.js中获取状态栏的高度statusBarHeight

globalData: {    statusBarHeight:wx.getSystemInfoSync()['statusBarHeight']  }

自定义组件navbar.wxml

view class="custom " style="padding-top:{{statusBarHeight}}px"  view class='title-container'    view class='capsule' wx:if="{{ back || home }}"      view bindtap='back' wx:if="{{back}}"        image src='img/back.svg'/image      /view      view bindtap='backHome' wx:if="{{home}}"        image src='img/home.svg'/image      /view    /view    view class='title'{{navigationBarTitle}}/view  /view/viewview class="empty_custom" style="padding-top:{{statusBarHeight}}px"/view

自定义组件navbar.wxss

.custom{  position: fixed;  width: 100%;  top: 0;  left: 0;  height: 45px;  background: #1d8be8;  z-index: 999;}.title-container {  height: 44px;  display: flex;  align-items: center;  position: relative;}.capsule {  margin-left: 10px;  height: 32px;  border: 1px solid #fff;  border-radius: 16px;  display: flex;  align-items: center;}.capsule  view {  width: 32px;  height: 60%;  position: relative;}.capsule  view:nth-child(2) {  border-left: 1px solid #fff;}.capsule image {  width: 60%;  height: 100%;  position: absolute;  left: 50%;  top: 50%;  transform: translate(-50%,-50%);}.title {  color: white;  position: absolute;  left: 104px;  right: 104px;  font-size: 14px;  text-align: center;  overflow: hidden;  text-overflow: ellipsis;  white-space: nowrap;}

自定义组件navbar.json

{  "component": true}

自定义组件navbar.js

const app = getApp()Component({  properties: {    navigationBarTitle: {      type: String,      value: ''    },    back: {      type: Boolean,      value: false    },    home: {      type: Boolean,      value: false    }  },  data: {    statusBarHeight: app.globalData.statusBarHeight  },  methods: {    backHome: function () {      wx.reLaunch({        url: '/pages/index/index',      })    },    back: function () {      wx.navigateBack({        delta: 1      })    }  }})

调用组件的页面.json

{  "usingComponents": {    "navBar": "/pages/common/navbar/navbar",  },  "navigationStyle": "custom",//导航栏样式,custom 模式可自定义导航栏,只保留右上角胶囊状的按钮.  "navigationBarBackgroundColor": "#1D8BE8",  "navigationBarTextStyle": "white"}

调用组件的页面.wxml

navBar back home navigationBarTitle="{{navigationBarTitle}}"/navBar

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎