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.页面引入使用

相关推荐
Avan_菜菜6 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程
JieE2129 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE21210 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
爱勇宝10 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
IT_陈寒13 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen13 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher14 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙14 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺14 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump15 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化