小程序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
}
相关推荐
2501_9160074729 分钟前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
天空属于哈夫克333 分钟前
Go 语言实战:构建一个企微外部群“技术贴收藏夹”小程序后端
小程序·golang·企业微信
菜鸟una1 小时前
【微信小程序+Taro 3+NutUI 3】input (nut-input) 、 textarea (nut-texteare)类型使用避坑
前端·vue.js·微信小程序·小程序·taro
计算机毕设指导61 小时前
基于微信小程序的校园二手交易系统【源码文末联系】
java·spring boot·spring·微信小程序·小程序·tomcat·maven
Jiaberrr1 小时前
小程序setData性能优化指南:避开坑点,让页面丝滑如飞
前端·javascript·vue.js·性能优化·小程序
Code王1 小时前
【课程设计/毕业设计】基于springboot +微信小程序的家政服务平台系统基于小程序的家政服务系统安全开发【附源码、数据库、万字文档】
spring boot·小程序·课程设计
CHU7290351 小时前
旧物回收小程序前端功能版块解析:便捷赋能闲置循环
小程序
说私域1 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序驱动下的电商裂变增长路径研究
人工智能·小程序·开源·流量运营·私域运营
说私域1 小时前
六度人脉视域下信息价值传递的创新路径——基于AI智能名片链动2+1模式小程序的实践研究
人工智能·小程序·流量运营·私域运营
2501_9151063219 小时前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview