小程序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
}
相关推荐
老华带你飞31 分钟前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序
卷Java4 小时前
小程序前端功能更新说明
java·前端·spring boot·微信小程序·小程序·uni-app
CTRA王大大4 小时前
【微信公众平台】小程序如何查找菜单?如何通过自定义的菜单路径生成小程序二维码?小程序二维码指定生成
小程序·微信公众平台
说私域4 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的引流爆款设计策略研究
人工智能·小程序
weixin_lynhgworld4 小时前
短剧小程序系统开发:构建便捷高效的影视观看平台
小程序·短剧
知识分享小能手4 小时前
微信小程序入门学习教程,从入门到精通,微信小程序常用API(下)——知识点详解 + 案例实战(5)
前端·javascript·学习·微信小程序·小程序·vue·前端开发
2501_915918419 小时前
Video over HTTPS,视频流(HLSDASH)在 HTTPS 下的调试与抓包实战
网络协议·http·ios·小程序·https·uni-app·iphone
说私域11 小时前
微商本地化发展模式的借鉴与探讨——以开源AI智能名片链动2+1模式S2B2C商城小程序为例
人工智能·小程序·开源
小咕聊编程12 小时前
【含文档+PPT+源码】基于微信小程序的房屋租赁系统
微信小程序·小程序·房屋租赁
云起SAAS12 小时前
菜谱食谱抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·菜谱食谱