uniapp自定义底部导航栏

1.新建 nav-custom.vue组件

javascript 复制代码
<template>
    <view class="nav-box" :style="{'height':height+'px','background':bgColor}">
        <!-- 自定义导航栏 -->
        <view class="status_bar" :style="{'height':statusBarHeight+'px'}">
            <!-- uni-ui这里是状态栏 -->
        </view>
        <!-- 胶囊位置信息 -->
        <view class="nav-main flex align-center justify-center" :style="{height: navBarHeight+'px'}">
            <slot name='right-icon'></slot>
            <view class="nav-main-back" @click="back" v-if="backIcon">
                <uni-icons type="back" size="26" color="#fff"></uni-icons>
            </view>
            
            <text class="nav-main-title"  :style="{color: titleColor}">{{title}}</text>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            bgColor: {
                type: String,
                default: "#F5CFA8"
            },
            backIcon: {
                type: Boolean,
                default: true
            },
            title: {
                type: String,
                default: "赴日通"
            },
            titleColor: {
                type: String,
                default: "#fff"
            }
        },
        data() {
            return {
                //总高度
                height: 0,
                //胶囊位置信息
                menuButtonRect: {},
                //状态栏的高度
                statusBarHeight: 0,
                //导航栏的高度
                navBarHeight: 0
            }
        },
        created() {
            // this.height = wx.getStorageSync('navBarHeight')
            this.getHeight();
        },
        methods: {
            //获取屏幕导航栏高度
            getHeight() {
                if (wx.canIUse('getMenuButtonBoundingClientRect')) {
                    let sysInfo = wx.getSystemInfoSync(); //状态栏的高度
                    this.statusBarHeight = sysInfo.statusBarHeight;
                    // 胶囊位置信息
                    let rect = wx.getMenuButtonBoundingClientRect();
                    this.menuButtonRect = JSON.parse(JSON.stringify(rect));
                    // 导航栏高度
                    let navBarHeight = (rect.top - sysInfo.statusBarHeight) * 2 + rect.height;
                    this.navBarHeight = navBarHeight;
                    // 自定义导航栏的高度
                    this.height = sysInfo.statusBarHeight + navBarHeight;
                } else {
                    wx.showToast({
                        title: '您的微信版本过低,界面可能会显示不正常',
                        icon: 'none',
                        duration: 4000
                    });
                }
            },
            //返回
            back() {
                uni.navigateBack({
                    delta: 1
                })
            },
        }
    }
</script>

<style lang="scss" scoped>
    .status_bar {
        // height: var(--status-bar-height);
        width: 100%;
        // background:#ff0;
    }

    .nav-main {
        position: relative;

        // background:#f00;
        .nav-main-back {
            position: absolute;
            left: 10rpx;
        }

        .nav-main-title {
            color: #fff;
            font-size: 28rpx;
        }
    }
</style>

2.页面引入使用

相关推荐
code2cat3 分钟前
【随笔】MCP长任务:查询已经返回,为什么任务还没有完成
javascript·人工智能·后端·mcp
FRing039 分钟前
vue2干货总结11(短信验证、登录功能、统一处理错误(响应拦截器)、将token信息存入vuex、vuex持久化处理)
开发语言·前端·javascript·vue.js·vue2
泯泷13 分钟前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
再吃一根胡萝卜19 分钟前
DSH plugin 开发示例:五种插件形态完整代码整理
前端
afumu42 分钟前
免费 API 满地都是,为什么还有人做「让网页 AI 干活」这种东西
javascript·chrome·浏览器
Yk酱228401 小时前
A2UI随笔-demo-01
前端
云起SAAS1 小时前
本地生活同城多商户商城系统源码源码 合伙人分红加区域代理加排队返现
前端·javascript·后端
奶油话梅糖1 小时前
3 天 1,705 Star:DeepSeek 开源官方聊天前端——零后端纯静态,浏览器直连 API
前端·开源
Linye1 小时前
HTML 流程图连线交叉看不清?聊聊画布避障折线与跳弧实现
前端·人工智能
接着奏乐接着舞_2 小时前
Electron 白屏问题排查及解决记录
前端