【微信小程序】顶部标题导航栏模板

以下是我自己写的一个标题导航栏模板,较为简陋,需要什么属性可自行添加。


HTML部分

<template>
  <div class="comp-navbar">
    <!-- 占位栏 -->
    <div class="placeholder-bar" :style="{height: holderBarHeight + 'px'}"> </div>
    <!-- 导航栏主体 -->
    <div class="navbar" :style="{height: navBarHeight + 'px',backgroundColor:bg}">
      <!-- 状态栏 -->
      <div class="nav-statusbar" :style="{height: statusBarHeight + 'px',background:navBackgroundColor1}"></div>
      <!-- 标题栏 -->
      <div class="nav-titlebar" :style="{height: titleBarHeight + 'px',backgroundColor:navBackgroundColor2}">
        <!-- 后退键 -->
        <div v-if="backVisible" :style="{height:rectHeight+'px' }" class="opt-back" @click="backClick()">
          <img class="back-image" src="/static/images/back.png">
        </div>
        <!-- 标题 -->
        <div class="bar-title" :style="[{color:titleColor,fontSize:titleSize+'px',lineHeight:rectHeight+5+'px'}]">{{title}}</div>
      </div>
    </div>
  </div>
</template>

javascript部分

<script>
export default {
  data(){
    return {
      holderBarHeight:20,//占位符高度
      statusBarHeight:20,//状态栏高度
      titleBarHeight:46,//标题栏高度
      navBarHeight:66,//导航栏主体高度
      rectHeight:40,
      bg:'transparent',
    }
  },
  beforeMount() {
    const self = this;
    wx.getSystemInfo({
      success(system) {
        // console.log(`system:`, system);
        self.statusBarHeight = system.statusBarHeight;
        // console.log(self.hasTitlebar)
          self.platform = system.platform;
          let platformReg = /ios/i;
          if (platformReg.test(system.platform)) {
            self.titleBarHeight = 44;
          } else {
            self.titleBarHeight = 48;
          }
        self.navBarHeight = self.statusBarHeight + self.titleBarHeight;
        if(self.hasTitlebar){
          self.holderBarHeight=self.navBarHeight;
          self.bg='#0b3299'
        }else if(!self.hasTitlebar){
          self.holderBarHeight=self.statusBarHeight;
          self.bg='transparent';
        }
      }
    });
    // 获取胶囊数据
    var rect=wx.getMenuButtonBoundingClientRect()
    // console.log(rect)
    self.rectHeight=rect.height+rect.top-self.statusBarHeight
  },
  methods:{
    backClick(){
      // console.log("back")
      wx.navigateBack({
        delta:1
      })
    },
  },
  props:[
    "hasTitlebar",//是否有标题栏
    "navBackgroundColor1",//状态栏背景颜色
    "navBackgroundColor2",//标题栏背景颜色
    "titleColor",
    "titleSize",
    "title",
    "backVisible",//是否有返回按钮
  ]
}
</script>

CSS部分

<style lang="less" scoped>
.comp-navbar {
    width: 100vw;
    .navbar {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        z-index: 9999999;
        .nav-titlebar {
            // border: 1px solid green;
            width: 100%;
            display: flex;
            // align-items: center;
            justify-content: center;
            position: relative;
            .opt-back{
              position: absolute;
              left: 0px;
              top: 0px;
              display: flex;
              justify-content: center;
              align-items: center;
              width: 32px;
              height: 40px;
              .back-image{
                width: 16px;
                height: 16px;
              }
            }
            .bar-title {
                width: 45%;
                font-size: 14px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                text-align: center;
            }
        }
    }
    .placeholder-bar{
        background-color: transparent;
        width: 100%;
    }
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值