首页 > 其他分享 >uniapp项目实践总结(六)自定义顶部导航栏

uniapp项目实践总结(六)自定义顶部导航栏

时间:2023-09-01 22:22:49浏览次数:49  
标签:uniapp 自定义 navbar type url 导航 顶部

本篇主要讲述如何自定义顶部导航栏,有时候默认导航栏不足以满足我们的需求,这时候就需要自定义导航栏来解决这个问题。

目录

  • 默认导航
  • 修改配置
  • 自定义顶部

默认导航

自带的默认顶部导航设置的内容有限,不容易扩展修改,因此如果有更加个性化的需求,则需要自定义顶部导航。

配置如下:

"globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "HelloApp",
    "navigationBarBackgroundColor": "#333",
    "backgroundColor": "#f8f8f8"
}

更多配置查看:https://uniapp.dcloud.net.cn/collocation/pages.html#globalstyle
在这里插入图片描述

修改配置

pages.json文件中的globalStyle加一个配置。

"globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "HelloApp",
    "navigationBarBackgroundColor": "#333",
    "backgroundColor": "#f8f8f8",
    "navigationStyle": "custom"
}

这样我们就可以自定义顶部导航了。

自定义顶部

使用官方插件

基本用法如下:

<uni-nav-bar left-icon="left" right-icon="cart" title="标题" left-text="返回" right-text="设置" />

自己编写组件

有时候官方的自定义顶部导航可能还是达不到我们的需求,这时候可以自己编写一个自定义顶部导航组件,更加灵活高效。

编写组件

components下面新建文件夹q-navbar和文件q-navbar.vue

  • html 部分

这部分就是使用flex布局的一个导航,里面是否绑定了很多父组件的消息,可以自定义左边、中间、右边的图标、名称和是否显示。

还有一个特色就是如果不想使用默认的,可以使用slot插槽自己写适合自己的那块内容。

<view
  class="q-navbar"
  :style="{'color': props.color,  'backgroundColor': props.bgColor, 'border-bottom': `2rpx solid ${props.borColor}`}">
  <slot name="navbar">
    <view class="q-navbar-left">
      <slot name="left">
        <view class="q-navbar-item" @click="clickSet('left')" v-if="props.left.show">
          <q-icon
            class="q-navbar-icon"
            :name="props.left.icon"
            :size="18"
            color="#333"
            v-if="props.left.icon" />
          <text class="q-navbar-text" v-if="props.left.name">{{props.left.name}}</text>
        </view>
      </slot>
    </view>
    <view class="q-navbar-center">
      <slot name="center">
        <view class="q-navbar-item" @click="clickSet('center')" v-if="props.center.show">
          <q-icon
            class="q-navbar-icon"
            :name="props.center.icon"
            :size="18"
            color="#333"
            v-if="props.center.icon" />
          <text class="q-navbar-text" v-if="props.center.name">{{props.center.name}}</text>
        </view>
      </slot>
    </view>
    <view class="q-navbar-right">
      <slot name="right">
        <view class="q-navbar-item" @click="clickSet('right')" v-if="props.right.show">
          <q-icon
            class="q-navbar-icon"
            :name="props.right.icon"
            :size="18"
            color="#333"
            v-if="props.right.icon" />
          <text class="q-navbar-text" v-if="props.right.name">{{props.right.name}}</text>
        </view>
      </slot>
    </view>
  </slot>
</view>
  • 样式部分
.q-navbar {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-sizing: border-box;
  padding: 0 30rpx;
  width: 100%;
  height: $navBarHei;
  background: $f8;
  .q-navbar-item {
    display: flex;
    align-items: center;
    width: 100%;
    .q-navbar-icon {
      padding: 0 5rpx;
    }
    .q-navbar-text {
      margin-left: 10rpx;
    }
  }
  .q-navbar-left,
  .q-navbar-right {
    max-width: 120rpx;
    width: 100%;
  }
  .q-navbar-left {
    .q-navbar-item {
      justify-content: flex-start;
    }
  }
  .q-navbar-right {
    .q-navbar-item {
      justify-content: flex-end;
    }
  }
  .q-navbar-center {
    flex: 1;
    text-align: center;
    .q-navbar-item {
      justify-content: center;
    }
  }
}

uni.scss里面加入:

$navBarHei: 110rpx; // 顶部导航栏高度
  • js 部分

主要是传递数据,可以根据按钮绑定的事件进行处理。

const props = defineProps({
  // 文字颜色
  color: {
    type: String,
    default: "#333",
  },
  // 背景色
  bgColor: {
    type: String,
    default: "#f8f8f8",
  },
  // 边框色
  borColor: {
    type: String,
    default: "#e3e3e3",
  },
  // 左边配置
  left: {
    type: Object,
    default() {
      return {
        name: "", // 导航名称
        icon: "arrow-line-left", // 图标名称
        url: "", // 页面地址
        isTabbar: false, // 是否导航页面
        type: "click", // 点击类型:click默认,self自定义
        show: true, // 是否显示
      };
    },
  },
  // 中间配置
  center: {
    type: Object,
    default() {
      return {
        name: "首页",
        icon: "",
        url: "",
        isTabbar: false,
        type: "click",
        show: true,
      };
    },
  },
  // 右边配置
  right: {
    type: Object,
    default() {
      return {
        name: "",
        icon: "more",
        url: "",
        isTabbar: false,
        type: "click",
        show: true,
      };
    },
  },
});

// 发送消息
const emits = defineEmits(["change"]);

// 方法

// 点击设置
function clickSet(from = "center") {
  let info = props[from];
  info.from = from;
  if (info.type == "click") {
    let url = info.url;
    if (!url) {
      uni.navigateBack({
        delta: 1,
      });
      return;
    }
    if (info.isTabbar) {
      uni.switchTab({
        url: info.url,
      });
    } else {
      uni.navigateTo({
        url: info.url,
      });
    }
  } else {
    emits("change", info);
  }
}

预览

在这里插入图片描述
以上就是自定义顶部导航栏的主要内容,如有不足之处,请多多指正。

标签:uniapp,自定义,navbar,type,url,导航,顶部
From: https://www.cnblogs.com/guanqiweb/p/17672973.html

相关文章

  • uniapp的u-album组件自定义删除功能
    加是否显示删除按钮图片字段删除按钮删除方法删除按钮样式代码<template><viewclass="u-album"><viewclass="u-album__row"ref="u-album__row"v-for="(arr,index)inshowUrls"......
  • 博客园如何设置自定义主题?
    作者:西瓜程序猿主页传送门:https://blog.51cto.com/kimiliucn前言写博客也有一个月了,发现博客园自带的主题都不太好看,然后搜索了一下发现这款主题【Cnblogs-Theme-SimpleMemory】界面还挺好看的,也是开源的。那[西瓜程序猿]就以这个主题来介绍一下如何在博客园中进行配置,跟着一起来操......
  • uniapp 如何自定义导航栏并自适应机型
    Uniapp是一款跨平台开发框架,可以同时开发出可以在多个平台(如微信小程序、H5、App等)上运行的应用。在开发过程中,我们常常需要自定义导航栏来满足UI设计的需求。本文将介绍如何在Uniapp中自定义导航栏并自适应不同机型的屏幕大小。1.使用nav-bar组件自定义导航栏Uniapp提......
  • uniapp随机生成图片
    //生成从minNum到maxNum的随机数         randomNum(minNum,maxNum){            switch(arguments.length){               case1:                  returnparseInt(Math.random()*minNum+1......
  • uniapp 远程搜索功能的封装
    封装组件<template> <viewclass="uni-combox"> <!--输入查询选择器--> <viewclass="uni-combox__input-box"> <inputclass="uni-combox__input"type="text":placeholder="placeholder"@c......
  • 直播开发app,Xcode如何添加字体,自定义字体
    直播开发app,Xcode如何添加字体,自定义字体1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序viewdidload中加载一下一段代码 NSArray*familyNames=[UIFontfamilyNames];for(NSString*familyNameinfamilyNames){  printf("Famil......
  • naiveui | select下拉选择自定义选项渲染
    <n-selectv-model:value="selectValue"placeholder="请选择数据":options="sourceOption"clearablefilterable:render-label="renderReportsLabel"......
  • dbeaver Community :自定义内部 dashboard 模版
    dbeaver的官方文档并没说明怎么去自定义内部的dashboard模版。但是,他是开源的,有源代码可查的。而且,他是java的,即使没有源代码,你也可以简单的去反编译class来读取源代码!#==========================================================以下是我的一点研究。#=================......
  • .Net 6/NetCore3.1 Vue Element Uniapp前后端分离低代码快速开发框架
    .Net6/NetCoreVueElementUniapp前后端分离低代码快速开发框架这是一个能提高开发效率的开发框架,全自动生成PC与移动端(uniapp)代码;支持移动ios/android/h5/微信小程序。一、框架能做什么1、前后端分离项目2、纯后端项目3、移动端开发uni-app(IOS、Android、H5、微信小程......
  • js_通过js主动触发原生事件, 以及通过js注册自定义事件并手动触发
    现实情景:在对博客园的样式进行修改时,需要对博客园的中某些DOM的绑定事件进行手动触发主动触发原生事件,以click为例constoBtn2=document.querySelector('#btn2')oBtn2.addEventListener('click',()=>{console.log('click')})constevObj=document.createEv......