微信小程序 自定义导航栏(1)

看着原博文https://www.cnblogs.com/sese/p/9761713.html简单的练习。

1.在app.json中     window配置navigationStyle

2.计算相关值

1. 整个导航栏的高度;

2. 胶囊按钮与顶部的距离;

3. 胶囊按钮与右侧的距离。

小程序可以通过 wx.getMenuButtonBoundingClientRect() 获取胶囊按钮的信息  和 wx.getSystemInfo() 获取设备信息

App.js 代码如下:

通过这些信息我们可以计算出上面说的3个值:

1. 整个导航栏高度 = statausBarHeight + height + (top-statausBarHeight )*2;

2. 胶囊按钮与顶部的距离 = top;

3.胶囊按钮与右侧的距离 = windowWidth - right。

3.头部组件navBar

navBar.wxml

<view class="navbar" style="height:{{navHeight}}px;">
  <view class="navbar_left" style="top:{{navTop}}px;" wx:if="{{showNav}}">
    <view bind:click="navBack"><image src="/image/back.png"></image></view>
    <view class="nav_line" bind:click="navHome"><image src="/image/home.png"></image></view>
  </view>
  <view class="navbar_title" style="top:{{navTop}}px;">
    {{pageName}}
  </view>
</view>

navBar.wxss

.navbar {
  width: 100%;
  overflow: hidden;
  position: relative;
  top: 0;
  left: 0;
  z-index: 10;
  flex-shrink: 0;
  background: #fff;
}
.navbar_left{
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  position: absolute;
  left: 10px;
  z-index: 11;
  line-height: 1;
  padding: 3px;
  border:1rpx solid #f0f0f0;
  border-radius: 40rpx;
  overflow: hidden;
  background: rgba(255,255,255,0.6);
}
.navbar_left view{
  width: 50rpx;
  height: 50rpx;
  margin-right: 10rpx;
}
.navbar_left view image{
  width: 100%;
  height: 100%;
}
.nav_line{
  border-left: 1rpx solid #f0f0f0;
  padding-left: 8px;
}
.navbar_title{
  width: 100%;
  box-sizing: border-box;
  padding-left: 115px;
  padding-right: 115px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  position: absolute;
  left: 0;
  z-index: 10;
  color: #333;
  font-size: 16px;
  font-weight: bold;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

navBar.js

// components/navBar/navBar.js
const App = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    pageName: String,  //中间的title
    showNav: {
      type: Boolean,
      value: true
    },
    showHome: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  lifetimes: {
    attached: function () {
      this.setData({
        navHeight: App.globalData.navHeight,
        navTop: App.globalData.navTop
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //回退未实现
    navBack: function () {
      wx.navigateBack({
        delta: 1
      })
    },
    //回主页未实现
    navHome: function () {
      wx.navigateTo({
        url: ‘/pages/index/index‘
      })
    },
  }
})

navBar.json

4.index中

index.json

index.wxml

最后效果图:

原文地址:https://www.cnblogs.com/yun101/p/11609407.html

时间: 2024-08-06 09:09:14

微信小程序 自定义导航栏(1)的相关文章

微信小程序自定义导航栏(wx_custom_navigation_bar) 自定义返回键、首页键,动态设置标题,响应式组件

微信小程序自定义导航栏 navigation bar 返回键 首页 github: https://github.com/chen-yt/wx_custom_navigation_bar https://github.com/Superman2113/wx_custom_navigation_bar 代码 navbar组件 navbar.wxml <view class="navbar" style="{{'height: ' + navigationBarHeight

小程序自定义导航栏

将app.json里的navigationStyle设置为navigationStyle:custom.可以在.wxss里通过margin和padding来自定义导航栏. 例如在.wxml里写 <view class="nav">我是汽车维修技师</view> 在.wxss里设置样式 .nav{height:90rpx;line-height: 90rpx;margin-top:60rpx;padding-left:20rpx;font-size:28rpx;f

微信小程序-自定义底部导航

代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html 1.基本需求. 实现用户自定义底部导航 2.案例目录结构 二.程序实现具体步骤 1.自定义nav.wxml代码 <template name="nav"> <view class=&quo

值得记录的(五)- 微信小程序自定义 tabbar

一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tabbar的情况下自制 tabbar 的方法.并说说这几种方法各自的特色. 类 navigator 跳转方式 类 navigator 跳转方式是我自己起的名称,因为它的实现思路就是这个样子的.期初参考 微信小程序自定义tabBar组件开发 这篇博文的思路.进行了这种方式的尝试,并为后续提供了解决思路.在

微信小程序自定义弹窗wcPop插件|仿微信弹窗样式

微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的是自定义组件化开发,就是把弹出框封装成一个组件,然后多处调用. 解决了小程序开发自定义弹窗出现后,遮罩层下的页面仍可以滚动的方法: 给遮罩层的最外层view中加入catchtouchmove="preventTouchMove" 即可解决该遮罩层点透问题. 根据需要还可以自定义多个按钮及事

微信小程序自定义顶部导航demo

注释:自定义导航需要自备相应图片 一.设置自定义顶部导航 Navigation是小程序的顶部导航组件,当页面配置navigationStyle设置为custom的时候可以使用此组件替代原生导航栏. 1.全局顶部导航自定义,在app.json的“window”里添加"navigationStyle": "custom" 2.只在某一个页面自定义顶部导航,在该页面的json文件里添加"navigationStyle": "custom&qu

微信小程序 自定义标题栏

微信小程序可以设置自定义标题栏,可以针对不同页面单独设置1. 在页面文件 .json 文件中,设置如下属性,自定义导航栏 和 导航栏样式"navigationStyle": "custom","navigationBarTextStyle": "white", 特别是第二个 navigationBarTextStyle 属性 可以将胶囊样式调整为透明效果 2. 设置自定义导航栏布局 样式 动效逻辑等 3. 划重点!!!导航栏中

微信小程序-自定义QQ版下拉刷新

最近给别个公司做技术支持,要实现微信小程序上拉刷新与下拉加载更多 微信给出的接口不怎么友好,最终想实现效果类似QQ手机版 ,一共3种下拉刷新状态变化,文字+图片+背景颜色 最终实现后的效果(这里提示有个不同点就是,自定义了导航条,并且下拉的时候,自定义导航条必须固定) 小程序实现下拉加载2种方式: 1. 简单粗暴,直接开启enablePullDownRefresh,开启全局下拉刷新 2.利用scroll-view组件 简单分析下2种方式的利与弊 enablePullDownRefresh方式 优

微信小程序自定义组件的使用以及调用自定义组件中的方法

在写小程序的时候,有时候页面的内容过多,逻辑比较复杂,如果全部都写在一个页面的话,会比较繁杂,代码可读性比较差,也不易于后期代码维护,这时候可以把里面某部分功能抽出来,单独封装为一个组件,也就是通常说的自定义组件,自定义组件类似于页面,它有wxml 模版.wxss 样式和js文件,然后在页面中使用该自定义组件即可. 例如,我的自定义组件代码结构是这样的: myComponent文件就是我所创的自定义组件,myComponent.wxml文件代码为: <view class="inner&q