小程序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_915921435 小时前
iOS 应用上架多环境实战,Windows、Linux 与 Mac 的不同路径
android·ios·小程序·https·uni-app·iphone·webview
Goona_6 小时前
PyQt多窗口应用开发:构建完整的可二次开发用户登录注册模板
python·小程序·excel·pyqt
yw00yw9 小时前
小程序插件使用
java·小程序·apache
00后程序员张9 小时前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
weixin_lynhgworld20 小时前
从闲置到珍宝:旧物回收小程序系统重塑物品价值
小程序·旧物回收
2501_916007471 天前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
小小怪下士_---_1 天前
uniapp开发微信小程序自定义导航栏
前端·vue.js·微信小程序·小程序·uni-app
fakaifa2 天前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
说私域2 天前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序