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

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

目录

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

默认导航

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

配置如下:

json 复制代码
"globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "HelloApp",
    "navigationBarBackgroundColor": "#333",
    "backgroundColor": "#f8f8f8"
}

更多配置查看:uniapp.dcloud.net.cn/collocation...

修改配置

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

json 复制代码
"globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "HelloApp",
    "navigationBarBackgroundColor": "#333",
    "backgroundColor": "#f8f8f8",
    "navigationStyle": "custom"
}

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

自定义顶部

使用官方插件

基本用法如下:

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

自己编写组件

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

编写组件

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

  • html 部分

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

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

html 复制代码
<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>
  • 样式部分
scss 复制代码
.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里面加入:

scss 复制代码
$navBarHei: 110rpx; // 顶部导航栏高度
  • js 部分

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

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);
  }
}

预览

以上就是自定义顶部导航栏的主要内容,如有不足之处,请多多指正。

相关推荐
崔庆才丨静觅4 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60614 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了4 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅5 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅5 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅5 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment5 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅6 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊6 小时前
jwt介绍
前端
爱敲代码的小鱼6 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax