小程序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
}
相关推荐
说私域8 小时前
移动互联网生态下定制开发开源AI智能名片S2B2C商城小程序源码在营销技术中的应用与发展
人工智能·小程序·开源
微爱帮监所写信寄信8 小时前
微爱帮监狱写信寄信工具服务器【Linux篇章】再续:TCP协议——用技术隐喻重构网络世界的底层逻辑
linux·服务器·开发语言·网络·网络协议·小程序·监狱寄信
微爱帮监所写信寄信12 小时前
微爱帮监狱写信寄信小程序:MySQL核心日志与备份恢复安全架构
数据库·mysql·小程序·邮局·监狱寄信·挂号信·邮政
Z单单13 小时前
微信小程序订单信息录入路径设置
微信小程序·小程序
码界奇点14 小时前
基于Spring Boot和微信小程序的小程序商城系统设计与实现
spring boot·微信小程序·小程序·毕业设计·源代码管理
微爱帮监所写信寄信14 小时前
微爱帮监狱寄信写信小程序:深入理解JavaScript中的Symbol特性
开发语言·javascript·网络协议·小程序·监狱寄信·微爱帮
计算机毕设指导615 小时前
基于微信小程序的智慧社区娱乐服务管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·娱乐
微爱帮监所写信寄信16 小时前
6G+AI:重构微爱帮监狱写信寄信小程序的特殊通信未来
人工智能·小程序·重构·6g·监狱信件·监狱系统·服刑人员子女
kdniao117 小时前
问答FAQ|快递鸟对接系统/小程序常见问题解答产品篇(二)
大数据·小程序
星盾网安17 小时前
智慧门店系统开发-03-新嘉丽小程序基础搭建
小程序