小程序UI(自定义Navbar)

组件代码

javascript 复制代码
// app.js
App({
  onLaunch() {
     const systemInfo = wx.getSystemInfoSync();
     this.globalData.statusBarHeight = systemInfo.statusBarHeight;
  },
  globalData: {
    statusBarHeight: 0,
  }
})

header.wxml

html 复制代码
<view class="header custom {{fixedTop?'fixed':''}}" style="padding-top:{{statusBarHeight}}px; background: {{background}}; z-index: 10">
  <view class="navigation" wx:if="{{nav}}">
    <navigator class="nav" hoverClass="none" openType="navigateBack" wx:if="{{canGoBack&&autoGoBack}}">
      <image src="https://nodestatic.fbstatic.cn/wxmini/imgs/back.png"></image>
    </navigator>
    <view bindtap="back" class="nav" wx:if="{{canGoBack&&!autoGoBack}}">
      <image src="https://nodestatic.fbstatic.cn/wxmini/imgs/back.png"></image>
    </view>
    <text wx:if="{{canGoBack&&!small}}">|</text>
    <navigator class="nav" hoverClass="none" openType="reLaunch" url="../../pages/index/index" wx:if="{{!(small&&canGoBack)}}">
      <image src="https://nodestatic.fbstatic.cn/wxmini/imgs/home.png"></image>
    </navigator>
  </view>
  <slot></slot>
  <text class="title" style="margin-right: {{small?80:0}}rpx;color:#{{small?'3C464F':'000'}}" wx:if="{{showTitle}}">{{title}}</text>
</view>
<view class="{{!fixedTop?'fixed':''}}" style="height: calc({{statusBarHeight+44}}px)"></view>

CSS

css 复制代码
.header {
  box-sizing: initial;
  color: #3c464f;
  font-size: 34rpx;
  font-weight: 700;
  height: 44px;
  line-height: 44px;
  position: relative;
  text-align: center
}

.navigation {
  align-items: center;
  background: #fff;
  border: 1px solid #ebebeb;
  border-radius: 28px;
  bottom: 8px;
  color: #ebebeb;
  display: -webkit-flex;
  display: flex;
  font-weight: 100;
  height: 28px;
  justify-content: center;
  left: 15rpx;
  line-height: 0;
  position: absolute
}

.header .navigation text {
  display: inline;
  flex: none;
  font-size: 16px;
  margin: 0
}

.header .navigation image {
  height: 16px;
  width: 16px
}

.header .nav {
  height: 16px;
  width: 38px
}

.fixed {
  left: 0;
  position: fixed;
  right: 0;
  top: 0;
  z-index: -100
}

.header .title {
  display: inline-block;
  max-width: 390rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap
}

@media screen and (min-width:600px) {
  .header {
    font-size: 20px
  }
}

JS

javascript 复制代码
const app = getApp();

Component({
  options: {
    
  },

  properties: {
    title: {
      value: "",
      type: String
    },
    background: {
      value: "#fff",
      type: String
    },
    nav: {
      value: true,
      type: Boolean
    },
    fixedTop: {
      value: true,
      type: Boolean
    },
    small: {
      value: false,
      type: Boolean
    },
    showTitle: {
      value: true,
      type: Boolean
    },
    autoGoBack: {
      value: true,
      type: Boolean
    }
  },

  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    canGoBack: false
  },


  attached: function () {
    this.setData({
      canGoBack: getCurrentPages().length > 1
    });
  },

  methods: {
    back: function () {
      this.triggerEvent("back");
    }
  }
});

使用

javascript 复制代码
{
  "usingComponents": {
    "header": "../../components/header/header"
  }
}

index.wxml

html 复制代码
<header title="关于我们"></header>
html 复制代码
<header title="证件照首页" nav="{{false}}"></header>
<navigator class="about-us" hoverClass="none" style="top: calc({{statusBarHeight+22}}px - 24rpx)" url="../about-us/about-us">
  <image src="/static/icon-about-us.png"></image>
</navigator>

CSS

css 复制代码
.about-us {
  left: 30rpx;
  position: fixed;
  z-index: 10
}

.about-us,
.about-us image {
  height: 48rpx;
  width: 48rpx
}
相关推荐
00后程序员张1 分钟前
iOS 26 App 运行状况全面解析 多工具协同监控与调试实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 小时前
iOS 混淆实战,多工具组合完成 IPA 混淆、加固与发布治理(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
2501_915918411 小时前
怎么上架 App?iOS 应用上架完整流程详解与跨平台发布实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 小时前
iOS 混淆工具链实战 多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码加固|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹2 小时前
JAVA无人共享台球杆台球柜系统球杆柜租赁系统源码支持微信小程序
java·开发语言·微信小程序·小程序
奶糖 肥晨10 小时前
微信小程序隐藏滚动条多种方法教程
微信小程序·小程序·notepad++
计算机学姐10 小时前
基于微信小程序的高校班务管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
sen_shan10 小时前
《微信小程序》第三章:Request封装
微信小程序·小程序
sen_shan10 小时前
《微信小程序》第一章:开发前准备与配置
微信小程序·小程序
游戏开发爱好者811 小时前
HTTPS 内容抓取实战 能抓到什么、怎么抓、不可解密时如何定位(面向开发与 iOS 真机排查)
android·网络协议·ios·小程序·https·uni-app·iphone