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

本文实例为大家分享了微信小程序自定义顶部导航组件,供大家参考,具体内容如下

在components中新建文件夹navbar

components/navbar.wxml

<!--components/navbar.wxml-->
<view class="navbar" style="height:{{navHeight+5}}px;background-image:url('{{imgUrl}}') ">
  <!-- 左上角 返回按钮 和 home按钮 wx:if="{{showNav}}" 是控制左上角按钮的显示隐藏,首页不显示 -->
  <view class="navbar_left" style="top:{{navTop}}px;height:{{jnheight-1}}px;width:{{jnwidth-3}}px" wx:if="{{showNav}}">
    <!-- 控制返回按钮的显示 -->
    <view class="navBack" bindtap="navBack">
      <image src="/images/back.png" mode="widthFix"></image>
    </view>
    <!-- home按钮 wx:if="{{showHome}}" 是控制左上角 home按钮的显示隐藏-->
    <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
      <image src="/images/index.png" mode="widthFix" style="width:50%"></image>
    </view>
  </view>
  <!-- 中间标题 -->
  <view class="navbar_title" style="top:{{navTop}}px;">{{pageName}}</view>
</view>

components/navbar.js

const App = getApp();
Component({
  // 组件的属性列表
  properties: {
    pageName: String, //中间的title
    showNav: { //判断是否显示左上角的按钮    
      type: Boolean,
      value: true
    },
    showHome: { //判断是否显示左上角的home按钮
      type: Boolean,
      value: true
    },
    imgUrl:{//图片背景路径
      type: String,
      value: App.globalData.imgLink+'index.jpg',
    },
  },
  // 组件的初始数据
  data: {
    showNav: true, //判断是否显示左上角的home按钮
    showHome: true, //判断是否显示左上角的按钮

  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function() {
      this.setData({
        navHeight: App.globalData.navHeight, //导航栏高度
        navTop: App.globalData.navTop, //胶囊按钮与顶部的距离
        jnheight: App.globalData.jnheight, //胶囊高度
        jnwidth: App.globalData.jnwidth //胶囊宽度
      })
    }
  },
  // 组件的方法列表
  methods: {
    //回退
    navBack: function() {
      wx.navigateBack()
    },
    //回主页
    navHome: function() {
      wx.switchTab({
        url: '/pages/index/index'
      })
    },
  }
})

components/navbar.wxss

/* components/navbar.wxss */
.navbar {
  width: 100%;
  overflow: hidden;
  position: sticky;
  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;
  border: 1rpx solid #f0f0f0;
  border-radius: 40rpx;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.6);
}
.navBack image{
  width: 60%;
}
 
.navbar_left view {
  width: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
 
.nav_line {
  border-left: 1rpx solid #f0f0f0;
}
 
.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;
}

在公共组件app.js中获取导航高度等信息

// app.js
App({
  onLaunch() {
    //设置导航栏
    //获取菜单按钮的布局位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    //获取系统信息
    wx.getSystemInfo({
      success: res => {
        console.log('xxxx', res)
        //状态栏的高度
        let statusBarHeight = res.statusBarHeight,
          //胶囊按钮与顶部的距离
          navTop = menuButtonObject.top,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let globalData = this.globalData;
        globalData.navHeight = navHeight;//导航栏高度
        globalData.navTop = navTop;//胶囊按钮与顶部的距离
        globalData.jnheight = menuButtonObject.height;//胶囊的高度
        globalData.jnwidth = menuButtonObject.width;//胶囊的宽度
        globalData.screenHeight = res.screenHeight;//屏幕高度
      },
      fail(err) {
        console.log(err);
      }
    });

  },
  //设置全局对象
  globalData: {
    navHeight: 0,
    navTop: 0,
    jnheight: 0,
    jnwidth: 0,
    screenHeight: 0,
  }
})

在app.json中设置导航自定义,去除默认的导航 “navigationStyle”: "custom"

 "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle": "black",
    "navigationStyle": "custom"
  },

引用到pages中的文件里

json文件中引用组件****

{
  "usingComponents":{
    "navbar":"/components/navbar/navbar"
  }
}

html文件中

<navbar page-name="{{navbar.shoppingName}}"></navbar>

js文件中

Page({
  data:{
    navbar:{
      shoppingName:'首页',
    },
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 微信小程序实现顶部导航特效

    本文实例为大家分享了微信小程序实现顶部导航的具体代码,供大家参考,具体内容如下 之前Android开发时,顶部导航用到viewPage,微信小程序里想要达到同样的效果,可用swiper来实现,先看效果图 上代码: 1.swiperTab.js Page({ data: { // tab切换 currentTab: 0, }, swichNav: function (e) { console.log(e); var that = this; if (this.data.currentTab ===

  • 微信小程序顶部可滚动导航效果

    需求是小程序做头部做导航分类的效果 顶部用 scroll-view 组件横向滚动,类似tab选项卡的效果,内容用类似模板方式引用,可重复利用 <scroll-view class="scroll-view_H" scroll-x="{{true}}" style="width: 100%"> <view wx:for="{{classify}}" wx:key="id" data-type

  • 微信小程序自定义头部导航栏(组件化)

    本文实例为大家分享了微信小程序自定义头部导航栏的具体代码,供大家参考,具体内容如下 效果图 支持 导航栏自定义背景颜色.背景图片 支持返回文字自定义 支持导航标题自定义 首先在app.json window配置项添加 "window": { "navigationStyle": "custom" } 自定义头部导航栏代码 wxml部分 <view class="cu-custom" style="height:

  • 微信小程序 基础组件与导航组件详细介绍

    微信小程序 基础组件与导航组件详解: 1.基础组件 1.1 图标 icon 1.2 文本 text 1.3 进度条 progress 2.导航组件(navigator)  1.基础组件    1.1 图标 icon (1)总结 (2) 案例 效果截图 page.wxml <view class="type-group"> <block wx:for="{{iconType}}"> <icon type="{{item}}&qu

  • 微信小程序 开发之顶部导航栏实例代码

    微信小程序 开发之顶部导航栏 需求:顶部导航栏 效果图: wxml: <!--导航条--> <view class="navbar"> <text wx:for="{{navbar}}" data-idx="{{index}}" class="item {{currentTab==index ? 'active' : ''}}" wx:key="unique" bindtap=

  • 微信小程序自定义navigationBar顶部导航栏适配所有机型(附完整案例)

    前言 navigationBar相信大家都不陌生把?今天我们就来说说自定义navigationBar,把它改变成我们想要的样子(搜索框+胶囊.搜索框+返回按钮+胶囊等). 思路 隐藏原生样式 获取胶囊按钮.状态栏相关数据以供后续计算 根据不同机型计算出该机型的导航栏高度,进行适配 编写新的导航栏 引用到页面 正文 一.隐藏原生的navigationBar window全局配置里有个参数:navigationStyle(导航栏样式),default=默认样式,custom=自定义样式. "wind

  • 微信小程序使用自定义组件导航实现当前页面高亮

    最近开发小程序,需要做一个导航,导航可以通过template写出来,但是这个项目需要在导航中处理一些逻辑,做成组件更方便些. 首先新建header文件夹,里面新建对应的js.json.wxml.wxss文件. <!-- 公共头部组件 --> <view class='headers'> <navigator open-type="redirectTo" class='logo' url="../index/index" hover-cl

  • 微信小程序顶部导航栏滑动tab效果

    小程序商品展示需要导航栏的商品分类进行滑动,供大家参考,具体内容如下 效果图: 首先是滑动的效果: <scroll-view scroll-x="true" style="width: 100%;white-space:nowrap;"> </scroll-view> 小程序使用</scroll-view>,横向移动即可 WXML:这里面我将导航栏显示类目定义为5个,每个20%,当超出5个分类,也就是index>4的时候,导

  • 微信小程序中顶部导航栏的实现代码

    微信小程序中顶部导航栏的实现 实例代码: <view class="swiper-tab"> <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">11</view> <view class="swiper-tab-list {{curre

  • 微信小程序实战之顶部导航栏(选项卡)(1)

    本文实例为大家分享了微信小程序顶部导航栏的具体代码,供大家参考,具体内容如下 需求:顶部导航栏 效果图: wxml: <!--导航条--> <view class="navbar"> <text wx:for="{{navbar}}" data-idx="{{index}}" class="item {{currentTab==index ? 'active' : ''}}" wx:key=&qu

  • 微信小程序系列之自定义顶部导航功能

    具备基础 vue框架:vue官方文档 mpvue框架:mpvue官方文档 全局配置 •找到全局的app.json文件,在配置中添加如下内容: "navigationStyle": "custom" "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", &q

  • 微信小程序 配置顶部导航条标题颜色的实现方法

    微信小程序 配置顶部导航条标题颜色的实现方法 关于小程序导航顶部配置都写在.json文件中. { "window":{ "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "navigationBarTitleText": "微信接口功能演示", "bac

  • 微信小程序点击顶部导航栏切换样式代码实例

    这篇文章主要介绍了微信小程序点击顶部导航栏切换样式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 类似这样的效果 <view class='helpCateList'> <!-- 类别 --> <scroll-view class='scroll-view' scroll-x="true"> <view class="item-content" wx:key=&qu

  • 微信小程序自定义底部导航栏组件

    本文实例为大家分享了微信小程序底部导航栏组件的具体实现代码,供大家参考,具体内容如下 1.在自己项目的公共组件的文件价下新建tabbar.vue(定义的自定义导航栏组件) <template> <view v-if="showTabbar" class="tabbar"> <view v-for="(item, index) in tabList" :key="index" class="

随机推荐